
- •Acknowledgments
- •Preface
- •Who Should Read This Book
- •What’s in This Book
- •Arduino Uno and the Arduino Platform
- •Code Examples and Conventions
- •Online Resources
- •The Parts You Need
- •Starter Packs
- •Complete Parts List
- •1. Welcome to the Arduino
- •What You Need
- •What Exactly Is an Arduino?
- •Exploring the Arduino Board
- •Installing the Arduino IDE
- •Meeting the Arduino IDE
- •Hello, World!
- •Compiling and Uploading Programs
- •What If It Doesn’t Work?
- •Exercises
- •2. Creating Bigger Projects with the Arduino
- •What You Need
- •Managing Projects and Sketches
- •Changing Preferences
- •Using Serial Ports
- •What If It Doesn’t Work?
- •Exercises
- •3. Building Binary Dice
- •What You Need
- •Working with Breadboards
- •Using an LED on a Breadboard
- •First Version of a Binary Die
- •Working with Buttons
- •Adding Your Own Button
- •Building a Dice Game
- •What If It Doesn’t Work?
- •Exercises
- •4. Building a Morse Code Generator Library
- •What You Need
- •Learning the Basics of Morse Code
- •Building a Morse Code Generator
- •Fleshing Out the Morse Code Generator’s Interface
- •Outputting Morse Code Symbols
- •Installing and Using the Telegraph Class
- •Publishing Your Own Library
- •What If It Doesn’t Work?
- •Exercises
- •5. Sensing the World Around Us
- •What You Need
- •Measuring Distances with an Ultrasonic Sensor
- •Increasing Precision Using Floating-Point Numbers
- •Increasing Precision Using a Temperature Sensor
- •Creating Your Own Dashboard
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Wiring Up the Accelerometer
- •Bringing Your Accelerometer to Life
- •Finding and Polishing Edge Values
- •Building Your Own Game Controller
- •More Projects
- •What If It Doesn’t Work?
- •Exercises
- •7. Writing a Game for the Motion-Sensing Game Controller
- •Writing a GameController Class
- •Creating the Game
- •What If It Doesn’t Work?
- •Exercises
- •8. Generating Video Signals with an Arduino
- •What You Need
- •How Analog Video Works
- •Building a Digital-to-Analog Converter (DAC)
- •Connecting the Arduino to Your TV Set
- •Using the TVout Library
- •Building a TV Thermometer
- •Working with Graphics in TVout
- •What If It Doesn’t Work?
- •Exercises
- •9. Tinkering with the Wii Nunchuk
- •What You Need
- •Wiring a Wii Nunchuk
- •Talking to a Nunchuk
- •Building a Nunchuk Class
- •Using Our Nunchuk Class
- •Creating Your Own Video Game Console
- •Creating Your Own Video Game
- •What If It Doesn’t Work?
- •Exercises
- •10. Networking with Arduino
- •What You Need
- •Using Your PC to Transfer Sensor Data to the Internet
- •Registering an Application with Twitter
- •Tweeting Messages with Processing
- •Communicating Over Networks Using an Ethernet Shield
- •Using DHCP and DNS
- •What If It Doesn’t Work?
- •Exercises
- •11. Creating a Burglar Alarm with Email Notification
- •What You Need
- •Emailing from the Command Line
- •Emailing Directly from an Arduino
- •Detecting Motion Using a Passive Infrared Sensor
- •Bringing It All Together
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Understanding Infrared Remote Controls
- •Grabbing Remote Control Codes
- •Cloning a Remote
- •Controlling Infrared Devices Remotely with Your Browser
- •Building an Infrared Proxy
- •What If It Doesn’t Work?
- •Exercises
- •13. Controlling Motors with Arduino
- •What You Need
- •Introducing Motors
- •First Steps with a Servo Motor
- •Building a Blaminatr
- •What If It Doesn’t Work?
- •Exercises
- •Current, Voltage, and Resistance
- •Electrical Circuits
- •Learning How to Use a Wire Cutter
- •Learning How to Solder
- •Learning How to Desolder
- •The Arduino Programming Language
- •Bit Operations
- •Learning More About Serial Communication
- •Serial Communication Using Various Languages
- •What Are Google Chrome Apps?
- •Creating a Minimal Chrome App
- •Starting the Chrome App
- •Exploring the Chrome Serial API
- •Writing a SerialDevice Class
- •Index

Compiling and Uploading Programs • 19
means the pin will output 5 volts until further notice, and the LED connected to the pin will light up.
The program then calls delay and waits for 500 milliseconds doing nothing. During this pause, pin 13 remains in HIGH state, and the LED continues to burn. The LED is eventually turned off when we set the pin’s state back to
LOW using digitalWrite again. We wait another 500 milliseconds, and then the loop function ends. The Arduino starts it again, and the LED blinks.
In the next section, you’ll learn how to bring the program to life and transfer it to the Arduino.
Compiling and Uploading Programs
Before you compile and upload a program to the Arduino, you have to configure two things in the IDE: the type of Arduino you’re using and the serial port your Arduino is connected to. Since Arduino 1.6.0, the IDE tries to identify all Arduino boards that are connected to your computer automatically. This feature works quite well, but it also fails sometimes. So, you need to learn how to determine the type of your Arduino board and the name of the serial port it is connected to.
Identifying the Arduino type is easy, because it is printed on the board. Popular types are Uno, Duemilanove, Diecimila, Nano, Mega, Mini, NG, BT, LilyPad, Pro, or Pro Mini. In some cases, you also have to check what microcontroller your Arduino uses—most have an ATmega328. You can find the microcontroller type printed on the microcontroller itself. When you have identified the exact type of your Arduino, choose it from the Tools > Board menu.
Now you have to choose the serial port your Arduino is connected to from the Tools > Serial Port menu. On Mac OS X, the name of the serial port usually starts with /dev/tty.usbserial or /dev/tty.usbmodem. (On my MacBook Pro, it’s
/dev/tty.usbmodem24321.) On Linux systems, it should be /dev/ttyUSB0, /dev/ttyUSB1, or something similar, depending on the number of USB ports your computer has.
On Windows systems, you have to use the Device Manager to find out the right serial port. In the Device Manager, look for USB Serial Port below the Ports (COM & LPT) menu entry. (See Installing the Drivers for Current Arduino Boards, on page 11) Usually the port is named COM1, COM2, or something similar.
report erratum • discuss

Chapter 1. Welcome to the Arduino • 20
After you have chosen the right serial port, click the Verify button, and you should see the following output in the IDE’s message area (the Arduino IDE calls programs sketches):
Build options changed, rebuilding all
Sketch uses 1,030 bytes (3%) of program storage space. Maximum is 32,256 bytes. Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for
local variables. Maximum is 2,048 bytes.
This means the IDE has successfully compiled the source code into 1,030 bytes of machine code that we can upload to the Arduino. If you see an error message instead, check whether you have typed in the program correctly. (When in doubt, download the code from the book’s website.)16 Depending on the Arduino board you’re using, the byte maximum may differ. On an Arduino Duemilanove, it’s usually 14336, for example. Also, the size of the sketch might be slightly different depending on the version of the Arduino IDE.
Now click the Upload button, and after a few seconds, you should see the following output in the message area:
Sketch uses 1,030 bytes (3%) of program storage space. Maximum is 32,256 bytes. Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for
local variables. Maximum is 2,048 bytes.
This is exactly the same message we got after compiling the program, and it tells us that the 1,030 bytes of machine code were transferred successfully to the Arduino. In case of any errors, check whether you have selected the correct Arduino type and the correct serial port in the Tools menu.
During the upload process, the TX and RX LEDs will flicker for a few seconds. This is normal, and it happens whenever the Arduino and your computer communicate via the serial port. When the Arduino sends information, it turns on the TX LED. When it gets some bits, it turns on the RX LED. Because the communication is pretty fast, the LEDs start to flicker, and you cannot identify the transmission of a single byte. (If you can, you’re probably an alien.)
As soon as the code has been transmitted completely, the Arduino executes it. In our case, this means the status LED starts to blink. It turns on for half a second, then it turns off for half a second, and so on.
16. http://www.pragprog.com/titles/msard2
report erratum • discuss

What If It Doesn’t Work? • 21
The figure shows the activity on pin 5 V
13 while the program is running.
The pin starts in LOW state and doesn’t output any current. We use digitalWrite 0 V to set it to HIGH and let it output 5 volts
for 500 milliseconds. Finally, we set it back to LOW for 500 milliseconds and repeat the whole process.
That’s it! You’ve created your first physical computing project. You’ve written some code, and it makes the world brighter. Your very own digital version of “fiat lux.”17
Admittedly, the status LED doesn’t look spectacular. In Chapter 3, Building Binary Dice, on page 39, we’ll attach “real” LEDs to the Arduino.
You’ll need the theory and skills you’ve learned in this chapter for nearly every Arduino project. In the next chapter, you’ll see how to gain more control over LEDs, and you’ll learn how to benefit from more advanced features of the Arduino IDE.
What If It Doesn’t Work?
Choosing the wrong serial port or Arduino type is the most common mistake when doing the first experiments with an Arduino. If you get an error message such as “Serial port already in use” when uploading a sketch, check whether you have chosen the right serial port from the Tools > Serial Port menu. If you get messages such as “Problem uploading to board” or “Programmer is not responding,” check whether you have chosen the right Arduino board from the Tools > Board menu.
Your Arduino programs, like all programs, will contain bugs. The compiler will detect typos and syntax errors. Figure 6, The Arduino IDE explains syntax errors nicely, on page 22 shows a typical error message. Instead of pinMode, we called pinMod, and because the compiler didn’t find a function with that name, it stopped with an error message. The Arduino IDE highlights the line, showing the error with a yellow background, and prints a helpful error message.
Other bugs might be more subtle, and sometimes you have to carefully study your code and use some plain old debugging techniques. (In Debug It! Find,
17. http://en.wikipedia.org/wiki/Fiat_lux
report erratum • discuss

Chapter 1. Welcome to the Arduino • 22
Figure 6—The Arduino IDE explains syntax errors nicely.
Repair, and Prevent Bugs in Your Code [But09], you can find plenty of useful
advice on this topic.)
Exercises
•Try different blink patterns using more pauses and vary the pause length. (They don’t necessarily all have to be the same.) Also, experiment with very short pauses that make the status LED blink at a high frequency. Can you explain the effect you’re observing?
•Let the status LED output your name in Morse code.18
18.http://en.wikipedia.org/wiki/Morse_code
report erratum • discuss