You are previewing Arduino Cookbook, 2nd Edition.

Arduino Cookbook, 2nd Edition

Cover of Arduino Cookbook, 2nd Edition by Michael Margolis Published by O'Reilly Media, Inc.
  1. Special Upgrade Offer
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
    3. What Was Left Out
    4. Code Style (About the Code)
    5. Arduino Platform Release Notes
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
    11. Notes on the Second Edition
  4. 1. Getting Started
    1. 1.0. Introduction
    2. 1.1. Installing the Integrated Development Environment (IDE)
    3. 1.2. Setting Up the Arduino Board
    4. 1.3. Using the Integrated Development Environment (IDE) to Prepare an Arduino Sketch
    5. 1.4. Uploading and Running the Blink Sketch
    6. 1.5. Creating and Saving a Sketch
    7. 1.6. Using Arduino
  5. 2. Making the Sketch Do Your Bidding
    1. 2.0. Introduction
    2. 2.1. Structuring an Arduino Program
    3. 2.2. Using Simple Primitive Types (Variables)
    4. 2.3. Using Floating-Point Numbers
    5. 2.4. Working with Groups of Values
    6. 2.5. Using Arduino String Functionality
    7. 2.6. Using C Character Strings
    8. 2.7. Splitting Comma-Separated Text into Groups
    9. 2.8. Converting a Number to a String
    10. 2.9. Converting a String to a Number
    11. 2.10. Structuring Your Code into Functional Blocks
    12. 2.11. Returning More Than One Value from a Function
    13. 2.12. Taking Actions Based on Conditions
    14. 2.13. Repeating a Sequence of Statements
    15. 2.14. Repeating Statements with a Counter
    16. 2.15. Breaking Out of Loops
    17. 2.16. Taking a Variety of Actions Based on a Single Variable
    18. 2.17. Comparing Character and Numeric Values
    19. 2.18. Comparing Strings
    20. 2.19. Performing Logical Comparisons
    21. 2.20. Performing Bitwise Operations
    22. 2.21. Combining Operations and Assignment
  6. 3. Using Mathematical Operators
    1. 3.0. Introduction
    2. 3.1. Adding, Subtracting, Multiplying, and Dividing
    3. 3.2. Incrementing and Decrementing Values
    4. 3.3. Finding the Remainder After Dividing Two Values
    5. 3.4. Determining the Absolute Value
    6. 3.5. Constraining a Number to a Range of Values
    7. 3.6. Finding the Minimum or Maximum of Some Values
    8. 3.7. Raising a Number to a Power
    9. 3.8. Taking the Square Root
    10. 3.9. Rounding Floating-Point Numbers Up and Down
    11. 3.10. Using Trigonometric Functions
    12. 3.11. Generating Random Numbers
    13. 3.12. Setting and Reading Bits
    14. 3.13. Shifting Bits
    15. 3.14. Extracting High and Low Bytes in an int or long
    16. 3.15. Forming an int or long from High and Low Bytes
  7. 4. Serial Communications
    1. 4.0. Introduction
    2. 4.1. Sending Debug Information from Arduino to Your Computer
    3. 4.2. Sending Formatted Text and Numeric Data from Arduino
    4. 4.3. Receiving Serial Data in Arduino
    5. 4.4. Sending Multiple Text Fields from Arduino in a Single Message
    6. 4.5. Receiving Multiple Text Fields in a Single Message in Arduino
    7. 4.6. Sending Binary Data from Arduino
    8. 4.7. Receiving Binary Data from Arduino on a Computer
    9. 4.8. Sending Binary Values from Processing to Arduino
    10. 4.9. Sending the Value of Multiple Arduino Pins
    11. 4.10. How to Move the Mouse Cursor on a PC or Mac
    12. 4.11. Controlling Google Earth Using Arduino
    13. 4.12. Logging Arduino Data to a File on Your Computer
    14. 4.13. Sending Data to Two Serial Devices at the Same Time
    15. 4.14. Receiving Serial Data from Two Devices at the Same Time
    16. 4.15. Setting Up Processing on Your Computer to Send and Receive Serial Data
  8. 5. Simple Digital and Analog Input
    1. 5.0. Introduction
    2. 5.1. Using a Switch
    3. 5.2. Using a Switch Without External Resistors
    4. 5.3. Reliably Detecting the Closing of a Switch
    5. 5.4. Determining How Long a Switch Is Pressed
    6. 5.5. Reading a Keypad
    7. 5.6. Reading Analog Values
    8. 5.7. Changing the Range of Values
    9. 5.8. Reading More Than Six Analog Inputs
    10. 5.9. Displaying Voltages Up to 5V
    11. 5.10. Responding to Changes in Voltage
    12. 5.11. Measuring Voltages More Than 5V (Voltage Dividers)
  9. 6. Getting Input from Sensors
    1. 6.0. Introduction
    2. 6.1. Detecting Movement
    3. 6.2. Detecting Light
    4. 6.3. Detecting Motion (Integrating Passive Infrared Detectors)
    5. 6.4. Measuring Distance
    6. 6.5. Measuring Distance Accurately
    7. 6.6. Detecting Vibration
    8. 6.7. Detecting Sound
    9. 6.8. Measuring Temperature
    10. 6.9. Reading RFID Tags
    11. 6.10. Tracking Rotary Movement
    12. 6.11. Tracking the Movement of More Than One Rotary Encoder
    13. 6.12. Tracking Rotary Movement in a Busy Sketch
    14. 6.13. Using a Mouse
    15. 6.14. Getting Location from a GPS
    16. 6.15. Detecting Rotation Using a Gyroscope
    17. 6.16. Detecting Direction
    18. 6.17. Getting Input from a Game Control Pad (PlayStation)
    19. 6.18. Reading Acceleration
  10. 7. Visual Output
    1. 7.0. Introduction
    2. 7.1. Connecting and Using LEDs
    3. 7.2. Adjusting the Brightness of an LED
    4. 7.3. Driving High-Power LEDs
    5. 7.4. Adjusting the Color of an LED
    6. 7.5. Sequencing Multiple LEDs: Creating a Bar Graph
    7. 7.6. Sequencing Multiple LEDs: Making a Chase Sequence (Knight Rider)
    8. 7.7. Controlling an LED Matrix Using Multiplexing
    9. 7.8. Displaying Images on an LED Matrix
    10. 7.9. Controlling a Matrix of LEDs: Charlieplexing
    11. 7.10. Driving a 7-Segment LED Display
    12. 7.11. Driving Multidigit, 7-Segment LED Displays: Multiplexing
    13. 7.12. Driving Multidigit, 7-Segment LED Displays Using MAX7221 Shift Registers
    14. 7.13. Controlling an Array of LEDs by Using MAX72xx Shift Registers
    15. 7.14. Increasing the Number of Analog Outputs Using PWM Extender Chips (TLC5940)
    16. 7.15. Using an Analog Panel Meter as a Display
  11. 8. Physical Output
    1. 8.0. Introduction
    2. 8.1. Controlling the Position of a Servo
    3. 8.2. Controlling One or Two Servos with a Potentiometer or Sensor
    4. 8.3. Controlling the Speed of Continuous Rotation Servos
    5. 8.4. Controlling Servos Using Computer Commands
    6. 8.5. Driving a Brushless Motor (Using a Hobby Speed Controller)
    7. 8.6. Controlling Solenoids and Relays
    8. 8.7. Making an Object Vibrate
    9. 8.8. Driving a Brushed Motor Using a Transistor
    10. 8.9. Controlling the Direction of a Brushed Motor with an H-Bridge
    11. 8.10. Controlling the Direction and Speed of a Brushed Motor with an H-Bridge
    12. 8.11. Using Sensors to Control the Direction and Speed of Brushed Motors (L293 H-Bridge)
    13. 8.12. Driving a Bipolar Stepper Motor
    14. 8.13. Driving a Bipolar Stepper Motor (Using the EasyDriver Board)
    15. 8.14. Driving a Unipolar Stepper Motor (ULN2003A)
  12. 9. Audio Output
    1. 9.0. Introduction
    2. 9.1. Playing Tones
    3. 9.2. Playing a Simple Melody
    4. 9.3. Generating More Than One Simultaneous Tone
    5. 9.4. Generating Audio Tones and Fading an LED
    6. 9.5. Playing a WAV File
    7. 9.6. Controlling MIDI
    8. 9.7. Making an Audio Synthesizer
  13. 10. Remotely Controlling External Devices
    1. 10.0. Introduction
    2. 10.1. Responding to an Infrared Remote Control
    3. 10.2. Decoding Infrared Remote Control Signals
    4. 10.3. Imitating Remote Control Signals
    5. 10.4. Controlling a Digital Camera
    6. 10.5. Controlling AC Devices by Hacking a Remote-Controlled Switch
  14. 11. Using Displays
    1. 11.0. Introduction
    2. 11.1. Connecting and Using a Text LCD Display
    3. 11.2. Formatting Text
    4. 11.3. Turning the Cursor and Display On or Off
    5. 11.4. Scrolling Text
    6. 11.5. Displaying Special Symbols
    7. 11.6. Creating Custom Characters
    8. 11.7. Displaying Symbols Larger Than a Single Character
    9. 11.8. Displaying Pixels Smaller Than a Single Character
    10. 11.9. Connecting and Using a Graphical LCD Display
    11. 11.10. Creating Bitmaps for Use with a Graphical Display
    12. 11.11. Displaying Text on a TV
  15. 12. Using Time and Dates
    1. 12.0. Introduction
    2. 12.1. Creating Delays
    3. 12.2. Using millis to Determine Duration
    4. 12.3. More Precisely Measuring the Duration of a Pulse
    5. 12.4. Using Arduino as a Clock
    6. 12.5. Creating an Alarm to Periodically Call a Function
    7. 12.6. Using a Real-Time Clock
  16. 13. Communicating Using I2C and SPI
    1. 13.0. Introduction
    2. 13.1. Controlling an RGB LED Using the BlinkM Module
    3. 13.2. Using the Wii Nunchuck Accelerometer
    4. 13.3. Interfacing to an External Real-Time Clock
    5. 13.4. Adding External EEPROM Memory
    6. 13.5. Reading Temperature with a Digital Thermometer
    7. 13.6. Driving Four 7-Segment LEDs Using Only Two Wires
    8. 13.7. Integrating an I2C Port Expander
    9. 13.8. Driving Multidigit, 7-Segment Displays Using SPI
    10. 13.9. Communicating Between Two or More Arduino Boards
  17. 14. Wireless Communication
    1. 14.0. Introduction
    2. 14.1. Sending Messages Using Low-Cost Wireless Modules
    3. 14.2. Connecting Arduino to a ZigBee or 802.15.4 Network
    4. 14.3. Sending a Message to a Particular XBee
    5. 14.4. Sending Sensor Data Between XBees
    6. 14.5. Activating an Actuator Connected to an XBee
    7. 14.6. Sending Messages Using Low-Cost Transceivers
    8. 14.7. Communicating with Bluetooth Devices
  18. 15. Ethernet and Networking
    1. 15.0. Introduction
    2. 15.1. Setting Up the Ethernet Shield
    3. 15.2. Obtaining Your IP Address Automatically
    4. 15.3. Resolving Hostnames to IP Addresses (DNS)
    5. 15.4. Requesting Data from a Web Server
    6. 15.5. Requesting Data from a Web Server Using XML
    7. 15.6. Setting Up an Arduino to Be a Web Server
    8. 15.7. Handling Incoming Web Requests
    9. 15.8. Handling Incoming Requests for Specific Pages
    10. 15.9. Using HTML to Format Web Server Responses
    11. 15.10. Serving Web Pages Using Forms (POST)
    12. 15.11. Serving Web Pages Containing Large Amounts of Data
    13. 15.12. Sending Twitter Messages
    14. 15.13. Sending and Receiving Simple Messages (UDP)
    15. 15.14. Getting the Time from an Internet Time Server
    16. 15.15. Monitoring Pachube Feeds
    17. 15.16. Sending Information to Pachube
  19. 16. Using, Modifying, and Creating Libraries
    1. 16.0. Introduction
    2. 16.1. Using the Built-in Libraries
    3. 16.2. Installing Third-Party Libraries
    4. 16.3. Modifying a Library
    5. 16.4. Creating Your Own Library
    6. 16.5. Creating a Library That Uses Other Libraries
    7. 16.6. Updating Third-Party Libraries for Arduino 1.0
  20. 17. Advanced Coding and Memory Handling
    1. 17.0. Introduction
    2. 17.1. Understanding the Arduino Build Process
    3. 17.2. Determining the Amount of Free and Used RAM
    4. 17.3. Storing and Retrieving Numeric Values in Program Memory
    5. 17.4. Storing and Retrieving Strings in Program Memory
    6. 17.5. Using #define and const Instead of Integers
    7. 17.6. Using Conditional Compilations
  21. 18. Using the Controller Chip Hardware
    1. 18.0. Introduction
    2. 18.1. Storing Data in Permanent EEPROM Memory
    3. 18.2. Using Hardware Interrupts
    4. 18.3. Setting Timer Duration
    5. 18.4. Setting Timer Pulse Width and Duration
    6. 18.5. Creating a Pulse Generator
    7. 18.6. Changing a Timer’s PWM Frequency
    8. 18.7. Counting Pulses
    9. 18.8. Measuring Pulses More Accurately
    10. 18.9. Measuring Analog Values Quickly
    11. 18.10. Reducing Battery Drain
    12. 18.11. Setting Digital Pins Quickly
    13. 18.12. Uploading Sketches Using a Programmer
    14. 18.13. Replacing the Arduino Bootloader
    15. 18.14. Reprogram the Uno to Emulate a Native USB device
  22. A. Electronic Components
    1. A.1. Capacitor
    2. A.2. Diode
    3. A.3. Integrated Circuit
    4. A.4. Keypad
    5. A.5. LED
    6. A.6. Motor (DC)
    7. A.7. Optocoupler
    8. A.8. Photocell (Photoresistor)
    9. A.9. Piezo
    10. A.10. Pot (Potentiometer)
    11. A.11. Relay
    12. A.12. Resistor
    13. A.13. Solenoid
    14. A.14. Speaker
    15. A.15. Stepper Motor
    16. A.16. Switch
    17. A.17. Transistor
    18. A.18. See Also
  23. B. Using Schematic Diagrams and Data Sheets
    1. B.1. How to Read a Data Sheet
    2. B.2. Choosing and Using Transistors for Switching
  24. C. Building and Connecting the Circuit
    1. C.1. Using a Breadboard
    2. C.2. Connecting and Using External Power Supplies and Batteries
    3. C.3. Using Capacitors for Decoupling
    4. C.4. Using Snubber Diodes with Inductive Loads
    5. C.5. Working with AC Line Voltages
  25. D. Tips on Troubleshooting Software Problems
    1. D.1. Code That Won’t Compile
    2. D.2. Code That Compiles but Does Not Work as Expected
  26. E. Tips on Troubleshooting Hardware Problems
    1. E.1. Still Stuck?
  27. F. Digital and Analog Pins
  28. G. ASCII and Extended Character Sets
  29. H. Migrating to Arduino 1.0
    1. Migrating Print Statements
    2. Migrating Wire (I2C) Statements
    3. Migrating Ethernet Statements
    4. Migrating Libraries
    5. New Stream Parsing Functions
  30. Index
  31. About the Author
  32. Colophon
  33. Special Upgrade Offer
  34. Copyright
O'Reilly logo

Chapter 1. Getting Started

1.0. Introduction

The Arduino environment has been designed to be easy to use for beginners who have no software or electronics experience. With Arduino, you can build objects that can respond to and/or control light, sound, touch, and movement. Arduino has been used to create an amazing variety of things, including musical instruments, robots, light sculptures, games, interactive furniture, and even interactive clothing.


If you’re not a beginner, please feel free to skip ahead to recipes that interest you.

Arduino is used in many educational programs around the world, particularly by designers and artists who want to easily create prototypes but do not need a deep understanding of the technical details behind their creations. Because it is designed to be used by nontechnical people, the software includes plenty of example code to demonstrate how to use the Arduino board’s various facilities.

Though it is easy to use, Arduino’s underlying hardware works at the same level of sophistication that engineers employ to build embedded devices. People already working with microcontrollers are also attracted to Arduino because of its agile development capabilities and its facility for quick implementation of ideas.

Arduino is best known for its hardware, but you also need software to program that hardware. Both the hardware and the software are called “Arduino.” The combination enables you to create projects that sense and control the physical world. The software is free, open source, and cross-platform. The boards are inexpensive to buy, or you can build your own (the hardware designs are also open source). In addition, there is an active and supportive Arduino community that is accessible worldwide through the Arduino forums and the wiki (known as the Arduino Playground). The forums and the wiki offer project development examples and solutions to problems that can provide inspiration and assistance as you pursue your own projects.

The recipes in this chapter will get you started by explaining how to set up the development environment and how to compile and run an example sketch.


Source code containing computer instructions for controlling Arduino functionality is usually referred to as a sketch in the Arduino community. The word sketch will be used throughout this book to refer to Arduino program code.

The Blink sketch, which comes with Arduino, is used as an example for recipes in this chapter, though the last recipe in the chapter goes further by adding sound and collecting input through some additional hardware, not just blinking the light built into the board. Chapter 2 covers how to structure a sketch for Arduino and provides an introduction to programming.


If you already know your way around Arduino basics, feel free to jump forward to later chapters. If you’re a first-time Arduino user, patience in these early recipes will pay off with smoother results later.

Arduino Software

Software programs, called sketches, are created on a computer using the Arduino integrated development environment (IDE). The IDE enables you to write and edit code and convert this code into instructions that Arduino hardware understands. The IDE also transfers those instructions to the Arduino board (a process called uploading).

Arduino Hardware

The Arduino board is where the code you write is executed. The board can only control and respond to electricity, so specific components are attached to it to enable it to interact with the real world. These components can be sensors, which convert some aspect of the physical world to electricity so that the board can sense it, or actuators, which get electricity from the board and convert it into something that changes the world. Examples of sensors include switches, accelerometers, and ultrasound distance sensors. Actuators are things like lights and LEDs, speakers, motors, and displays.

There are a variety of official boards that you can use with Arduino software and a wide range of Arduino-compatible boards produced by members of the community.

The most popular boards contain a USB connector that is used to provide power and connectivity for uploading your software onto the board. Figure 1-1 shows a basic board that most people start with, the Arduino Uno.

Basic board: the Arduino Uno. Photograph courtesy
Figure 1-1. Basic board: the Arduino Uno. Photograph courtesy

The Arduino Uno has a second microcontroller onboard to handle all USB communication; the small surface-mount chip (the ATmega8U2) is located near the USB socket on the board. This can be programmed separately to enable the board to appear as different USB devices (see Recipe 18.14 for an example). The Arduino Leonardo board replaces the ATmega8U2 and the ATmega328 controllers with a single ATmega32u4 chip that implements the USB protocol in software. The Arduino-compatible Teensy and Teensy+ boards from PJRC ( are also capable of emulating USB devices. Older boards, and most of the Arduino-compatible boards, use a chip from the FTDI company that provides a hardware USB solution for connection to the serial port of your computer.

You can get boards as small as a postage stamp, such as the Arduino Mini and Pro Mini; larger boards that have more connection options and more powerful processors, such as the Arduino Mega; and boards tailored for specific applications, such as the LilyPad for wearable applications, the Fio for wireless projects, and the Arduino Pro for embedded applications (standalone projects that are often battery-operated).

Recent additions to the range include the Arduino ADK, which has a USB host socket on it and is compatible with the Android Open Accessory Development Kit, the officially approved method of attaching hardware to Android devices. The Leonardo board uses a controller chip (the ATmega32u4) that is able to present itself as various HID devices. The Ethernet board includes Ethernet connectivity, and has a Power Over Ethernet option, so it is possible to use a single cable to connect and power the board.

Other Arduino-compatible boards are also available, including the following:

A list of Arduino-compatible boards is available at

See Also

An overview of Arduino boards:

Online guides for getting started with Arduino are available at for Windows, for Mac OS X, and for Linux.

A list of over a hundred boards that can be used with the Arduino development environment can be found at:

1.1. Installing the Integrated Development Environment (IDE)


You want to install the Arduino development environment on your computer.


The Arduino software for Windows, Mac, and Linux can be downloaded from

The Windows download is a ZIP file. Unzip the file to any convenient directory—Program Files/Arduino is a sensible place.


A free utility for unzipping files, called 7-Zip, can be downloaded from

Unzipping the file will create a folder named Arduino-00<nn> (where <nn> is the version number of the Arduino release you downloaded). The directory contains the executable file (named Arduino.exe), along with various other files and folders. Double-click the Arduino.exe file and the splash screen should appear (see Figure 1-2), followed by the main program window (see Figure 1-3). Be patient, as it can take some time for the software to load.

Arduino splash screen (Version 1.0 in Windows 7)
Figure 1-2. Arduino splash screen (Version 1.0 in Windows 7)
IDE main window (Arduino 1.0 on a Mac)
Figure 1-3. IDE main window (Arduino 1.0 on a Mac)

The Arduino download for the Mac is a disk image (.dmg); double-click the file when the download is complete. The image will mount (it will appear like a memory stick on the desktop). Inside the disk image is the Arduino application. Copy this to somewhere convenient—the Applications folder is a sensible place. Double-click the application once you have copied it over (it is not a good idea to run it from the disk image). The splash screen will appear, followed by the main program window.

Linux installation varies depending on the Linux distribution you are using. See the Arduino wiki for information (

To enable the Arduino development environment to communicate with the board, you need to install drivers.

On Windows, use the USB cable to connect your PC and the Arduino board and wait for the Found New Hardware Wizard to appear. If you are using an Uno board, let the wizard attempt to find and install drivers. It will fail to do this (don’t worry, this is the expected behavior). To fix it you now need to go to Start MenuControl PanelSystem and Security. Click on System, and then open Device Manager. In the listing that is displayed find the entry in COM and LPT named Arduino UNO (COM nn). nn will be the number Windows has assigned to the port created for the board. You will see a warning logo next to this because the appropriate drivers have not yet been assigned. Right click on the entry and select Update Driver Software. Choose the “Browse my computer for driver software” option, and navigate to the Drivers folder inside the Arduino folder you just unzipped. Select the ArduinoUNO.inf file and windows should then complete the installation process.

If you are using an earlier board (any board that uses FTDI drivers) with Windows Vista or Windows 7 and are online, you can let the wizard search for drivers and they will install automatically. On Windows XP (or if you don’t have Internet access), you should specify the location of the drivers. Use the file selector to navigate to the FTDI USB Drivers directory, located in the directory where you unzipped the Arduino files. When this driver has installed, the Found New Hardware Wizard will appear again, saying a new serial port has been found. Follow the same process as before.


It is important that you go through the sequence of steps to install the drivers two times, or the software will not be able to communicate with the board.

On the Mac, the latest Arduino boards, such as the Uno, can be used without additional drivers. When you first plug the board in a notification will pop up saying a new network port has been found, you can dismiss this. If you are using earlier boards (boards that need FTDI drivers), you will need to install driver software. There is a package named FTDIUSBSerialDriver, with a range of numbers after it, inside the disk image. Double-click this and the installer will take you through the process. You will need to know an administrator password to complete the process.

On Linux, most distributions have the driver already installed, but follow the Linux link given in this chapter’s introduction for specific information for your distribution.


If the software fails to start, check the troubleshooting section of the Arduino website,, for help solving installation problems.

See Also

Online guides for getting started with Arduino are available at for Windows, for Mac OS X, and for Linux.

1.2. Setting Up the Arduino Board


You want to power up a new board and verify that it is working.


Plug the board in to a USB port on your computer and check that the green LED power indicator on the board illuminates. Standard Arduino boards (Uno, Duemilanove, and Mega) have a green LED power indicator located near the reset switch.

An orange LED near the center of the board (labeled “Pin 13 LED” in Figure 1-4) should flash on and off when the board is powered up (boards come from the factory preloaded with software to flash the LED as a simple check that the board is working).

Basic Arduino board (Duemilanove and Uno)
Figure 1-4. Basic Arduino board (Duemilanove and Uno)

New boards such as Leonardo have the LEDs located near the USB connector; see Figure 1-5. Recent boards have duplicate pins for use with I2C (marked SCL and SDA). These boards also have a pin marked IOREF that can be used to determine the operating voltage of the chip.

Leonardo Board
Figure 1-5. Leonardo Board


The latest boards have three additional connections in the new standard for connector layout on the board. This does not affect the use of older shields (they will all continue to work with the new boards, just as they did with earlier boards). The new connections provide a pin (IOREF) for shields to detect the analog reference voltage (so that analog input values can be calibrated to the supply voltage), SCL and SDA pins to enable a consistent connection for I2C devices (the location of the I2C pins has differed on previous boards due to different chip configurations). Shields designed for the new layout should work on any board that uses the new pin locations. An additional pin (next to the IOREF pin) is not being used at the moment, but enables new functionality to be implemented in the future without needing to change the pin layout again.


If the power LED does not illuminate when the board is connected to your computer, the board is probably not receiving power.

The flashing LED (connected to digital output pin 13) is being controlled by code running on the board (new boards are preloaded with the Blink example sketch). If the pin 13 LED is flashing, the sketch is running correctly, which means the chip on the board is working. If the green power LED is on but the pin 13 LED is not flashing, it could be that the factory code is not on the chip; follow the instructions in Recipe 1.3 to load the Blink sketch onto the board to verify that the board is working. If you are not using a standard board, it may not have a built-in LED on pin 13, so check the documentation for details of your board. The Leonardo board fades the LED up and down (it looks like the LED is “breathing”) to show that the board is working.

See Also

Online guides for getting started with Arduino are available at for Windows, for Mac OS X, and for Linux.

A troubleshooting guide can be found at

1.3. Using the Integrated Development Environment (IDE) to Prepare an Arduino Sketch


You want to get a sketch and prepare it for uploading to the board.


Use the Arduino IDE to create, open, and modify sketches that define what the board will do. You can use buttons along the top of the IDE to perform these actions (shown in Figure 1-6), or you can use the menus or keyboard shortcuts (shown in Figure 1-7).

The Sketch Editor area is where you view and edit code for a sketch. It supports common text-editing keys such as Ctrl-F (⌘+F on a Mac) for find, Ctrl-Z (⌘+Z on a Mac) for undo, Ctrl-C (⌘+C on a Mac) to copy highlighted text, and Ctrl-V (⌘+V on a Mac) to paste highlighted text.

Figure 1-7 shows how to load the Blink sketch (the sketch that comes preloaded on a new Arduino board).

After you’ve started the IDE, go to the FileExamples menu and select 1. BasicsBlink, as shown in Figure 1-7. The code for blinking the built-in LED will be displayed in the Sketch Editor window (refer to Figure 1-6).

Before the code can be sent to the board, it needs to be converted into instructions that can be read and executed by the Arduino controller chip; this is called compiling. To do this, click the compile button (the top-left button with a tick inside), or select SketchVerify/Compile (Ctrl-R; ⌘+R on a Mac).

You should see a message that reads “Compiling sketch...” and a progress bar in the message area below the text-editing window. After a second or two, a message that reads “Done Compiling” will appear. The black console area will contain the following additional message:

Binary sketch size: 1026 bytes (of a 32256 byte maximum)

The exact message may differ depending on your board and Arduino version; it is telling you the size of the sketch and the maximum size that your board can accept.

Arduino IDE
Figure 1-6. Arduino IDE


Source code for Arduino is called a sketch. The process that takes a sketch and converts it into a form that will work on the board is called compilation. The IDE uses a number of command-line tools behind the scenes to compile a sketch. For more information on this, see Recipe 17.1.

IDE menu (selecting the Blink example sketch)
Figure 1-7. IDE menu (selecting the Blink example sketch)

The final message telling you the size of the sketch indicates how much program space is needed to store the controller instructions on the board. If the size of the compiled sketch is greater than the available memory on the board, the following error message is displayed:

Sketch too big; see
 for tips on reducing it.

If this happens, you need to make your sketch smaller to be able to put it on the board, or get a board with higher capacity.

If there are errors in the code, the compiler will print one or more error messages in the console window. These messages can help identify the error—see Appendix D on software errors for troubleshooting tips.


To prevent accidental overwriting of the examples, the Arduino IDE does not allow you to save changes to the provided example sketches. You must rename them using the Save As menu option. You can save sketches you write yourself with the Save button (see Recipe 1.5).

As you develop and modify a sketch, you should also consider using the FileSave As menu option and using a different name or version number regularly so that as you implement each bit, you can go back to an older version if you need to.


Code uploaded onto the board cannot be downloaded back onto your computer. Make sure you save your sketch code on your computer. You cannot save changes back to the example files; you need to use Save As and give the changed file another name.

See Also

Recipe 1.5 shows an example sketch. Appendix D has tips on troubleshooting software problems.


You want to transfer your compiled sketch to the Arduino board and see it working.


Connect your Arduino board to your computer using the USB cable. Load the Blink sketch into the IDE as described in Recipe 1.3.

Next, select ToolsBoard from the drop-down menu and select the name of the board you have connected (if it is the standard Uno board, it is probably the first entry in the board list).

Now select ToolsSerial Port. You will get a drop-down list of available serial ports on your computer. Each machine will have a different combination of serial ports, depending on what other devices you have used with your computer.

On Windows, they will be listed as numbered COM entries. If there is only one entry, select it. If there are multiple entries, your board will probably be the last entry.

On the Mac, your board will be listed twice if it is an Uno board:


If you have an older board, it will be listed as follows:


Each board will have different values for XXXXXXX. Select either entry.

Click on the upload button (in Figure 1-6, it’s the second button from the left), or choose FileUpload to I/O board (Ctrl-U, ⌘+U on a Mac).

The software will compile the code, as in Recipe 1.3. After the software is compiled, it is uploaded to the board. If you look at your board, you will see the LED stop flashing, and two lights (labeled as Serial LEDs in Figure 1-4) just below the previously flashing LED should flicker for a couple of seconds as the code uploads. The original light should then start flashing again as the code runs.


For the IDE to send the compiled code to the board, the board needs to be plugged in to the computer, and you need to tell the IDE which board and serial port you are using.

When an upload starts, whatever sketch is running on the board is stopped (if you were running the Blink sketch, the LED will stop flashing). The new sketch is uploaded to the board, replacing the previous sketch. The new sketch will start running when the upload has successfully completed.


Older Arduino boards and some compatibles do not automatically interrupt the running sketch to initiate upload. In this case, you need to press the Reset button on the board just after the software reports that it is done compiling (when you see the message about the size of the sketch). It may take a few attempts to get the timing right between the end of the compilation and pressing the Reset button.

The IDE will display an error message if the upload is not successful. Problems are usually due to the wrong board or serial port being selected or the board not being plugged in. The currently selected board and serial port are displayed in the status bar at the bottom of the Arduino window

If you have trouble identifying the correct port on Windows, try unplugging the board and then selecting ToolsSerial Port to see which COM port is no longer on the display list. Another approach is to select the ports, one by one, until you see the lights on the board flicker to indicate that the code is uploading.

See Also

The Arduino troubleshooting page:

1.5. Creating and Saving a Sketch


You want to create a sketch and save it to your computer.


To open an editor window ready for a new sketch, launch the IDE (see Recipe 1.3), go to the File menu, and select New. Paste the following code into the Sketch Editor window (it’s similar to the Blink sketch, but the blinks last twice as long):

const int ledPin =  13;    // LED connected to digital pin 13

void setup()
  pinMode(ledPin, OUTPUT);

void loop()
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(2000);                  // wait for two seconds
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(2000);                  // wait for two seconds

Compile the code by clicking the compile button (the top-left button with a triangle inside), or select SketchVerify/Compile (see Recipe 1.3).

Upload the code by clicking on the upload button, or choose FileUpload to I/O board (see Recipe 1.4). After uploading, the LED should blink, with each flash lasting two seconds.

You can save this sketch to your computer by clicking the Save button, or select FileSave.

You can save the sketch using a new name by selecting the Save As menu option. A dialog box will open where you can enter the filename.


When you save a file in the IDE, a standard dialog box for the operating system will open. It suggests that you save the sketch to a folder called Arduino in your My Documents folder (or your Documents folder on a Mac). You can replace the default sketch name with a meaningful name that reflects the purpose of your sketch. Click Save to save the file.


The default name is the word sketch followed by the current date. Sequential letters starting from a are used to distinguish sketches created on the same day. Replacing the default name with something meaningful helps you to identify the purpose of a sketch when you come back to it later.

If you use characters that the IDE does not allow (e.g., the space character), the IDE will automatically replace these with valid characters.

Arduino sketches are saved as plain text files with the extension .ino. Older versions of the IDE used the .pde extension, also used by Processing. They are automatically saved in a folder with the same name as the sketch.

You can save your sketches to any folder on your computer, but if you use the default folder (the Arduino folder in your Documents folder) your sketches will automatically appear in the Sketchbook menu of the Arduino software and be easier to locate.


If you have edited one of the examples from the Arduino download, you will not be able to save the changed file using the same filename. This preserves the standard examples intact. If you want to save a modified example, you will need to select another location for the sketch.

After you have made changes, you will see a dialog box asking if you want to save the sketch when a sketch is closed.


The § symbol following the name of the sketch in the top bar of the IDE window indicates that the sketch code has changes that have not yet been saved on the computer. This symbol is removed when you save the sketch.

The Arduino software does not provide any kind of version control, so if you want to be able to revert to older versions of a sketch, you can use Save As regularly and give each revision of the sketch a slightly different name.

Frequent compiling as you modify or add code is a good way to check for errors as you write your code. It will be easier to find and fix any errors because they will usually be associated with what you have just written.


Once a sketch has been uploaded onto the board there is no way to download it back to your computer. Make sure you save any changes to your sketches that you want to keep.

If you try and save a sketch file that is not in a folder with the same name as the sketch, the IDE will inform you that this can’t be opened as is and suggest you click OK to create the folder for the sketch with the same name.


Sketches must be located in a folder with the same name as the sketch. The IDE will create the folder automatically when you save a new sketch.

Sketches made with older versions of Arduino software have a different file extension (.pde). The IDE will open them, when you save the sketch it will create a file with the new extension (.ino). Code written for early versions of the IDE may not be able to compile in version 1.0. Most of the changes to get old code running are easy to do. See Appendix H for more details.

See Also

The code in this recipe and throughout this book use the const int expression to provide meaningful names (ledPin) for constants instead of numbers (13). See Recipe 17.5 for more on the use of constants.

1.6. Using Arduino


You want to get started with a project that is easy to build and fun to use.


This recipe provides a taste of some of the techniques that are covered in detail in later chapters.

The sketch is based on the LED blinking code from the previous recipe, but instead of using a fixed delay, the rate is determined by a light-sensitive sensor called a light dependent resistor or LDR (see Recipe 6.2). Wire the LDR as shown in Figure 1-8.

Arduino with light dependent resistor
Figure 1-8. Arduino with light dependent resistor


If you are not familiar with building a circuit from a schematic, see Appendix B for step-by-step illustrations on how to make this circuit on a breadboard.

The following sketch reads the light level of an LDR connected to analog pin 0. The light level striking the LDR will change the blink rate of the internal LED connected to pin 13:

const int ledPin =  13;     // LED connected to digital pin 13
const int sensorPin = 0;    // connect sensor to analog input 0

void setup()
  pinMode(ledPin, OUTPUT);  // enable output on the led pin

void loop()
  int rate = analogRead(sensorPin);    // read the analog input
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(rate);                  // wait duration dependent on light level
  digitalWrite(ledPin, LOW);    // set the LED off


The value of the 4.7K resistor is not critical. Anything from 1K to 10K can be used. The light level on the LDR will change the voltage level on analog pin 0. The analogRead command (see Chapter 6) provides a value that ranges from around 200 when the LDR is dark to 800 or so when it is very bright. This value determines the duration of the LED on and off times, so the blink time increases with light intensity.

You can scale the blink rate by using the Arduino map function as follows:

const int ledPin =  13;     // LED connected to digital pin 13
const int sensorPin = 0;    // connect sensor to analog input 0

// the next two lines set the min and max delay between blinks
const int minDuration = 100;  // minimum wait between blinks
const int maxDuration = 1000;  // maximum wait between blinks

void setup()
  pinMode(ledPin, OUTPUT);  // enable output on the led pin

void loop()
  int rate = analogRead(sensorPin);    // read the analog input
  // the next line scales the blink rate between the min and max values
  rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
  rate = constrain(rate, minDuration,maxDuration);    // constrain the value       

  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(rate);                  // wait duration dependent on light level
  digitalWrite(ledPin, LOW);    // set the LED off

Recipe 5.7 provides more details on using the map function to scale values. Recipe 3.5 has details on using the constrain function to ensure values do not exceed a given range.

If you want to view the value of the rate variable on your computer, you can print this to the Arduino Serial Monitor as shown in the revised loop code that follows. The sketch will display the blink rate in the Serial Monitor. You open the Serial Monitor window in the Arduino IDE by clicking on the icon on the right of the top bar (see Chapter 4 for more on using the Serial Monitor):

const int ledPin =  13;     // LED connected to digital pin 13
const int sensorPin = 0;    // connect sensor to analog input 0

// the next two lines set the min and max delay between blinks
const int minDuration = 100;  // minimum wait between blinks
const int maxDuration = 1000;  // maximum wait between blinks

void setup()
  pinMode(ledPin, OUTPUT);  // enable output on the led pin
  Serial.begin(9600);       // initialize Serial

void loop()
  int rate = analogRead(sensorPin);    // read the analog input
  // the next line scales the blink rate between the min and max values
  rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
  rate = constrain(rate, minDuration,maxDuration);    // constrain the value       
  Serial.println(rate);         // print rate to serial monitor
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(rate);                  // wait duration dependent on light level
  digitalWrite(ledPin, LOW);    // set the LED off

You can use the LDR to control the pitch of a sound by connecting a small speaker to the pin, as shown in Figure 1-9.

Connections for a speaker with the LDR circuit
Figure 1-9. Connections for a speaker with the LDR circuit

You will need to increase the on/off rate on the pin to a frequency in the audio spectrum. This is achieved, as shown in the following code, by decreasing the min and max durations:

const int outputPin = 9;    // Speaker connected to digital pin 9
const int sensorPin = 0;    // connect sensor to analog input 0

const int minDuration = 1;   // 1ms on, 1ms off (500 Hz)
const int maxDuration = 10;  // 10ms on, 10ms off (50 hz)

void setup()
  pinMode(outputPin, OUTPUT);  // enable output on the led pin

void loop()
  int sensorReading = analogRead(sensorPin);    // read the analog input
  int rate = map(sensorReading, 200,800,minDuration, maxDuration);
  rate = constrain(rate, minDuration,maxDuration);    // constrain the value
  digitalWrite(outputPin, HIGH);   // set the LED on
  delay(rate);                     // wait duration dependent on light level
  digitalWrite(outputPin, LOW);    // set the LED off

See Also

See Recipe 3.5 for details on using the constrain function.

See Recipe 5.7 for a discussion on the map function.

If you are interested in creating sounds, see Chapter 9 for a full discussion on audio output with Arduino.

The best content for your career. Discover unlimited learning on demand for around $1/day.