WHAT IS AN ARDUINO?
There are various types of Arduino board, but by far the most common is the Arduino Uno, and this is the one used for all the projects in this book (see Figure C-1).
Figure C-1: An Arduino Uno R3
The Arduino Uno shown in Figure C-1 is a revision 3 (R3) board, which is the latest at the time of writing. We’ll have a look at each of the components and their uses.
Let’s start our tour with the USB socket. This serves several purposes: it can be used to provide power to the Arduino or to connect the Arduino to your computer for programming. It can also serve as a communications link to other computers, as in “Project 13: A Raspberry Pi Control Center” on page 140 where it sends data from the Arduino to a Raspberry Pi. The little red button on the Arduino is the Reset button. Pressing it will cause the program that is installed on the Arduino to restart.
The connection sockets along both the top and bottom edges of the Arduino are where you attach electronics. On the top side of Figure C-1 are digital input and output pins, numbered 0 to 13 and configurable as either inputs or outputs. Inputs read messages coming in; for example, if you connect a switch to a digital input, the input will detect whether the switch is pressed. Outputs send information or power out; if you connect an LED to a digital output, you can turn it on by switching the output from low to high. In fact, one LED, called the L LED, is built onto the board and connected to digital pin 13.
On the right, the power LED indicates whether the board is powered. The ICSP (In-Circuit Serial Programming) header is only for advanced programming of the Arduino, and most casual users of Arduino will never use it.
The ATMega328 is a microcontroller integrated circuit (IC) and the brains of the Arduino. The chip contains 32KB of flash memory, where you store the program you want the Arduino to run.
On the bottom right of Figure C-1 is a row of analog input pins labeled A0 to A5. Digital inputs can only tell whether something is on or off, but analog inputs can actually measure the voltage at the pin, as long as the voltage is between 0V and 5V. Analog input pins could be used, for example, to measure voltage from a temperature sensor like the one used in “Project 12: Temperature Alarm” on page 131.
The final row of sockets provides miscellaneous power connections. In “Project 4: Battery Monitor” on page 53, we use Vin (volts in) to provide power to the Arduino; 5V and GND (or ground), which means 0V, are also power connections that you will need when connecting external electronics.
At the bottom left, we have a DC power jack, which is another power connection. This can accept anything between 7V and 12V DC. The Arduino will automatically accept power from the USB socket and power from the DC connector or Vin socket, too.
ARDUINO SOFTWARE
The Arduino might not be what you would expect from a computer. It has no operating system and no keyboard, monitor, or mouse. This is, of course, good news for the survivor who needs to travel light. And while you can reprogram an Arduino as many times as you like, it also only ever runs a single program (called a sketch) at a time. To program the Arduino, you must have the Arduino IDE software installed on your normal computer, so we’ll first cover installation and then talk about writing programs.
INSTALLING THE ARDUINO IDE
The Arduino IDE is easy to use, making it one major reason for the Arduino’s great popularity. It is available for Windows, Mac, and Linux computers, and it programs the Arduino over a USB connection without any need for special programming hardware.
NOTE
You will need an Internet connection to download the Arduino IDE, so do this before you start hearing about zombies on the news!
To install the Arduino IDE for your platform, download the software from the Arduino site at http://www.arduino.cc/ (click Download at the top and install the version that’s appropriate for your system). Then follow the instructions from the Getting Started link. Windows and Mac users will need to install USB drivers for the Arduino IDE to be able to communicate with the Arduino.
Once you have everything installed, run the Arduino IDE. Figure C-2 shows the Arduino IDE window with some code in it.
Figure C-2: The Arduino IDE window
The Upload button, as the name suggests, uploads the current sketch to the Arduino board. Before uploading, however, it converts the textual programming code into executable code for the Arduino and displays any errors in the Log area. The Verify button checks the code for errors without uploading the program to the board.
The serial monitor button opens the serial monitor window, which is used for two-way communication between the Arduino and another computer, as in “Project 13: A Raspberry Pi Control Center” on page 140. You can type in text messages to send to the Arduino, and you should see any responses that come back in the same window. The Status area at the bottom of the screen gives information on the type of Arduino you’re using and the corresponding serial port that will be programmed when the Upload button is pressed. The Status area in Figure C-2 also shows the type of port you would expect to see when using a Mac or Linux computer (something like /dev/cu.usbmodem411). If you’re using a Windows computer, this will display COM followed by a number.
The large, white area of the IDE is the Program Area, where you type the program code you want uploaded to the Arduino.
The File menu allows you to Open and Save sketches as you would in a word processor, and it has an Examples submenu from which you can load example sketches.
UPLOADING A SKETCH
To test out your Arduino board and make sure the Arduino IDE is properly installed, click File ▸ Examples ▸ 01. Basics to open the example sketch called Blink (shown in Figure C-2).
Use a USB cable to attach your Arduino to your computer. The power LED of the Arduino should light up as it’s plugged in, and a few other LEDs should flicker as well.
Now that the Arduino is connected, you need to tell the IDE the type of board being programmed and the serial port it’s connected to. Set the board using the menu Tools ▸ Board and then select Arduino Uno from the list of boards.
Set the serial port using the menu Tools ▸ Port. If you’re using a Windows computer, you probably won’t have many options there; you may find only the option COM4. On a Mac or Linux computer, there are generally more serial connections listed, many of which are internal devices, and it can be difficult to work out which one refers to your Arduino board.
Usually, the correct port is one that starts dev/ttyusbmodemNNNN, where NNNN is a number. In Figure C-3, the Arduino attached to my Mac has been selected.
Figure C-3: Selecting the Arduino serial port
If your Arduino doesn’t show up in the list, this usually means you have a problem with the USB drivers, so try reinstalling them. If you’re a Windows user, try rebooting.
You should now be ready to upload the sketch to the Arduino, so press the Upload button. Messages should appear in Log area, and then the TX and RX LEDs on the Arduino should flicker as the program is uploaded onto the board.
When the upload is complete, you should see a message like the one shown in Figure C-4.