
Chapter 1. Getting Started with Arduino
Hello there! If you are reading this book right now, it means that you've taken your first step to make fascinating projects using Arduinos. This chapter will teach you how to set up an Arduino and write your first Arduino code.
You'll be in good hands whilst you learn some of the basics aspects of coding using the Arduino platform; this will allow you to build almost anything including robots, home automation systems, touch interfaces, sensory systems, and so on. Firstly, you will learn how to install the powerful Arduino software, then set that up, followed by hooking up your Arduino board and, after making sure that everything is fine and well, you will write your first code! Once you are comfortable with that, we will modify the code to make it do something more, which is often what Arduino coders do. We do not just create completely new programs; often we build on what has been done before, to make it better and more suited to our objectives. The contents of this chapter are divided into the following topics:
- Prerequisites
- Setting up
- Hello World
- Summary
Prerequisites
Well, you can't jump onto a horse without putting on a saddle first, can you? This section will cover what components you need to start coding on an Arduino. These can be purchased from your favorite electrical hobby store or simply ordered online.
Materials needed
- 1x Arduino-compatible board such as an Arduino UNO
- 1x USB cable A to B
- 2x LEDs
- 2x 330Ω resistors
- A mini breadboard
- 5x male-to-male jumper wires
Note
The UNO can be substituted for any other Arduino board (Mega, Leonardo, and so on) for most of the projects. These boards have their own extra features. For example, the Mega has almost double the number of I/O (input/output) pins for added functionality. The Leonardo has a feature that enables it to control the keyboard and mouse of your computer.
Setting up
This topic involves downloading the Arduino software, installing the drivers, hooking up the Arduino, and understanding the IDE menus.
Downloading and installing the software
Arduino is open source-oriented. This means all the software is free to use non-commercially. Go to Mac, make sure you choose the right Java version; similarly on Linux, download the 32-or 64-bit version according to your computer.

Arduino download page
Windows
Once you have downloaded the setup file, run it. If it asks for administrator privileges, allow it. Install it in its default location (C:\Program Files\Arduino
or C:\Program Files (x86)\Arduino
). Create a new folder in this location and rename it My Codes
or something where you can conveniently store all your programs.
Mac OS X
Once the ZIP file has finished downloading, double-click to expand it. Copy the Arduino application to the Applications
folder. You won't have to install additional drivers to make the Arduino work since we will be using only the Arduino UNO and MEGA throughout the book. You're all set.
If you didn't get anything to work, go to https://www.arduino.cc/en/guide/macOSX.
Linux (Ubuntu 12.04 and above)
Once you have downloaded the latest version of Arduino from the preceding link, install the compiler and the library packages using the following command:
sudo apt-get update && sudo apt-get install arduino arduino-core
If you are using a different version of Linux, this official Arduino walkthrough at http://playground.arduino.cc/Learning/Linux will help you out.
Connecting the Arduino
It is time to hook up the Arduino board. Plug in the respective USB terminals to the USB cable and the tiny LEDs on the Arduino should begin to flash.

Arduino UNO plugged in
If the LEDs didn't turn on, ensure that the USB port on your computer is functioning and make sure the cable isn't faulty. If it still does not light up, there is something wrong with your board and you should get it checked.
Windows
The computer will begin to install the drivers for the Arduino by itself. If it does not succeed, do the following:
- Open Device Manager.
- Click on Ports (COM & LPT).
- Right-click on Unknown Device and select Properties.
- Click on Install Driver and choose browse files on the computer.
- Choose the
drivers
folder in the previously installedArduino
folder.
The computer should say that your Arduino UNO (USB) has been successfully installed on COM port (xx). Here xx refers to a single or double digit number. If this message didn't pop up, go back to the Device Manager and check if it has been installed under COM ports.

Arduino UNO COM port
Remember the (COMxx) port that the Arduino UNO was installed on.
Mac OS X
If you are using Mac OS, a dialog box will tell you that a new network interface has been detected. Click Network Preferences and select Apply. Even though the Arduino board may show up as Not Configured, it should be working perfectly.
Linux
You are ready to go.
The serial ports for Mac OS and Linux will be obtained once the Arduino software has been launched.
The Arduino IDE
The Arduino software, commonly referred to as the Arduino IDE (Integrated Development Environment), is something that you will become really familiar with as you progress through this book. The IDE for Windows, Mac OS, and Linux is almost identical. Now let's look at some of the highlights of this software.

Arduino IDE
This is the window that you will see when you first start up the IDE. The tick/check mark verifies that your code's syntax is correct. The arrow pointing right is the button that uploads the code to the board and checks if the code has been changed since the last upload or verification. The magnifying glass is the Serial Monitor. This is used to input text or output debugging statements or sensor values.

Examples of Arduino
All Arduino programmers start by using one of these examples. Even after mastering Arduino, you will still return here to find examples to use.

Arduino tools
The screenshot shows the tools that are available in the Arduino IDE. The Board option opens up all the different boards that the software supports.
Hello World
The easiest way to start working with Arduinos begins here. You'll learn how to output print statements. The Arduino uses a Serial Monitor for displaying information such as print statements, sensor data, and the like. This is a very powerful tool for debugging long codes. Now for your first code!
Writing a simple print statement
Open up the Arduino IDE and copy the following code into a new sketch:
void setup() { Serial.begin(9600); Serial.println("Hello World!"); } void loop() { }
Open Tools | Board and choose Arduino UNO, as shown in the following screenshot:

Open Tools | Port and choose the appropriate port (remember the previous COM xx number? select that), as shown in the following screenshot. For Mac and Linux users, once you have connected the Arduino board, going to Tools | Serial Port will give you a list of ports. The Arduino is typically something like /dev/tty.usbmodem12345
where 12345 will be different.

Selecting the Port
Finally, hit the Upload button. If everything is fine, the LEDs on the Arduino should start flickering as the code is uploaded to the Arduino. The code will then have uploaded to the Arduino.
To see what you have accomplished, click on the Serial Monitor button on the right side and switch the baud rate on the Serial Monitor window to 9600.
You should see your message Hello World!
waiting for you there.
Using serial communication
Serial communication is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port which is also known as a UART. Serial data transfer is when we transfer data one bit at a time, one right after the other. Information is passed back and forth between the computer and Arduino by, essentially, setting a pin to high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it.
In this section, you will see two examples. In the first example, Arduino will send data to the computer using serial communication, while in the second example, by sending a command (serial) from the computer, you can control the functionality of the Arduino board.
Serial write
In this example, the Arduino board will communicate with the computer using the serial port, which can be viewed on your machine using the Serial Monitor.
Write the following code to your Arduino editor:
void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Hello world!"); // prints hello with ending line break } void loop() // run over and over again { // do nothing! }
Tip
Even if you have nothing in the setup or loop procedures, Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them!
Serial.begin
sets up Arduino with the transfer rate we want, in this case 9600 bits per second. Serial.println
sends data from Arduino to the computer.
Once you compile and upload it to your connected Arduino board, open Serial Monitor from the Arduino IDE. You should be able to see the Hello world! text being sent from the Arduino board:
Note
If you have trouble locating Serial Monitor, check the Understanding Arduino IDE section of this chapter.

Serial read
In the previous example, serial library was used to send a command from Arduino to your computer. In this example, you will send a command from the computer, and Arduino will do a certain operation (turn on/off LED) based on the command received:
int inByte; // Stores incoming command void setup() { Serial.begin(9600); pinMode(13, OUTPUT); // LED pin Serial.println("Ready"); // Ready to receive commands } void loop() { if(Serial.available() > 0) { // A byte is ready to receive inByte = Serial.read(); if(inByte == 'o') { // byte is 'o' digitalWrite(13, HIGH); Serial.println("LED is ON"); } else { // byte isn't 'o' digitalWrite(13, LOW); Serial.println("LED is OFF"); } } }
The inByte
function will store the incoming serial byte. From the previous example, you should be familiar with the commands written in the setup
function. In the loop function, first you need to know when a byte is available to be read. The Serial.available()
function returns the number of bytes that are available to be read. If it is greater than 0, Serial.read()
will read the byte and store it in an inByte
variable. Let's say you want to turn on the LED when the letter 'o' is available. For that you will be using the if
condition, and you will check whether the received byte is 'o' or not. If it is 'o', turn on the LED by setting pin 13 to HIGH
. Arduino will also send an LED is ON message to the computer, which can be viewed in Serial Monitor:

If it is any other character, then turn off the LED by setting pin 13 to LOW
. Arduino will also send an LED is OFF message to the computer, which can be viewed in Serial Monitor:

The world of LED
LED stands for light emitting diode, so it emits light when sufficient voltage is provided across the LED anode and cathode. Today's LEDs are available in many different types, shapes, and sizes – a direct result of the tremendous improvements in semiconductor technology over recent years. These advancements have led to better illumination, longer service life, and lower power consumption. They've also led to more difficult decision making, as there are so many types of LED to choose from.
LEDs can be categorized into miniature, high power, and application-specific LEDs:
- Miniature LEDs: These LEDs are extremely small and usually available in a single color/shape. They can be used as indicators on devices such as cell phones, calculators, and remote controls.
- High power LEDs: Often referred to as high output LEDs, these are a direct result of improved diode technology. They offer a much higher lumen output than standard LEDs. Typically, these LEDs are used in car headlights.
- Application-specific LEDs: As the name suggests, there are many LEDs that fall under this category. These are flash LEDs, RGB LEDs, seven segment display, LED lamps, and LED bars.
Using serial communication
Serial communication is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port which is also known as a UART. Serial data transfer is when we transfer data one bit at a time, one right after the other. Information is passed back and forth between the computer and Arduino by, essentially, setting a pin to high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it.
In this section, you will see two examples. In the first example, Arduino will send data to the computer using serial communication, while in the second example, by sending a command (serial) from the computer, you can control the functionality of the Arduino board.
Serial write
In this example, the Arduino board will communicate with the computer using the serial port, which can be viewed on your machine using the Serial Monitor.
Write the following code to your Arduino editor:
void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Hello world!"); // prints hello with ending line break } void loop() // run over and over again { // do nothing! }
Tip
Even if you have nothing in the setup or loop procedures, Arduino requires them to be there. That way it knows you really mean to do nothing, as opposed to forgetting to include them!
Serial.begin
sets up Arduino with the transfer rate we want, in this case 9600 bits per second. Serial.println
sends data from Arduino to the computer.
Once you compile and upload it to your connected Arduino board, open Serial Monitor from the Arduino IDE. You should be able to see the Hello world! text being sent from the Arduino board:
Note
If you have trouble locating Serial Monitor, check the Understanding Arduino IDE section of this chapter.

Serial read
In the previous example, serial library was used to send a command from Arduino to your computer. In this example, you will send a command from the computer, and Arduino will do a certain operation (turn on/off LED) based on the command received:
int inByte; // Stores incoming command void setup() { Serial.begin(9600); pinMode(13, OUTPUT); // LED pin Serial.println("Ready"); // Ready to receive commands } void loop() { if(Serial.available() > 0) { // A byte is ready to receive inByte = Serial.read(); if(inByte == 'o') { // byte is 'o' digitalWrite(13, HIGH); Serial.println("LED is ON"); } else { // byte isn't 'o' digitalWrite(13, LOW); Serial.println("LED is OFF"); } } }
The inByte
function will store the incoming serial byte. From the previous example, you should be familiar with the commands written in the setup
function. In the loop function, first you need to know when a byte is available to be read. The Serial.available()
function returns the number of bytes that are available to be read. If it is greater than 0, Serial.read()
will read the byte and store it in an inByte
variable. Let's say you want to turn on the LED when the letter 'o' is available. For that you will be using the if
condition, and you will check whether the received byte is 'o' or not. If it is 'o', turn on the LED by setting pin 13 to HIGH
. Arduino will also send an LED is ON message to the computer, which can be viewed in Serial Monitor:

If it is any other character, then turn off the LED by setting pin 13 to LOW
. Arduino will also send an LED is OFF message to the computer, which can be viewed in Serial Monitor:

The world of LED
LED stands for light emitting diode, so it emits light when sufficient voltage is provided across the LED anode and cathode. Today's LEDs are available in many different types, shapes, and sizes – a direct result of the tremendous improvements in semiconductor technology over recent years. These advancements have led to better illumination, longer service life, and lower power consumption. They've also led to more difficult decision making, as there are so many types of LED to choose from.
LEDs can be categorized into miniature, high power, and application-specific LEDs:
- Miniature LEDs: These LEDs are extremely small and usually available in a single color/shape. They can be used as indicators on devices such as cell phones, calculators, and remote controls.
- High power LEDs: Often referred to as high output LEDs, these are a direct result of improved diode technology. They offer a much higher lumen output than standard LEDs. Typically, these LEDs are used in car headlights.
- Application-specific LEDs: As the name suggests, there are many LEDs that fall under this category. These are flash LEDs, RGB LEDs, seven segment display, LED lamps, and LED bars.
LED blink
That wasn't too bad but it isn't cool enough. This little section will enlighten you, literally.
Open up a new sketch.
Go to File | Examples | 01. Basics | Blink.

Blink example
Before we upload the code, we need to make sure of one more thing. Remember the LED that we spoke about in the prerequisites? Let's learn a bit about it before plugging it in, as shown in the following image:

LED basics
We will make use of it now. Plug in the LED such that the longer leg goes into pin 13 and the shorter leg goes into the GND pin, as in the following:

LED blink setup (Fritzing)
Note
This diagram is made using software called Fritzing. This software will be used in future projects to make it cleaner to see and easier to understand as compared to a photograph with all the wires running around. Fritzing is open source software which you can learn more about at www.fritzing.org.

Arduino LED setup
Upload the code. Your LED will start blinking, as shown in the following image.

A lit up LED
Isn't it just fascinating? You just programmed your first hardware. There's no stopping you now. Before advancing to the next chapter, let's see what the code does and what happens when you change it.
This is the blink example code that you just used:
/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ //Pin 13 has an LED connected on most Arduino boards. //give it a name: int led = 13; //the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } //the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
We have three major sections in this code. This format will be used for most of the projects in the book.
int led = 13;
This line simply stores the numerical PIN value onto a variable called led
.
void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }
This is the setup
function. Here is where you tell the Arduino what is connected on each used pin. In this case, we tell the Arduino that there is an output device (LED) on pin 13
.
void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
This is the loop
function. It tells the Arduino to keep repeating whatever is inside it in a sequence. The digitalWrite
command is like a switch that can be turned ON (HIGH
) or OFF (LOW
). The delay(1000)
function simply makes the Arduino wait for a second before heading to the next line.
If you wanted to add another LED, you'd need some additional tools and some changes to the code. This is the setup that you want to create.

Connecting two LEDs to an Arduino
If this is your first time using a breadboard, take some time to make sure all the connections are in the right place. The colors of the wires don't matter. However, GND is denoted using a black wire and VCC/5V/PWR is denoted with a red wire. The two resistors, each connected in series (acting like a connecting wire itself) with the LEDs, limit the current flowing to the LEDs, making sure they don't blow up.
As before, create a new sketch and paste in the following code:
/* Double Blink Turns on and off two LEDs alternatively for one second each repeatedly. This example code is in the public domain. */ int led1 = 12; int led2 = 13; void setup() { // initialize the digital pins as an output. pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); // turn off LEDs before loop begins digitalWrite(led1, LOW); // turn the LED off (LOW is the voltage level) digitalWrite(led2, LOW); // turn the LED off (LOW is the voltage level) } //the loop routine runs over and over again forever: void loop() { digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level) digitalWrite(led2, LOW); // turn the LED off (LOW is the voltage level) delay(1000); // wait for a second digitalWrite(led1, LOW); // turn the LED off (LOW is the voltage level) digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second }
Once again, make sure the connections are made properly, especially the positive LEDs (the longer one to OUTPUT PIN) and the negative (the shorter to the GND) terminals. Save the code as DoubleBlink.ino
. Now, if you make any changes to it, you can always retrieve the backup.
Upload the code. 3… 2… 1… And there you have it, an alternating LED blink cycle created purely with the Arduino. You can try changing the delay to see its effects.
For the sake of completeness, I would like to mention that you could take this mini-project further by using a battery to power the system and decorate your desk/room/house. More on how to power the Arduino will be covered in subsequent chapters.
Summary
You have now completed the basic introduction to the world of Arduino. In short, you have successfully set up your Arduino and have written your first code. You also learned how to modify the existing code to create something new, making it more suitable for your specific needs. This methodology will be applied repeatedly while programming, because almost all the code available is open source and it saves time and energy.
In the next chapter, we will look into sensors and displays. You will build a digital ruler that you can use to measure short distances. It will consist of an ultrasound sensor to compute distance and a small LCD screen to display it. Additionally, we will look at safely powering the Arduino board using a battery so that you are not dependent on your computer for USB power every time.