Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Arduino in Action
Martin Evans, Joshua Noble, and Jordan Hochenbaum
Copyright
Brief Table of Contents
Table of Contents
Preface
Acknowledgments
About this Book
Roadmap
Code conventions and downloads
Author Online
About the Cover Illustration
Part 1. Getting started
Chapter 1. Hello Arduino
1.1. A brief history of the Arduino
1.2. The Arduino hardware
Note
1.2.1. Arduino Uno
Figure 1.1. Board layout and pins of the Arduino Uno
1.2.2. Arduino Duemilanove
1.2.3. Arduino Ethernet
1.2.4. Arduino Mega
Figure 1.2. The Arduino Mega pins and layout; note the additional input-output pins and the extra serial ports compared to the Arduino Uno.
1.2.5. Other Arduino boards
LilyPad Arduino
Figure 1.3. The LilyPad Arduino is suitable for sewing onto fabric, and there’s a range of sewable accessories available.
Arduino Nano
1.2.6. Attack of the clones
Seeeduino (yes, 3 e’s)
Seeeduino Film
Boarduino
Sippino
eBay
1.2.7. Getting an Arduino
1.3. Setting up your working environment
1.3.1. Software for Arduino
Note
1.3.2. Basic hardware setup
1.3.3. Your Arduino toolbox
1.4. Make something happen!
Note
1.4.1. Your first blinking LED
1.4.2. Sketch to make an LED blink
Listing 1.1. Code required to make an LED blink
Note
1.4.3. Connecting everything
Figure 1.4. LED inserted between pin 13 and GND. Note that the shorter leg is connected to GND.
Note
1.4.4. Uploading and testing
Figure 1.5. In this example, the Duemilanove has been selected, but you can see there’s quite a list to choose from.
Figure 1.6. Select the correct serial board from the list.
Note
Figure 1.7. Click the upload button to upload the sketch to the Arduino.
Note
1.5. Touring the IDE
1.5.1. The main editor
Figure 1.8. A typical sketch with the buttons and areas of the screen labeled
Figure 1.9. The serial monitor showing the output from an Arduino printing out an ASCII table
1.5.2. Serial monitor
1.5.3. Catching errors
Figure 1.10. The code editor reports an error we’ve introduced into the code. The code checker indicates which line it thinks the error is on, as well as what it expected.
1.5.4. Process
1.6. Anatomy of a sketch
1.6.1. A routine called setup
Listing 1.2. The setup function
1.6.2. The endless loop
Listing 1.3. An example loop function that blinks an LED on and off
1.7. Commenting code
Listing 1.4. Example header code
Listing 1.5. Example code comments
1.8. Summary
Chapter 2. Digital input and output
2.1. Getting started
Figure 2.1. The components required to complete this tutorial
2.1.1. Using a breadboard
Figure 2.2. Breadboard layout: the sockets in the top and bottom two rows are connected horizontally; the other sockets are connected vertically with a break in the center of the breadboard.
2.1.2. Circuit diagram
Figure 2.3. Schematic diagram showing Arduino connected to five LEDs
2.1.3. Adding the LEDs
2.1.4. Connecting the hardware
Figure 2.4. Making connections to the first LED with a currentlimiting resistor and pin 12 of the Arduino
Figure 2.5. Connections of the five resistors to pins 8 through 12 on the Arduino
Figure 2.6. The completed circuit with power being provided by the USB connection
2.1.5. Sketch to flash five LEDs
Listing 2.1. Five flashing LEDs, flashing after each other
Note
2.1.6. Upload and test
Note
2.2. Gaining control
2.2.1. Circuit diagram
Figure 2.7. Schematic of an Arduino connected to five LEDs controlled by a push button
2.2.2. Connections
Figure 2.8. Connecting the push button to the breadboard
Figure 2.9. The completed circuit connected to the USB for power
2.2.3. Interrupts butting in
2.2.4. Sketch to control the LEDs with a push button
Listing 2.2. Start-stop display
Note
Caution
2.2.5. Upload and test
2.2.6. Time for a break
Listing 2.3. Adding a break
2.2.7. Upload and test
2.3. Reaction tester
2.3.1. Circuit diagram
Figure 2.10. Schematic of Arduino with a push button and seven LEDs: two of them are used as start/stop indicators.
2.3.2. Connections
Figure 2.11. Completed connections with two additional LEDs for stop and start
2.3.3. Sketch to test reaction speed
Listing 2.4. Reaction tester
2.3.4. Upload and test
2.4. Reactometer: Who really has the fastest reaction time?
2.4.1. Sketch to measure reaction speed
Listing 2.5. Reaction timer
2.4.2. Upload and test
Figure 2.12. Final circuit running the reactometer
Figure 2.13. Serial monitor displaying reaction times
2.5. Summary
Chapter 3. Simple projects: input and output
3.1. Time to get analog
3.1.1. What’s the difference between analog and digital?
Table 3.1. The Arduino’s analog resolution and analog input and output pins
3.1.2. Reading a potentiometer
Figure 3.1. A selection of potentiometers
Figure 3.2. Schematic symbols for a potentiometer: U.S. (left), International (center), Fritzing (right)
3.1.3. Connecting the hardware
Figure 3.3. A circuit diagram showing the potentiometer connected to the Arduino
Figure 3.4. The potentiometer connected to the Arduino
3.1.4. Sketch to read a potentiometer
Listing 3.1. Reading a potentiometer
3.1.5. Upload and test
Figure 3.5. Output displayed as the potentiometer is rotated
3.2. A piezoelectric transducer
Figure 3.6. A typical piezoelectric transducer used in some musical cards and as sensors on drum kits
Figure 3.7. When a piezoelectric transducer is distorted, it produces an electric charge; alternately squeezing and releasing the transducer will produce a varying voltage.
Figure 3.8. When varying voltage is applied to a piezoelectric transducer, the transducer’s shape distorts.
3.2.1. The circuit diagram
Figure 3.9. A piezoelectric transducer attached to analog input A0. The zener diode protects the Arduino from the high voltages produced by the transducer when it’s struck.
3.2.2. Connecting the hardware
Figure 3.10. The completed circuit connected to the Arduino. Note the orientation of the zener diode and the polarity of the piezoelectric transducer.
3.2.3. Sketch to measure output from a piezoelectric transducer
Figure 3.11. The serial monitor showing the results of squeezing or tapping the piezoelectric transducer
Listing 3.2. Threshold for a piezoelectric transducer
3.2.4. Upload and test
Figure 3.12. Output from hitting the piezoelectric transducer with varying degrees of force
3.2.5. Circuit with added speaker
Figure 3.13. A speaker has been added to the circuit, with which you’ll output a tone.
3.2.6. Connecting the hardware
Figure 3.14. Connections with the addition of the speaker
3.2.7. Sketch to generate a tone
Listing 3.3. Generating a tone in the speaker
3.2.8. Upload and test
Tip
3.3. Making a pentatonic or five-tone keyboard
3.3.1. Circuit diagram
Figure 3.15. Circuit diagram for pentatonic keyboard
3.3.2. Connecting the hardware
Figure 3.16. The pentatonic keyboard fully assembled
3.3.3. Sketch to create a pentatonic keyboard
Listing 3.4. Pentatonic keyboard sketch
Note
3.3.4. Upload and test
3.4. Summary
Part 2. Putting Arduino to work
Chapter 4. Extending Arduino
4.1. Extending the Arduino with libraries
4.2. Core library
4.3. Standard libraries
Note
4.3.1. Test-driven development with ArduinoTestSuite
Table 4.1. The tests available in the ArduinoTestSuite library
4.3.2. Storing values using EEPROM
Table 4.2. EEPROM memory on microcontrollers used on Arduino boards
Note
Table 4.3. The functions available in the EEPROM library
Note
4.3.3. Storing more data with SD
Table 4.4. Key differences between SD and SDHC memory cards
Note
Table 4.5. The functions provided by the SD library
Note
Note
Figure 4.1. The microSD shield from SparkFun Electronics
4.3.4. Get connected with Ethernet
Table 4.6. Some of the functions provided by the Ethernet library
4.3.5. Serial communication with Firmata
Table 4.7. Typical Firmata functions
4.3.6. Displaying data using the LiquidCrystal library
Table 4.8. Some LiquidCrystal library functions
4.3.7. Controlling a servo motor
Table 4.9. The main functions provided by the Servo library
Note
4.3.8. Turning a stepper motor
Table 4.10. Main functions provided by the Stepper library
4.3.9. Communicating with SPI peripherals
Table 4.11. Four wire designations for SPI on the Arduino
Table 4.12. SPI functions
Table 4.13. The setDataMode modes dependent on clock phase and clock polarity.
4.3.10. Communicating with the two-wire interface
Table 4.14. Pin designations on the standard Arduino and the Mega for I2C
Table 4.15. List of main Wire library functions
4.3.11. Get more serial ports with SoftwareSerial
Table 4.16. The functions of the SoftwareSerial library
Listing 4.1. Using the SoftwareSerial library with two ports
4.4. Contributed libraries
4.4.1. Installing a new library
Figure 4.2. The contributed libraries available to a sketch after installation
Note
4.5. Expanding the Arduino with shields
4.5.1. Common shields
Motor shields
Figure 4.3. A motor shield from adafruit.com
Ethernet
Figure 4.4. The official Arduino Ethernet shield
Wi-Fi
Figure 4.5. The WiFly shield from SparkFun
Prototyping shields
Figure 4.6. A prototyping shield from adafruit.com
Note
4.5.2. Gotchas: will it work with my Arduino?
4.6. Summary
Chapter 5. Arduino in motion
5.1. Getting up to speed with DC motors
Figure 5.1. A DC motor complete with gearbox from solarbotics.com
5.1.1. Stopping and starting
Figure 5.2. The elements of an SPDT relay
Note
Figure 5.3. A NPN 2N2222 transistor in a TO-92 plastic package
5.1.2. Sketch to turn a small DC motor on and off
Listing 5.1. Sketch to turn a small DC motor on or off
5.1.3. Connecting the hardware
Figure 5.4. A 2N2222 NPN transistor with legs connected to a TO-92 plastic package on the left and a TO-18 metal package on the right
Note
Figure 5.5. Schematic symbol for an NPN transistor showing the base, collector, and emitter
Note
Note
Figure 5.6. Schematic diagram for switching a motor on and off with a transistor and a relay
Figure 5.7. Pinouts of DPDT relay
Figure 5.8. Completed circuit controlling a motor with a relay
5.1.4. Upload and test
5.2. Speed control and reverse
Note
Figure 5.9. Using a potentiometer to control the speed of a motor
5.2.1. PWM to the rescue
Figure 5.10. Output from an Arduino using the analogWrite function
Note
5.2.2. The H-bridge for motor control
Figure 5.11. An H-bridge made up of four switches to control the direction of a motor
Table 5.1. Motor action based on position of switches in the H-bridge shown in figure 5.11
Note
5.2.3. The L293D dual H driver
Figure 5.12. Pinouts of the L293D dual H driver
Table 5.2. Pinouts of L293D dual H driver
5.2.4. Connecting the hardware
Figure 5.13. Circuit diagram showing connections between the motor, the L293D, and the Arduino
Figure 5.14. DC motor control using an L293D integrated circuit
Note
5.2.5. Sketch to control a motor with an L293D
Table 5.3. Truth table for L293D connected to a DC motor
Listing 5.2. Using an L293D to control a small DC motor
5.2.6. Upload and test
5.2.7. Changing motor speed
Listing 5.3. Controlling motor speed with an L293D
5.2.8. Upload and test
Note
5.3. Stepper motors: one step at a time
Figure 5.15. A stepper motor purchased from eBay
Note
5.3.1. Unipolar or bipolar
Table 5.4. Main differences between unipolar and bipolar stepper motors
Note
Figure 5.16. Label on the back of a stepper motor
Figure 5.17. Measuring the resistance between two stepper motor wires
Table 5.5. Record of resistance between stepper motor wires
Figure 5.18. Resistance measured from coils of a unipolar stepper motor
Figure 5.19. A surplus bipolar stepper motor pulled from an old printer
Figure 5.20. Label on reverse of bipolar stepper motor
Table 5.6. Specification of surplus bipolar stepper motor
5.3.2. Connecting the hardware
Figure 5.21. A schematic diagram using an L293D to drive a bipolar stepper motor
Note
Figure 5.22. Circuit connections between the L293D and the unipolar stepper motor
5.3.3. Stepper motor library functions
Stepper
Note
setSpeed
steps
5.3.4. Sketch to control a stepper motor
Listing 5.4. Sketch to drive a stepper motor
5.3.5. Upload and test
5.4. Try not to get in a flap with servomotors
Figure 5.23. A typical small servomotor
5.4.1. Controlling a servomotor
Figure 5.24. Relationship between pulse width and servo angle
5.4.2. Servomotor functions and methods
Table 5.7. Servo library functions
Note
5.4.3. Sketch to control a servomotor
Listing 5.5. Sketch to rotate a servomotor between 0 and 180 degrees
5.4.4. Connecting the hardware
Figure 5.25. Section of single-row header 0.1-inch pitch to connect servomotor to breadboard
Figure 5.26. Connections between servomotor and Arduino
5.4.5. Upload and test
5.5. Mighty power comes in small packages with brushless DC motors
5.5.1. Why go brushless
Figure 5.27. An outrunner (top) and an inrunner (bottom) brushless motor
5.5.2. Gaining control
Caution
Note
Note
5.5.3. Sketch to control a brushless motor
Listing 5.6. Sketch to control a brushless motor in one direction
Listing 5.7. Sketch to use with ESC that can control speed and direction
5.5.4. Connecting the hardware
Tip
Figure 5.28. Brushless motor controlled by an Arduino
5.5.5. Upload and test
5.5.6. Reverse
5.5.7. Sketch to reverse a brushless motor
Listing 5.8. Sketch to control a brushless motor in both forward and reverse
5.5.8. Connecting the hardware
5.5.9. Upload and test
5.6. The motor control shield for more motors
Figure 5.29. Components supplied in the Adafruit Industries motor control shield kit
Figure 5.30. The fully-assembled motor controller shield
5.7. Summary
Chapter 6. Object detection
Figure 6.1. How ultrasonic waves are transmitted and received by a distance sensor
6.1. Object detection with ultrasound
6.1.1. Choosing an ultrasonic sensor
Figure 6.2. The Devantech SRF05, an ultrasonic sensor
Figure 6.3. The Parallax Ping, an ultrasonic sensor
6.1.2. Three wires or four
6.1.3. Sketches for ultrasonic object finding
Sketch for the Parallax Ping
Listing 6.1. Reading ranges with the Parallax Ping
Sketch for the Devantech SRF05
Listing 6.2. Reading distances with the SRF05
6.1.4. Connecting the hardware
Figure 6.4. Connecting the Parallax Ping to the Arduino
Figure 6.5. Connecting the Devantech SRF05 to the Arduino
6.1.5. Upload and test
6.2. Infrared for range finding
6.2.1. Infrared and ultrasound together
6.2.2. The Sharp GP2D12 range finder
Figure 6.6. The Sharp GP2D12 IR Ranger
6.2.3. Nonlinear algorithm for calculating distance
Figure 6.7. Distance to voltage output from the GP2D12
6.2.4. Sketch for range finding
Listing 6.3. Creating a theremin with the GP2D12
6.2.5. Connecting the hardware
Figure 6.8. Connecting the GP2D12 to the Arduino
6.2.6. Upload and test
6.3. Passive infrared to detect movement
6.3.1. Using the Parallax PIR sensor
Figure 6.9. The Parallax PIR sensor
6.3.2. Sketch for infrared motion detection
Listing 6.4. Detecting motion with the Parallax PIR sensor
6.3.3. Connecting the hardware
Figure 6.10. Connecting the Parallax PIR sensor to the Arduino
6.3.4. Upload and test
6.4. Summary
Chapter 7. LCD displays
7.1. Introduction to LCDs
7.1.1. String variables: String type vs. char type
Table 7.1. The functions of the Arduino String class
Note
Table 7.2. Possible char type string array initializations
7.2. Parallel character LCDs: the Hitachi HD44780
7.2.1. 4-bit or 8-bit?
7.2.2. Library and functions
Table 7.3. The functions available in the LiquidCrystal LCD library
7.2.3. Circuit diagram
Figure 7.1. The connections between a Hitachi HD44780-based LCD display and the Arduino
Note
7.2.4. Connecting everything up in 4-bit mode
Figure 7.2. Power and contrast wiring for the Hitachi HD44780 parallel LCD
Note
Table 7.4. The required circuit connections between the Hitachi HD44780 LCD and the Arduino
7.2.5. Sketch for writing to the Hitachi HD44780
Listing 7.1. Writing text onto your LCD
7.2.6. Upload and test
Figure 7.3. Completing the wiring for the Hitachi HD44780 parallel LCD
7.3. Serial LCD weather station
7.3.1. Serial vs. parallel LCDs
7.3.2. SerLCD library and functions
Table 7.5. The functions available in the SerLCD library
Note
7.3.3. The Maxim IC DS18B20 temperature sensor
7.3.4. OneWire and DallasTemperature libraries
Note
7.3.5. Circuit diagram
Figure 7.4. Circuit diagram for a weather station using the SparkFun (or compatible) serial LCD, and the DS18B20 one-wire digital temperature sensor
7.3.6. Connecting everything up
Table 7.6. Required circuit connections between the serial LCD and the Arduino
Figure 7.5. Pin layout for the DS18B20 temperature sensor
Figure 7.6. Completed wiring for a DS18B20-based LCD weather station
Table 7.7. Required circuit connections between the DS18B20 one-wire temperature sensor and the Arduino
7.3.7. Sketch for an LCD weather station
Listing 7.2. Weather station
Note
7.3.8. Upload and test
7.4. Graphic LCDs: the Samsung KS0108 GLCD
7.4.1. Library and functions
Table 7.8. The functions available in the GLCDks0108 library
Note
7.4.2. Circuit diagram
Figure 7.7. Circuit diagram for the KS0108 GLCD with pinout A connected to the Arduino Mega
7.4.3. Connecting everything up
Table 7.9. KS0108 GLCD pinouts and connections to the Arduino
7.4.4. Sketch for drawing to a GLCD
Listing 7.3. Drawing to the KS0108 GLCD
7.4.5. Upload and test
7.5. Summary
Chapter 8. Communications
8.1. Ethernet
Table 8.1. Key Ethernet terms and concepts
8.1.1. The Ethernet library
Table 8.2. Overview of the Ethernet, Server, and Client class functions in the Ethernet library
Table 8.3. Overview of main UDP class functions in the Ethernet library
8.1.2. Ethernet Shield with SD data card
Note
8.2. Arduino web server
Figure 8.1. Overview of Arduino web server communication
8.2.1. Setting up the server
8.2.2. Sketch for creating a web server
Listing 8.1. Arduino web server
8.2.3. Upload and test
8.2.4. Troubleshooting
8.3. Tweet tweet: talking to Twitter
8.3.1. Of Twitter and tokens
8.3.2. Libraries and functions
Table 8.4. Overview of the Twitter library’s functions
8.3.3. Circuit diagram and connecting the hardware
Figure 8.2. Simple button-tweeting circuit
8.3.4. Sketch for the Twitter button-press tweeter
Listing 8.2. Twitter button-press tweeter
8.3.5. Upload and test
Figure 8.3. Screenshot of a Twitter button tweet
Note
8.4. Wi-Fi
Note
8.4.1. Arduino Wifi Shield
Using your Wifi Shield with Older Arduino Boards?
Figure 8.4. Overview of pins used by Wifi Shield.
8.4.2. WiFi library and functions
Table 8.5. Overview of the WiFi, WiFiServer, and WiFiClient library functions
8.4.3. Gestures: wireless accelerometers
8.4.4. Connecting the hardware
Figure 8.5. Connecting the ADXL335 analog accelerometer to the Arduino
8.4.5. Sketch for Bluetooth communication
Listing 8.3. Arduino accelerometer client
Listing 8.4. Processing sketch to request accelerometer data from Arduino server
8.4.6. Upload and test
8.5. Bluetooth wireless
Figure 8.6. Screenshot showing how your computer’s Bluetooth chip acts as a serial device
8.5.1. ArduinoBT
Figure 8.7. ArduinoBT, a Bluetoothenabled Arduino board
Note
8.5.2. Adding Bluetooth
Figure 8.8. Connecting the SparkFun BlueSMiRF Silver to an Arduino
Note
8.5.3. Establishing a Bluetooth connection
8.5.4. Sketch for Bluetooth communication
Listing 8.5. Bluetooth test sketch
Note
8.6. Serial peripheral interface (SPI)
Figure 8.9. SPI communication channels
Table 8.6. Arduino SPI connection overview
8.6.1. SPI library
Table 8.7. Arduino SPI library functions
8.6.2. SPI devices and digital potentiometers
8.6.3. Circuit diagram and connecting the hardware
Figure 8.10. Four LEDs controlled by the AD5206 digital potentiometer
8.6.4. Sketch for a digital LED dimmer
Listing 8.6. SPI digital potentiometer LED dimmer
8.7. Data logging
8.7.1. Types of memory
8.7.2. SD cards and SD library
Table 8.8. SD library SD class functions
Table 8.9. SD library File class functions
8.7.3. Sketch for an SD card sensor logger
Listing 8.7. SD card data logger
8.8. Cosm
8.8.1. Sign up for an account and get an API key
Figure 8.11. Main Cosm user interface
8.8.2. Creating a new data feed
Figure 8.12. Creating a new Cosm feed to log sensor data
Note
8.8.3. Sketch for Cosm sensor logging
Note
Listing 8.8. Cosm sensor logging code
8.8.4. Upload and test
8.9. Summary
Chapter 9. Game on
9.1. Nintendo Wii salutes you
Figure 9.1. Wii Nunchuk
9.1.1. Wii Nunchuk
Figure 9.2. A self-balancing motorized skateboard built by John Dingley, UK
Three-axis Accelerometer
Figure 9.3. Angular rotation of three-axis accelerometer
Joystick
Figure 9.4. Range of motion of Nunchuk joystick
Buttons
Note
9.1.2. Nunchuk connections
Table 9.1. Pin designations for the standard Arduino and the Mega for I2C
Table 9.2. Color designations for wires in the Wii Nunchuk
Note
Note
Figure 9.5. Nunchuk end connector
Figure 9.6. WiiChuck designed by Tod E. Kurt
Figure 9.7. NunChucky breakout board from Solarbotics
9.1.3. Wii will talk
Code to Communicate with the Nunchuk
Table 9.3. Byte data returned from Nunchuk
Code to Set Up and Power the Nunchuk
Note
Code for the Main Loop
Code to Print the Output
Code for send-request function
The Complete Sketch
Listing 9.1. Sketch to communicate with Nunchuk using I2C
9.1.4. Wii will test
Figure 9.8. Typical output from Nunchuk to serial monitor
9.2. Release the Xbox
Figure 9.9. Xbox 360 game controller
Note
9.2.1. Getting connected
Figure 9.10. Version 2.0 of the USB Host Shield from circuitsathome.com
9.2.2. USB Host library
The USB Protocol
9.2.3. Learning about the Xbox controller using the USB Host Shield
Figure 9.11. Select the USB_desc example sketch
Note
Figure 9.12. Device descriptor and configuration descriptor
Figure 9.13. Description of interface 00 for the Xbox controller
Note
9.2.4. Xbox reporting for duty
Table 9.4. Xbox input report for interface 00
9.2.5. Let’s boot it
9.2.6. Interfacing with code
Note
Code for Xboxhidboot.h
Code for Xboxhidboot.cpp
Listing 9.2. Xboxhidboot.cpp
9.2.7. Xboxhid.ino
Listing 9.3. Xboxhid.ino complete listing
9.2.8. Hardware connections and testing
Figure 9.14. Xbox controller connected to USB Host Shield and Arduino
Figure 9.15. Typical output from the Xbox controller
9.3. Summary
Chapter 10. Integrating the Arduino with iOS
10.1. Connecting your device to the Arduino
10.1.1. The Redpark serial cable
Figure 10.1. The Redpark Product Development serial cable for use with older iOS devices
Figure 10.2. Pinout of male RS232 DB-9 connector
Table 10.1. Pinouts of RS232 DB-9 male connector
Figure 10.3. P4B TTL to RS232 adapter
10.1.2. The final connection
Note
Figure 10.4. P4B TTL to RS232 adapter connected to the Arduino
10.2. iOS code
10.2.1. Creating a single-view application in Xcode
Figure 10.5. Select Single View Application
Note
Figure 10.6. Complete the project details.
Figure 10.7. MainStoryboard_iPhone.storyboard view
Figure 10.8. Switch object dragged onto the viewer with its state set to Off
Figure 10.9. Name the outlet toggleSwitch.
Figure 10.10. Create an action and name it toggleLED.
Figure 10.11. Import the Redpark serial SDK files.
Figure 10.12. Add the external accessory framework to the project.
10.2.2. Writing the code
Listing 10.1. ViewController.h
Listing 10.2. ViewController.m
Figure 10.13. Declaring support for the Redpark serial cable
10.3. The Arduino gets involved
10.3.1. Sketch to switch LED from iOS device
Listing 10.3. Switching LED from iOS device
Note
10.3.2. Testing the sketch
Note
Figure 10.14. iPhone connected to Arduino, switching LED on and off
10.4. Doing more with Xcode
10.4.1. Adding a Slider control
Figure 10.15. Adding a Slider control to the iPhone storyboard
Figure 10.16. Add the Tag value 13 to the Switch.
Figure 10.17. Add the moveSlider outlet.
Figure 10.18. Add the brightnessLED action.
Listing 10.4. ViewController.h
Listing 10.5. ViewController.m
10.5. Arduino sliding
Listing 10.6. Sketch for iOS Slider control
10.5.1. Arduino slider circuit
Figure 10.19. LED connected to pin 9 on the Arduino
10.5.2. Testing the circuit
Figure 10.20. Complete setup: iPhone controlling LED’s brightness
10.6. Moving data to the iOS device
10.6.1. Xcode coding
Figure 10.21. Labels added to the view
Figure 10.22. Adding the distance outlet
Listing 10.7. ViewController.h
Listing 10.8. ViewController.m
10.6.2. The GP2D12 IR distance sensor
Figure 10.23. GP2D12 infrared sensor added to circuit
Listing 10.9. Sketch to read distance from GP2D12 sensor
Note
10.6.3. Testing
Figure 10.24. The completed circuit with GP2D12 sensor connected to Arduino and iPhone
Figure 10.25. The complete IOSArduino app running on an iPhone
10.7. Summary
Chapter 11. Making wearables
11.1. Introducing the LilyPad
Figure 11.1. The pins of the LilyPad
Figure 11.2. Connecting the SparkFun FTDI breakout board to the LilyPad for programming
Figure 11.3. The LilyPad Simple
11.1.1. LilyPad accessories
Figure 11.4. LilyPad Temperature Sensor and LilyPad Vibe Board from SparkFun Electronics
Figure 11.5. Two different LilyPad power boards: the AAA Battery Holder, and the LiPo Holder, which allows you to connect a lithium polymer battery
11.1.2. Conductive thread and fabric
Figure 11.6. Conductive ribbon
Table 11.1. Types of conductive thread
Table 11.2. Types of conductive fabric
11.2. Creating a turn-signal jacket
Figure 11.7. Flex sensor
Figure 11.8. Connecting the LilyPad, LEDs, and flex sensors
Figure 11.9. Sewing the components into the jacket
Listing 11.1. TurnSignals.ino
11.3. Creating a wearable piano
Figure 11.10. Creating a simple soft button
Listing 11.2. WearablePiano.ino
Figure 11.11. Connecting the buttons and speaker to the LilyPad Arduino
11.4. The Arduino Pro Mini
Figure 11.12. The Arduino Pro Mini
11.5. Creating a smart headphone
Figure 11.13. The tiny QRE1113 IR-reflectance sensor
Listing 11.3. headphones.ino
Listing 11.4. win.py
Listing 11.5. osx.py
Figure 11.14. A Bluetooth Mate Silver transmitter
11.6. Creating a jacket with a compass
Figure 11.15. The HMC5883L magnetic compass
Figure 11.16. The SparkFun 7-segment serial display
Listing 11.6. compass.ino
11.7. Summary
Chapter 12. Adding shields
12.1. Shield basics
Figure 12.1. The Adafruit motor shield—the first motor shield we will be using in this chapter. Image from http://www.adafruit.com/products/81.
12.2. The Adafruit motor shield
12.2.1. The AFMotor library
12.2.2. Using the motor shield with a stepper motor
Figure 12.2. The connections for listing 12.1
Listing 12.1. MotorDriving.pde
12.2.3. Using the motor shield with a DC motor
Figure 12.3. Connecting a servomotor to the motor shield
Listing 12.2. PotToMotors.pde
12.2.4. Getting a motor shield
12.3. Creating your own shield
Figure 12.4. A project board, sometimes called a perfboard (source: SparkFun)
12.3.1. Memory
12.3.2. Level shifters
Figure 12.5. The pins for the 74HC4050
12.3.3. The SD card holder
Figure 12.6. An SD card holder that can be soldered into a project board
12.3.4. Connecting the SD card to the Arduino
Figure 12.7. The various connections for an SD card
Figure 12.8. Connecting the 74HC4050 to the Arduino and SD card holder
12.3.5. Preparing the perfboard
Figure 12.9. The two types of header pins: standard female header on the left, and Arduino offset header on the right
Figure 12.10. You can drill additional 0.8 mm holes into the perfboard if you don’t have prebent pins or don’t want to bend them yourself.
Figure 12.11. The SD card shield is ready to be connected to the Arduino board.
Figure 12.12. Connecting the level shifter to the SD card holder
12.3.6. Testing the shield
Listing 12.3. SDShieldWriter.pde
12.4. Summary
Chapter 13. Software integration
13.1. The serial channel
13.2. Servos for face tracking
Serial communication in Processing
13.2.1. Assembling the face-tracking hardware
Figure 13.1. The Lynx Pan and Tilt kit
Figure 13.2. Connecting the servomotors to the Arduino
13.2.2. Code for face-tracking
Listing 13.1. Face tracking using OpenCV in Arduino
Listing 13.2. Face tracking in Processing
13.3. Using Firmata to create an equalizer
13.3.1. Using Firmata in your application
13.3.2. Audio analysis in Processing
13.3.3. Assembling the equalizer hardware
Figure 13.3. Connecting the LEDs to create your Arduino equalizer
13.3.4. Code for the equalizer
Figure 13.4. Selecting the StandardFirmata program
Listing 13.3. Using Firmata in Processing
13.4. Using Pure Data to create a synthesizer
Figure 13.5. A Pd patch
Figure 13.6. The comport object
Figure 13.7. Connecting the comport to the devices object
13.4.1. Assembling the synthesizer hardware
Figure 13.8. Connecting potentiometers for the mixer
13.4.2. Code for the synthesizer
Figure 13.9. The Pd patch in the Pd IDE
Listing 13.4. Pd application as text (AIA13_4.pd)
Listing 13.5. Application to communicate with Pd
13.5. Using Python to monitor temperatures
13.5.1. The Serial library in Python
13.5.2. Assembling the thermometer hardware
Figure 13.10. Connecting the temperature sensors to the Arduino
13.5.3. Code for monitoring temperatures
Listing 13.6. Arduino application to send temperature data to Python
Listing 13.7. Python application to receive temperature data
13.6. Summary
Appendix A. Installing the Arduino IDE
A.1. Windows
Figure A.1. Extracting/copying the Arduino IDE and drivers to your local hard drive on Windows 7
A.1.1. Installing drivers for your board
Driver installation for the Arduino Uno
Figure A.2. Setting driver location search path for Arduino Uno driver installation on Windows 7
Driver installation for the Arduino Duemilanove, Nano, or Diecimila
Important
Figure A.3. Setting driver location for FTDI-based Arduino boards (such as the Duemilanove, Nano, and Diecimila) on Windows 7
A.2. Mac OS X
Figure A.4. Select the type of Arduino board
Figure A.5. Select your serial port
A.3. Linux
Figure A.6. Using the Synaptic Package Manager to install dependencies for Linux
Figure A.7. Marking OpenJDK for installation
Figure A.8. Libraries to be installed for OpenJDK
Appendix B. Coding primer
B.1. The Arduino language
Figure B.1. Additional functionality is added to the language by using libraries.
B.2. Variables
Figure B.2. Typical variables, considered as though they are held in named buckets
Listing B.1. Value of the variable pinLED
B.2.1 Variable types
Table B.1. Variable types
Note
B.2.2 Arrays
Note
B.2.3 Strings
B.2.4 Constants
B.2.5 Variable scope
Listing B.2. Global and local variables within a sketch
Figure B.3. The scope of variables varA, varB, and varC
B.3. Taking control
Table B.2. Relational operators
Note
Figure B.4. A simple task: upon entering a room, if it is dark, turn on the light.
B.3.1 If, else, else if
B.3.2 Switch case
Listing B.3. The switch-case statement
B.3.3 Logical operators
AND
OR
NOT
B.4. Going loopy
B.4.1. The for loop
Listing B.4. A for loop printing the value of i, from 0 to 99
Figure B.5. A for loop header showing initialization, test, and increment or decrement
B.4.2. The while loop
Listing B.5. A while loop that prints out the value of i from 0 to 99
Note
B.4.3. The do while loop
B.5. Functions
B.6. Summary
Appendix C. Libraries
C.1. Anatomy of a library
C.1.1. The .h (header) file
Listing C.1. Sabre.h
C.1.2. The .cpp file
Listing C.2. Sabre.cpp
Note
C.2. Using a library
C.2.1. Using a library in a sketch
Listing C.3. Sabre.ino
C.2.2. Distributing a library
Appendix D. Components list
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Appendix E. Useful links
Additional Arduino articles
Other useful links and materials
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
List of Figures
List of Tables
List of Listings
← Prev
Back
Next →
← Prev
Back
Next →