Cover image for Arduino Cookbook

Book description

Create your own robots, toys, remote controllers, alarms, detectors, and more with the Arduino device, the simple microcontroller that's become popular with artists, designers, students, engineers, and hobbyists. With this book, you too can dive right in and experiment, regardless of your technical skill level. You'll find solutions for most common problems and questions Arduino users have, including everything from programming fundamentals to working with sensors and communicating over wired and wireless networks.

Table of Contents

  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
  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 the Movement of a Dial
    12. 6.11. Tracking the Movement of More Than One Rotary Encoder
    13. 6.12. Tracking the Movement of a Dial 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 from the Serial Port
    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
  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
  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
  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
    3. D.3. Troubleshooting Interrelated Hardware/Software Problems
  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. Index
  30. About the Author
  31. Colophon
  32. Special Upgrade Offer
  33. Copyright