Chapter 2. Arduino

Arduino is a simple and robust development board (Figure 2-1). It’s one of the simplest options available for making the electronics world programmable, and it’s extremely reliable as well.

Arduino connections
Figure 2-1. Arduino connections

It doesn’t take much to get started with Arduino. To make something interesting happen, you just need an Arduino Uno and a USB cable; together, it shouldn’t cost more than $35 or $40 USD. The software is free (the source code is available for people to use, study, modify, and share with others).

First, we’ll show you how to install the Arduino development environment (often called IDE, or integrated development environment) on your computer. After that, you’ll plug in a USB cable and upload your first program (called a sketch in Arduino parlance). There’s only one program you install on the Arduino—the sketch that you’re running. Aside from that, there’s nothing else to maintain because, unlike with Raspberry Pi, Arduino has no operating system. It’s just you, your program, and the bare metal.

There’s one more piece, actually. Arduino has a bootloader that occupies a small amount of the chip’s available storage. The bootloader is a small program that runs briefly when you power up or reset the board, and lets you load programs over USB without the need for a separate hardware programmer device.

The Arduino Uno is robust. It’s unlikely to suffer damage even if you were to connect a wire the wrong way (but don’t get too careless because, with enough abuse, it is possible to fry a pin on the Arduino).

It’s very easy to learn Arduino. Beginners can accomplish a lot of things just by turning pins on and off. Unlike with Raspberry Pi, you can plug analog resistance sensors directly into the Arduino without needing external hardware, because Arduino has a built-in analog-to-digital converter.

Here’s how to get set up with Arduino on Linux, Windows, and Mac.

Hello World

Now that you have Arduino the IDE open, you can run the Arduino equivalent of “Hello World.”

First, confirm that you have the correct board selected. The Arduino Uno is the default. If you have another board, such as a Mega or a Leonardo, choose it from the Tools→Board menu.

Now you need to load the Blink test program. Choose File→Examples→1.Basics→Blink. Click the Upload button (or choose File→Upload) to compile and upload your program to Arduino.

The first time you do this, Arduino may display an error popup: “Serial port COM1 not found.” That’s because you haven’t chosen which serial port to use (the connection between your computer and Arduino is represented as a USB serial port). Select your serial port from the drop-down menu. On Linux, it’s probably /dev/ttyACM0. On Mac, it may be something like /dev/usbmodem1234, and on Windows, it’s one of the COM ports.

While the program is uploading, Arduino’s TX and RX (transmit and receive) lights blink rapidly. Finally, when the program is running, the tiny light labeled “L” is blinking.

The L LED blinking means that everything was successfully installed, and you just got your first sketch running.

Congratulations! Remember this simple procedure: if you ever get so stuck you are wondering whether Arduino is even running your code at all, return to this “Hello World” example. Whenever you start a new program, start with a “Hello World” to make sure everything is working.

An Arduino program starts by executing the code inside the setup() function once. After that, the code inside loop() is repeated forever (or until you disconnect the power). See Example 2-1.

1

When Arduino boots, it executes setup() once.

2

Configures digital pin D13 to be in OUTPUT mode, so that you can control it from your program.

3

After setup() has finished, Arduino calls loop(). After loop() finishes, it’s called again. And again. Forever.

4

Sets D13 to be HIGH, which indicates Arduino is giving the pin +5 V.

5

During the delay, the pins stay as they are. Here, D13 stays HIGH, so the Arduino’s built-in L LED stays lit. During the next delay, it’s LOW, so it’s off. On for one second (1000 milliseconds), and off for one second. Forever.

Shields Make It Easy and Robust

Shields are boards that attach on top of Arduino and extend its features or make it more usable (Figure 2-2). There are many different shields available, from simple prototyping shields to more complex shields such as an Ethernet or WiFi shield. One of the best things about shields is how they reduce the need for extra wires; this is because they stack on top of the Arduino and use pin-to-pin connections instead of jumper wires. Of course there won’t always be a shield for your needs, but they are one good option to keep in mind.

Some shields don’t have any electronics on them, but are simply designed to help you prototype: these usually bring out the Arduino header pins so they are adjacent to a solderless breadboard so you can easily connect jumper wires. Our all-time favorite is the priceless ScrewShield, which adds “wings” with terminal blocks to both sides of Arduino. This eliminates loose wires, which is likely the most annoying thing about building prototypes.

You can also consider building your own shields to make easy-to-use and robust Arduino add-ons Figure 2-3. Just solder pin headers to a circuit board so that they match the pin layout of Arduino.