Getting started with Arduino by BGN
Getting started with Arduino by BGN
1. Introduction
The Arduino is a small, inexpensive computer that can be programmed to control endless
creations limited only by your imagination. As you’ll soon see, the Arduino can be used to make a
whole host of projects, like a ghost detector, joystick-controlled laser, electronic die, laser trip wire
alarm, motion sensor alarm, keypad entry system, and many others. All of these projects are easy to
build and have one thing in common: they use the power of the Arduino.
In simple terms, the Arduino is a small computer that can be programmed to connect and control
various electronic parts. The Arduino has a number of pins that can be set as either input, which
means they can receive data from items such as switches, buttons, and sensors, or output, which
means they send data to control items such as motors, lights, and buzzers. This type of programmable
development board is better known as a microcontroller.
The Arduino project began in Ivrea, Italy, in 2005 with the goal of creating a device to control
student-built interactive design projects that would be less expensive than other prototyping
systems available at the time. Founders Massimo Banzi and David Cuartielles named the project
after a local bar called Arduino (an Italian masculine first name meaning “strong friend”).
The Arduino can be powered by batteries, USB, or an external power supply. Once the Arduino is
programmed, it can be disconnected from your computer and will run independently using a power
supply or batteries.
Page 1
Project 0: GETTING STARTED WITH ARDUINO
0.1 Hardware
First let’s look at the Arduino Uno board and a few pieces of hardware that you’ll use in almost every
project.
There are numerous types of Arduino boards available, but here we will exclusively use the most
popular one: the Arduino Uno, shown in Figure 0.1. The Arduino Uno is open source (meaning its
designs may be freely copied). Let’s walk through the different elements of the Arduino Uno.
The Arduino controls components you attach to it, like motors or LEDs, by sending information to
them as output (information sent out from the Arduino). Data that the Arduino reads from a sensor
is input (information going into the Arduino). There are 14 digital input/output pins (pins 0–
13). Each can be set to either input or output, and Appendix B has a full pin reference table.
Page 2
0.3 Power
The Arduino Uno board is powered from your computer’s USB port when you connect it to your PC
to upload a program. When the Arduino is not linked to your PC, you can run it independently by
connecting a 9-volt AC adapter or 9-volt battery pack with a 2.1 mm jack, with the centre pin
connected to the positive wire, shown in Figure 0-2. Simply insert the jack into the power socket of
the Arduino.
Page 3
Breadboards come in various sizes. To build some of the projects in this course, you’ll need four
breadboards: two full-size, typically with 830 holes; one half-size with 420 holes; and one mini with
170 holes. The full-size breadboard is ideal for projects that use an LCD screen or a lot of components,
and the half-size and mini boards are best for smaller projects.
The main board area of the breadboard shown in Figure 0.3 has 30 columns of tie points that are
connected vertically. There is a break in the center of the board, which you’ll often have to straddle
with components to make your circuit. This break helps to connect the pins individually so they are
not shorted together unintentionally, which can doom your project and even damage your
components. The blue and red lines at the top and bottom are power rails that you use to power the
components inserted in the main breadboard area (see Figure 0.4). The power rails connect all the
holes in the rail horizontally; the red lines are for positive power and the blue lines for negative
power (or ground, as you’ll often see it referred to).
You’ll use jumper wires to make connections on the breadboard. Jumper wires are solid-core wires
with a molded plastic holder on each end that makes it easier to insert and remove the wires. (You
could use your own wire if you have it, but make sure to use solid core wire, as stranded wire is not
strong enough to push into the hole clips). When you insert a jumper wire into a breadboard hole,
it’s held in place beneath the board by a small spring clip, making an electrical connection in that row
that typically consists of five holes. You can then place a component in an adjoining hole to help create
a circuit, as shown in Figure 0.5.
Page 4
Figure 0.5: Use of Jumpers on a breadboard
To make our projects do what we want, we need to write programs that give the Arduino instructions.
We do so using a tool called the Arduino integrated development environment (IDE). It enables you
to write computer programs (a set of step-by-step instructions, known as sketches in the Arduino
world) that you then upload to the Arduino using a USB cable. Your Arduino will carry out the
instructions based on its interaction with the outside world.
When you open the Arduino IDE, it should look very similar to Figure 0.6. The IDE is divided into a
toolbar at the top, with buttons for the most commonly used functions; the code or sketch window in
the centre, where you’ll write or view your programs; and the Serial Output window at the bottom.
The Serial Output window displays communication messages between your PC and the Arduino, and
will also list any errors if your sketch doesn’t compile properly.
Page 5
Figure 0.6: Arduino IDE
I’ll give you the sketch for each project within the relevant project, and talk through it there. All of
the sketches are available to download from http://www.nostarch.com/arduinohandbook/. Like
any program, sketches are a very strict set of instructions, and very sensitive to errors. To make
sure you’ve copied the sketch correctly, press the green check mark at the top of the screen. This is
the Verify button, and it checks for mistakes and tells you in the Serial Output window whether the
sketch has compiled correctly. If you get stuck, you can always download the sketch and then copy
and paste it into the IDE.
0.9 Libraries
In the Arduino world, a library is a small piece of code that carries out a specific function. Rather than
enter this same code repeatedly in your sketches, you can add a command that borrows code from
the library. This shortcut saves time and makes it easy for you to connect to items such as a sensor,
display, or module.
The Arduino IDE includes a number of built-in libraries, such as the Liquid Crystal library, which
makes it easy to talk to LCD displays and there are many more available online. To create some of the
projects for this course, you will need to import the following libraries: RFID, Tone, Pitches, Keypad,
Page 6
Password, Ultrasonic, New Ping, IR Remote, and DHT. You’ll find all of the libraries you need at
http://www.nostarch.com/arduinohandbook/. Once you’ve downloaded the libraries, you’ll need to
install them.
To install a library in Arduino version 1.0.6 and higher, follow these steps:
To install a library manually, go to the ZIP file containing the library and uncompressed it. For
example, if you were installing a library called “keypad” in a compressed file called keypad.zip, you
would uncompressed keypad.zip, which would expand into a folder called keypad, which in turn
contains files like keypad.cpp and keypad.h. Once the ZIP file was expanded, you would drag the
keypad folder into the libraries folder on your operating system: sketchbook/libraries in Linux, My
Documents\ Arduino\ Libraries on Windows, and Documents/Arduino/libraries on OS X. Then
restart the Arduino application.
Libraries are listed at the start of a sketch and are easily identified because they begin with the
command #include. Libraries are surrounded by angle brackets, <>, and end with .h, as in the
following call to the Servo library: #include <Servo.h>. Go ahead and install the libraries you’ll need
for the others projects the teacher will give you.
Now that you’ve seen the hardware and software, let’s begin our tour with the classic first Arduino
project: blinking a light emitting diode (LED). Not only is this the simplest way to make sure that your
Arduino is working correctly, but it will also introduce you to a simple sketch. As we mentioned
earlier, a sketch is just a series of instructions that run on a computer. The Arduino can hold only one
sketch at a time, so once you upload your sketch to your Arduino, that sketch will run every time the
Arduino is switched on until you upload a new one.
For this project we’ll use the Blink example sketch that comes with the Arduino IDE. This program
turns on an LED for 1 second and then off for 1 second, repeatedly. An LED emits light when a small
current is passed through it. The LED will work only with current flowing in one direction, so the
longer wire must connect to a positive power connection. LEDs also require a current limiting
resistor; otherwise, they may burn out. There is a built-in resistor in line with pin 13 of the Arduino.
Page 7
1. Insert the long positive leg (also known as +5V or anode) of the LED into pin 13 on the
Arduino, as shown in Figure 0.7. Connect the short negative leg (also known as cathode) to
the GND pin next to pin 13.
4. Click the Verify button (which looks like a check mark) to confirm that the sketch is working
correctly.
5. Now click the upload button to send the sketch to your Arduino.
Page 8
4. This tells the Arduino that pin 13 is an output pin, indicating that we want to send power to
the LED. The close curly bracket, { }, ends the setup code.
5. This creates a loop. Everything between the curly brackets, { }, after the loop () statement will
run once the Arduino is powered on and then repeat until it is powered off.
6. This tells the Arduino to set led (pin 13) to HIGH, which sends power to that pin. Think of it
as switching the pin on. In this sketch, this turns on the LED.
7. This tells the Arduino to wait for 1 second. Time on the Arduino is measured in milliseconds,
so 1 second = 1,000 milliseconds.
8. This tells the Arduino to set led (pin 13) to LOW, which removes power and switches off the
pin. This turns off the LED.
9. Again the Arduino is told to wait for 1 second.
10. This closing curly bracket ends the loop. All code that comes after the initial setup must be
enclosed within curly brackets. A common cause of errors in a sketch is missing open or close
brackets, which will prevent your sketch from compiling correctly. After this curly bracket,
the sketch goes back to the start of the loop at 1.
Running this code should make your LED flash on and off. Now that you’ve tested your Arduino and
understand how a sketch works and how to upload it, we’ll take a look next at the components you’ll
need to carry out all of the projects in this book. Appendix A has more details about each component,
what it looks like, and what it does.
Page 9