Arduino in Action
()
About this ebook
Arduino in Action is a hands-on guide to prototyping and building electronics using the Arduino platform. Suitable for both beginners and advanced users, this easy-to-follow book begins with the basics and then systematically guides you through projects ranging from your first blinking LED through connecting Arduino to devices like game controllers or your iPhone.
About the Technology
Arduino is an open source do-it-yourself electronics platform that supports a mind-boggling collection of sensors and actuators you can use to build anything you can imagine. Even if you've never attempted a hardware project, this easy-to-follow book will guide you from your first blinking LED through connecting Arduino to your iPhone.
About this Book
Arduino in Action is a hands-on guide to prototyping and building DIY electronics. You'll start with the basics—unpacking your board and using a simple program to make something happen. Then, you'l attempt progressively more complex projects as you connect Arduino to motors, LCD displays, Wi-Fi, GPS, and Bluetooth. You'll explore input/output sensors, including ultrasound, infrared, and light, and then use them for tasks like robotic obstacle avoidance.
Arduino programs look a lot like C or C++, so some programming skill is helpful.
What's Inside
- Getting started with Arduino—no experience required!
- Writing programs for Arduino
- Sensing and responding to events
- Robots, flying vehicles, Twitter machines, LCD displays, and more!
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the Authors
Martin Evans is a professional developer, a lifelong electronics enthusiast, and the creator of an Arduino-based underwater ROV. Joshua Noble is an author and creative technologist who works with smart spaces. Jordan Hochenbaum uses Arduino to explore musical expression and creative interaction.
Table of Contents
-
Part 1 Getting started
- Chapter 1 Hello Arduino
- Chapter 2 Digital input and output
- Chapter 3 Simple projects: input and output Part 2 Putting Arduino to work
- Chapter 4 Extending Arduino
- Chapter 5 Arduino in motion
- Chapter 6 Object detection
- Chapter 7 LCD displays
- Chapter 8 Communications
- Chapter 9 Game on
- Chapter 10 Integrating the Arduino with iOS
- Chapter 11 Making wearables
- Chapter 12 Adding shields
- Chapter 13 Software integration
Jordan Hochenbaum
Jordan Hochenbaum is an artist whose work explores new interfaces for musical expression and creative interaction. His work can be found at www.flipmu.com.
Related to Arduino in Action
Related ebooks
Arduino: Building LED and Espionage Projects Rating: 0 out of 5 stars0 ratingsProgramming Arduino Next Steps: Going Further with Sketches, Second Edition Rating: 3 out of 5 stars3/5Dependency Injection: Design patterns using Spring and Guice Rating: 0 out of 5 stars0 ratingsA Novice Guide to Arduino Programming Rating: 4 out of 5 stars4/5Arduino Applied: Comprehensive Projects for Everyday Electronics Rating: 0 out of 5 stars0 ratingsArduino Robotic Projects Rating: 4 out of 5 stars4/5C Programming for the Pc the Mac and the Arduino Microcontroller System Rating: 0 out of 5 stars0 ratingsExploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5RabbitMQ in Action: Distributed Messaging for Everyone Rating: 4 out of 5 stars4/5Arduino Development Cookbook Rating: 4 out of 5 stars4/5JavaScript on Things: Hacking hardware for web developers Rating: 0 out of 5 stars0 ratingsiOS in Practice Rating: 0 out of 5 stars0 ratingsUno Easy Starter Project: LED Cube Arduino Uno Building and Coding Project 3 Beginners Edition Ebonygeek45 Rating: 5 out of 5 stars5/5Arduino Software Internals: A Complete Guide to How Your Arduino Language and Hardware Work Together Rating: 0 out of 5 stars0 ratingsArduino for the Cloud:: Arduino Yun and Dragino Yun Shield Rating: 0 out of 5 stars0 ratingsArduino Networking Rating: 4 out of 5 stars4/5Raspberry Pi Robotic Projects - Third Edition Rating: 0 out of 5 stars0 ratingsBanana Pi Cookbook Rating: 0 out of 5 stars0 ratingsBeginning Robotics with Raspberry Pi and Arduino: Using Python and OpenCV Rating: 0 out of 5 stars0 ratingsProgramming Arduino Next Steps: Going Further with Sketches Rating: 3 out of 5 stars3/5Arduino A Complete Guide Rating: 0 out of 5 stars0 ratingsArduino Step by Step: The Ultimate Beginner's Guide with Basics on Hardware, Software, Programming & DIY Projects Rating: 0 out of 5 stars0 ratingsARDUINO PROGRAMMING FOR BEGINNERS: Tips and Tricks for the Efficient Use of Arduino Programming Rating: 0 out of 5 stars0 ratingsArduino Made Simple: With Interactive Projects Rating: 0 out of 5 stars0 ratingsArduino Home Automation Projects Rating: 3 out of 5 stars3/5Raspberry Pi Computer Architecture Essentials Rating: 0 out of 5 stars0 ratingsRxJava for Android Developers Rating: 0 out of 5 stars0 ratingsArduino Sketches: Tools and Techniques for Programming Wizardry Rating: 4 out of 5 stars4/5Arduino BLINK Blueprints Rating: 0 out of 5 stars0 ratings
Hardware For You
Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Chip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5CompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5Linux All-In-One For Dummies Rating: 2 out of 5 stars2/5macOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5After Steve: How Apple Became a Trillion-Dollar Company and Lost Its Soul Rating: 4 out of 5 stars4/5Build Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Creative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5Electrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsRaspberry Pi Cookbook for Python Programmers Rating: 0 out of 5 stars0 ratingsUpgrading and Fixing Computers Do-it-Yourself For Dummies Rating: 4 out of 5 stars4/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Exploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsMacBook For Dummies Rating: 4 out of 5 stars4/5Dancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5Exploring Apple iPad: iPadOS 15 Edition: The Illustrated, Practical Guide to Using your iPad Rating: 0 out of 5 stars0 ratingsArduino for Secret Agents Rating: 4 out of 5 stars4/5iMac For Dummies Rating: 0 out of 5 stars0 ratingsLearning Android Forensics Rating: 4 out of 5 stars4/5Building Confidence Using Your iPhone: Book I — THE ULTIMATE GUIDE Rating: 0 out of 5 stars0 ratingsChromebook For Dummies Rating: 5 out of 5 stars5/5Apple Watch For Dummies Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Study Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 0 out of 5 stars0 ratingsTroubleshooting and Maintaining Your PC All-in-One For Dummies Rating: 5 out of 5 stars5/5
Reviews for Arduino in Action
0 ratings0 reviews
Book preview
Arduino in Action - Jordan Hochenbaum
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email:
©2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Photographs in this book were created by Martin Evans and Jordan Hochenbaum, unless otherwise noted. Illustrations were created by Martin Evans, Joshua Noble, and Jordan Hochenbaum. Fritzing (fritzing.org) was used to create some of the circuit diagrams.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
ISBN: 9781617290244
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 13
Brief Table of Contents
Copyright
Brief Table of Contents
Table of Contents
Preface
Acknowledgments
About this Book
About the Cover Illustration
1. Getting started
Chapter 1. Hello Arduino
Chapter 2. Digital input and output
Chapter 3. Simple projects: input and output
2. Putting Arduino to work
Chapter 4. Extending Arduino
Chapter 5. Arduino in motion
Chapter 6. Object detection
Chapter 7. LCD displays
Chapter 8. Communications
Chapter 9. Game on
Chapter 10. Integrating the Arduino with iOS
Chapter 11. Making wearables
Chapter 12. Adding shields
Chapter 13. Software integration
Appendix A. Installing the Arduino IDE
Appendix B. Coding primer
Appendix C. Libraries
Appendix D. Components list
Appendix E. Useful links
Index
List of Figures
List of Tables
List of Listings
Table of Contents
Copyright
Brief Table of Contents
Table of Contents
Preface
Acknowledgments
About this Book
About the Cover Illustration
1. Getting started
Chapter 1. Hello Arduino
1.1. A brief history of the Arduino
1.2. The Arduino hardware
1.2.1. Arduino Uno
1.2.2. Arduino Duemilanove
1.2.3. Arduino Ethernet
1.2.4. Arduino Mega
1.2.5. Other Arduino boards
1.2.6. Attack of the clones
1.2.7. Getting an Arduino
1.3. Setting up your working environment
1.3.1. Software for Arduino
1.3.2. Basic hardware setup
1.3.3. Your Arduino toolbox
1.4. Make something happen!
1.4.1. Your first blinking LED
1.4.2. Sketch to make an LED blink
1.4.3. Connecting everything
1.4.4. Uploading and testing
1.5. Touring the IDE
1.5.1. The main editor
1.5.2. Serial monitor
1.5.3. Catching errors
1.5.4. Process
1.6. Anatomy of a sketch
1.6.1. A routine called setup
1.6.2. The endless loop
1.7. Commenting code
1.8. Summary
Chapter 2. Digital input and output
2.1. Getting started
2.1.1. Using a breadboard
2.1.2. Circuit diagram
2.1.3. Adding the LEDs
2.1.4. Connecting the hardware
2.1.5. Sketch to flash five LEDs
2.1.6. Upload and test
2.2. Gaining control
2.2.1. Circuit diagram
2.2.2. Connections
2.2.3. Interrupts butting in
2.2.4. Sketch to control the LEDs with a push button
2.2.5. Upload and test
2.2.6. Time for a break
2.2.7. Upload and test
2.3. Reaction tester
2.3.1. Circuit diagram
2.3.2. Connections
2.3.3. Sketch to test reaction speed
2.3.4. Upload and test
2.4. Reactometer: Who really has the fastest reaction time?
2.4.1. Sketch to measure reaction speed
2.4.2. Upload and test
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?
3.1.2. Reading a potentiometer
3.1.3. Connecting the hardware
3.1.4. Sketch to read a potentiometer
3.1.5. Upload and test
3.2. A piezoelectric transducer
3.2.1. The circuit diagram
3.2.2. Connecting the hardware
3.2.3. Sketch to measure output from a piezoelectric transducer
3.2.4. Upload and test
3.2.5. Circuit with added speaker
3.2.6. Connecting the hardware
3.2.7. Sketch to generate a tone
3.2.8. Upload and test
3.3. Making a pentatonic or five-tone keyboard
3.3.1. Circuit diagram
3.3.2. Connecting the hardware
3.3.3. Sketch to create a pentatonic keyboard
3.3.4. Upload and test
3.4. Summary
2. Putting Arduino to work
Chapter 4. Extending Arduino
4.1. Extending the Arduino with libraries
4.2. Core library
4.3. Standard libraries
4.3.1. Test-driven development with ArduinoTestSuite
4.3.2. Storing values using EEPROM
4.3.3. Storing more data with SD
4.3.4. Get connected with Ethernet
4.3.5. Serial communication with Firmata
4.3.6. Displaying data using the LiquidCrystal library
4.3.7. Controlling a servo motor
4.3.8. Turning a stepper motor
4.3.9. Communicating with SPI peripherals
4.3.10. Communicating with the two-wire interface
4.3.11. Get more serial ports with SoftwareSerial
4.4. Contributed libraries
4.4.1. Installing a new library
4.5. Expanding the Arduino with shields
4.5.1. Common shields
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
5.1.1. Stopping and starting
5.1.2. Sketch to turn a small DC motor on and off
5.1.3. Connecting the hardware
5.1.4. Upload and test
5.2. Speed control and reverse
5.2.1. PWM to the rescue
5.2.2. The H-bridge for motor control
5.2.3. The L293D dual H driver
5.2.4. Connecting the hardware
5.2.5. Sketch to control a motor with an L293D
5.2.6. Upload and test
5.2.7. Changing motor speed
5.2.8. Upload and test
5.3. Stepper motors: one step at a time
5.3.1. Unipolar or bipolar
5.3.2. Connecting the hardware
5.3.3. Stepper motor library functions
5.3.4. Sketch to control a stepper motor
5.3.5. Upload and test
5.4. Try not to get in a flap with servomotors
5.4.1. Controlling a servomotor
5.4.2. Servomotor functions and methods
5.4.3. Sketch to control a servomotor
5.4.4. Connecting the hardware
5.4.5. Upload and test
5.5. Mighty power comes in small packages with brushless DC motors
5.5.1. Why go brushless
5.5.2. Gaining control
5.5.3. Sketch to control a brushless motor
5.5.4. Connecting the hardware
5.5.5. Upload and test
5.5.6. Reverse
5.5.7. Sketch to reverse a brushless motor
5.5.8. Connecting the hardware
5.5.9. Upload and test
5.6. The motor control shield for more motors
5.7. Summary
Chapter 6. Object detection
6.1. Object detection with ultrasound
6.1.1. Choosing an ultrasonic sensor
6.1.2. Three wires or four
6.1.3. Sketches for ultrasonic object finding
6.1.4. Connecting the hardware
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
6.2.3. Nonlinear algorithm for calculating distance
6.2.4. Sketch for range finding
6.2.5. Connecting the hardware
6.2.6. Upload and test
6.3. Passive infrared to detect movement
6.3.1. Using the Parallax PIR sensor
6.3.2. Sketch for infrared motion detection
6.3.3. Connecting the hardware
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
7.2. Parallel character LCDs: the Hitachi HD44780
7.2.1. 4-bit or 8-bit?
7.2.2. Library and functions
7.2.3. Circuit diagram
7.2.4. Connecting everything up in 4-bit mode
7.2.5. Sketch for writing to the Hitachi HD44780
7.2.6. Upload and test
7.3. Serial LCD weather station
7.3.1. Serial vs. parallel LCDs
7.3.2. SerLCD library and functions
7.3.3. The Maxim IC DS18B20 temperature sensor
7.3.4. OneWire and DallasTemperature libraries
7.3.5. Circuit diagram
7.3.6. Connecting everything up
7.3.7. Sketch for an LCD weather station
7.3.8. Upload and test
7.4. Graphic LCDs: the Samsung KS0108 GLCD
7.4.1. Library and functions
7.4.2. Circuit diagram
7.4.3. Connecting everything up
7.4.4. Sketch for drawing to a GLCD
7.4.5. Upload and test
7.5. Summary
Chapter 8. Communications
8.1. Ethernet
8.1.1. The Ethernet library
8.1.2. Ethernet Shield with SD data card
8.2. Arduino web server
8.2.1. Setting up the server
8.2.2. Sketch for creating a 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
8.3.3. Circuit diagram and connecting the hardware
8.3.4. Sketch for the Twitter button-press tweeter
8.3.5. Upload and test
8.4. Wi-Fi
8.4.1. Arduino Wifi Shield
8.4.2. WiFi library and functions
8.4.3. Gestures: wireless accelerometers
8.4.4. Connecting the hardware
8.4.5. Sketch for Bluetooth communication
8.4.6. Upload and test
8.5. Bluetooth wireless
8.5.1. ArduinoBT
8.5.2. Adding Bluetooth
8.5.3. Establishing a Bluetooth connection
8.5.4. Sketch for Bluetooth communication
8.6. Serial peripheral interface (SPI)
8.6.1. SPI library
8.6.2. SPI devices and digital potentiometers
8.6.3. Circuit diagram and connecting the hardware
8.6.4. Sketch for a digital LED dimmer
8.7. Data logging
8.7.1. Types of memory
8.7.2. SD cards and SD library
8.7.3. Sketch for an SD card sensor logger
8.8. Cosm
8.8.1. Sign up for an account and get an API key
8.8.2. Creating a new data feed
8.8.3. Sketch for Cosm sensor logging
8.8.4. Upload and test
8.9. Summary
Chapter 9. Game on
9.1. Nintendo Wii salutes you
9.1.1. Wii Nunchuk
9.1.2. Nunchuk connections
9.1.3. Wii will talk
9.1.4. Wii will test
9.2. Release the Xbox
9.2.1. Getting connected
9.2.2. USB Host library
9.2.3. Learning about the Xbox controller using the USB Host Shield
9.2.4. Xbox reporting for duty
9.2.5. Let’s boot it
9.2.6. Interfacing with code
9.2.7. Xboxhid.ino
9.2.8. Hardware connections and testing
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
10.1.2. The final connection
10.2. iOS code
10.2.1. Creating a single-view application in Xcode
10.2.2. Writing the code
10.3. The Arduino gets involved
10.3.1. Sketch to switch LED from iOS device
10.3.2. Testing the sketch
10.4. Doing more with Xcode
10.4.1. Adding a Slider control
10.5. Arduino sliding
10.5.1. Arduino slider circuit
10.5.2. Testing the circuit
10.6. Moving data to the iOS device
10.6.1. Xcode coding
10.6.2. The GP2D12 IR distance sensor
10.6.3. Testing
10.7. Summary
Chapter 11. Making wearables
11.1. Introducing the LilyPad
11.1.1. LilyPad accessories
11.1.2. Conductive thread and fabric
11.2. Creating a turn-signal jacket
11.3. Creating a wearable piano
11.4. The Arduino Pro Mini
11.5. Creating a smart headphone
11.6. Creating a jacket with a compass
11.7. Summary
Chapter 12. Adding shields
12.1. Shield basics
12.2. The Adafruit motor shield
12.2.1. The AFMotor library
12.2.2. Using the motor shield with a stepper motor
12.2.3. Using the motor shield with a DC motor
12.2.4. Getting a motor shield
12.3. Creating your own shield
12.3.1. Memory
12.3.2. Level shifters
12.3.3. The SD card holder
12.3.4. Connecting the SD card to the Arduino
12.3.5. Preparing the perfboard
12.3.6. Testing the shield
12.4. Summary
Chapter 13. Software integration
13.1. The serial channel
13.2. Servos for face tracking
Serial communication in Processing
13.2.2. Code for face-tracking
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
13.3.4. Code for the equalizer
13.4. Using Pure Data to create a synthesizer
13.4.1. Assembling the synthesizer hardware
13.4.2. Code for the synthesizer
13.5. Using Python to monitor temperatures
13.5.1. The Serial library in Python
13.5.2. Assembling the thermometer hardware
13.5.3. Code for monitoring temperatures
13.6. Summary
Appendix A. Installing the Arduino IDE
A.1. Windows
A.1.1. Installing drivers for your board
A.2. Mac OS X
A.3. Linux
Appendix B. Coding primer
B.1. The Arduino language
B.2. Variables
B.2.1 Variable types
B.2.2 Arrays
B.2.3 Strings
B.2.4 Constants
B.2.5 Variable scope
B.3. Taking control
B.3.1 If, else, else if
B.3.2 Switch case
B.3.3 Logical operators
B.4. Going loopy
B.4.1. The for loop
B.4.2. The while loop
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
C.1.2. The .cpp file
C.2. Using a library
C.2.1. Using a library in a sketch
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
List of Figures
List of Tables
List of Listings
Preface
My Arduino journey started after watching Elise Huard present her talk, The internet of things,
at Rails Underground in the summer of 2009. Following the conference, I immediately purchased a copy of Massimo Banzi’s Getting Started with Arduino (O’Reilly, 2008), which I read from cover to cover on the train back to where I was staying.
Shortly afterwards, I purchased my first Arduino and started playing, experimenting, and building small projects. My first major project was an obstacle-avoidance robot, which I presented at the 2010 Scottish Ruby conference in Edinburgh, Scotland.
I’ve had a lifelong interest in underwater vehicles and the marine environment, and following the conference I started work on an Arduino-controlled underwater remote-operated vehicle (ROV), which I duly presented at the 2011 Scottish Ruby conference.
Since then, I’ve toured the UK and Ireland displaying my ROV at a number of Maker Faires, where it has generated much interested and discussion.
I’m one of the founding members of Aberduino, a hack space based in Aberdeen, Scotland, where we produce installations for various events.
Other Arduino-based projects I’ve worked on include the development of a medical training aid and helping with the Wikispeed project, an open source car.
I continue to work with underwater vehicles and am actively developing a new Arduino-based underwater ROV that can be distributed as a kit.
MARTIN EVANS
I first started working with microcontrollers with the same introduction that a lot of artists and designers had ten years ago: PIC controllers. I found them difficult to understand, finicky, slow to build with, and yet they were the only option. Later I discovered Teleo controllers and then Wiring boards, but when the Arduino arrived in my world, I was hooked.
I’ve used Arduinos for everything from prototyping smart spray-paint cans to building interactive exhibits for museums to creating tools for science experiments. I’m in love with the boards, the environment, and, most especially, the community that has grown up around the Arduino and that’s so willing to teach, experiment, explore, and share.
JOSHUA NOBLE
My interest in music technology led me to discover the Arduino as a platform for rapid development and physical computing sometime around 2008. I was originally introduced to the Arduino as a tool for designing musical interfaces for live performance. This led to the Arduinome project, an open source port of the popular Monome USB MIDI controller, which I worked on with longtime collaborator Owen Vallis. The success of the Arduinome project was a true testament to the uniqueness of the Arduino itself—a device that empowers musicians and artists of all technical backgrounds to create unique and powerful tools for expression. Around the same time, I was taking a course in musical robotics and kinetic sculpture, and we used the Arduino to drive a collaborative musical robotic instrument.
Since then, the Arduino has been at the heart of my work. In 2009 I began pursuing my PhD, which investigated the affordances of multimodal sensor systems for musical performance and pedagogy. Using the Arduino, I’ve built numerous interfaces and hyperinstruments for capturing data and metrics from musical performance. I built the SmartFiducial, which added z-depth (in-air proximity) and pressure sensing to tangible tabletop surfaces. Embedding multimodal sensing systems within instruments or placing them on human performers, I’ve investigated a wide variety of machine learning tasks, such as performer recognition and drum-hand recognition. I completed my PhD and became a professor in Music Technology: Interaction, Intelligence, and Design at California Institute of the Arts in 2012, and the Arduino continues to be an important part of my artistic and academic practice. My work with the Arduino has been featured online and in print, including in WIRED and Computer Arts magazine, and my current Arduino-based projects range from kinetic surfaces for live projection mapping and visuals to wireless sensing systems for interactive dance performance.
JORDAN HOCHENBAUM
Acknowledgments
We would like to thank the following people at Manning: Sebastian Stirling for his endless patience and support; Cynthia Kane for guiding us and giving gentle prods over the final review stages to bring the manuscript to publication; Troy Mott who handled the preproduction stages; technical editors Sharon Cichelli and Daniel Soltis who offered help and advice on how to improve the final manuscript; and copyeditor Andy Carroll who carefully combed through the manuscript, removing unnecessary words and tidying everything up.
We also want to thank our reviewers who helped clarify parts of the book that needed further explanation and who pointed out inconsistencies. Thanks to Alan Burlison, Andrew Davidson, Bill Westfield, Daniel Soltis, George Entenman, Howard R. Hansen, Jeroen Benckhuijsen, John Raines, Margriet Bruggeman, Matt Scarpino, Nikander Bruggeman, P. David Pull, Philipp K. Janert, Scott Couprie, Scott Howard, Steve Prior, and Ursin Stauss.
MARTIN EVANS would like to thank his wife Henrietta and children Leanne, Heather, and Luke, who all in one way or another encouraged him to keep on working on this book. He would also like to thank Paul and the team at Symposium Coffee House, Peterhead, who kept him fueled with coffee when most needed.
JOSHUA NOBLE would like to acknowledge a huge debt of gratitude to Simona Maschi, David Gauthier, and everyone at CIID who let him slack off a little on his thesis project so he could finish his chapters for this book, his lovely girlfriend Rachel Buker, and of course the man who originally taught him to program in his first halting steps, Morgan Schwartz.
JORDAN HOCHENBAUM would like acknowledge his friend and mentor Ajay Kapur for introducing him to the Arduino and to systematically thinking about musical interface design. He’d also like to thank longtime friend and collaborator Owen Vallis for his help as they stumbled through their first Arduino sketches together and delved deeper into the world of the AVR.
About this Book
This book is organized into two parts. Part 1 discusses the Arduino in general and includes a tutorial that introduces you to your first project before looking at a couple of simple projects that use the Arduino inputs and outputs. Part 2 looks at the Arduino in more depth, and this is where we really start to put the Arduino to work with a number of advanced techniques that you can use in your own projects.
Code for the sketches covered in each chapter is available online via the book’s website: www.manning.com/ArduinoinAction. We suggest trying to follow along with the projects in the book as much as you can. Typing in the individual code listings will help to fix concepts and ideas into your mind.
This book is suitable for both beginners and intermediate Arduino users. It starts from a very basic level and assumes no prior knowledge, but we think even expert users will gain things from the second part of the book, which covers a wide variety of subjects, many of which can be combined into your own projects. A basic understanding of electronics will help with some project circuits, although we endeavor to explain them as much as we can.
Roadmap
Part 1 of the book discusses the Arduino in general.
Chapter 1 explains how to get started by setting up your development environment and a basic software and hardware toolbox. It shows you how to blink your first LED and walks you through the anatomy of an Arduino sketch.
Chapter 2 takes the form of a tutorial that introduces your first project and covers a number of key concepts.
Chapter 3 builds on the knowledge gained in chapter 2 by looking at a couple of simple projects that use the Arduino inputs and outputs.
Part 2 of the book looks at the Arduino in more depth. This is where we put the Arduino to work.
Chapter 4 covers software libraries that extend the Arduino’s functionality.
Chapter 5 gets the Arduino into motion by showing how an Arduino can be used to control a range of motors.
Object detection is covered in chapter 6 with a section on how ultrasound and ultrasonic sensors can be interfaced.
Chapter 7 is all about outputting data to LCD displays. It covers communication with the Hitachi HD44780 parallel LCD as well as the KS0108 graphic LCD that can also display graphics.
In chapter 8 we cover communication with the external world. We start by using an Ethernet Shield to create a web server and then move on to tweeting messages from an Arduino to Twitter, using a Wi-Fi network and Bluetooth communication, logging data to an SD card and the internet using the Cosm service, and communicating with other devices over the serial peripheral interface (SPI).
Chapter 9 details connecting an Arduino to game controllers, starting with the widely available Wii Nunchuk over I2C. Then we take a detailed look at using a USB shield to interface with a USB Xbox controller.
Chapter 10 covers integration with iOS devices like the iPhone and iPad using the Redpark serial cable.
In chapter 11 we look at two alternative forms of the Arduino that can be used as wearables: the LilyPad that can be sewn into clothing, and the Arduino Mini Pro, which is a special customized version of the Arduino notable for its small size.
Chapter 12 looks at shields, which provide a simple method of extending or enhancing the Arduino hardware. This chapter includes instructions for creating your own shields.
Finally, chapter 13 is on software integration, and it covers communicating with the Arduino from other software programs.
There are also several appendices.
Appendix A is about installing the Arduino software on Windows, Mac OS X, and Linux operating systems.
Appendix B is a coding primer for the Arduino language.
Appendix C is about Arduino software libraries and their structure.
Appendix D provides a listing of all the components required to complete the individual projects in each chapter.
Appendix E is a list of useful links.
Code conventions and downloads
There are many code examples in this book, edited using the Arduino integrated development environment (IDE). Source code in listings and text is in a fixed-width font like this, to separate it from ordinary text, and code annotations accompany many of the listings.
You’ll find the source code for the examples in this book available from the publisher’s website at www.manning.com/ArduinoinAction.
Author Online
The purchase of Arduino in Action includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/ArduinoinAction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
About the Cover Illustration
The figure on the cover of Arduino in Action is captioned Travailleur de déplacement,
which means an itinerant laborer. The illustration is taken from a 19th-century edition of Sylvain Maréchal’s four-volume compendium of regional dress customs published in France. Each illustration is finely drawn and colored by hand. The rich variety of Maréchal’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.
Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns or regions. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.
At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Maréchal’s pictures.
Part 1. Getting started
Part 1 of this book (chapters 1 to 3) is a discussion of the Arduino in general. You’ll start by learning your way around the Arduino and its development environment and completing a tutorial that introduces you to your first project. Then you’ll look at a couple of simple projects that use the Arduino inputs and outputs.
Chapter 1. Hello Arduino
This chapter covers
The history of the Arduino
Arduino hardware
Hardware and software setup
The first blinking LED
What can the Arduino be used for? The answers are surprisingly diverse. The Arduino has been used in a wide variety of projects:
Video games such as Pong and Space Invaders that will remind some readers of their childhood and introduce others to the games their parents played when they were young, complete with monochrome graphics and simple sound effects
Line-following robots that introduce robotics principles but are also used in factories and warehouses to deliver components along predetermined paths
Light harps that produce music with a wave of your hands, as used internationally by the performer Little Boots
MIDI controllers that control a series of instruments
Self-balancing robots that mimic the Segway
These are all examples of projects built using the Arduino, a microcontroller so small that it fits in the palm of your hand. Originally designed to be used as a tool for physical computing projects by design and art students, the Arduino has been adopted as the tool of choice by communities of tinkerers and makers interested in building and prototyping their own projects.
In this chapter, we’ll start with a look at the history of Arduino and how it became the tool that many makers reach for when starting a new project. This background includes its origins at the Interaction Design Institute Ivrea and explains why it was so desperately needed. We’ll then review the different types of Arduinos available and the advantages and disadvantages of each. We’ll also look at what you need to get started: tools, equipment, and suggested electronic components. Finally, we’ll round this opening chapter out with a look at the Arduino integrated development environment (IDE) before making our first project: an LED that blinks on and off.
Let’s start by learning where the Arduino comes from.
1.1. A brief history of the Arduino
The Arduino got its start at the Interaction Design Institute in the city of Ivrea, Italy, in 2005. Professor Massimo Banzi was looking for a low-cost way to make it easier for the design students there to work with technology. He discussed his problem with David Cuartielles, a researcher visiting from Malmö University in Sweden who was looking for a similar solution, and Arduino was born.
Existing products on the market were expensive and relatively difficult to use. Banzi and Cuartielles decided to make a microcontroller that could be used by their art and design students in their projects. The main requirements were that it be inexpensive—the target price was to be no more than a student would spend going out for a pizza—and be a platform that anyone could use. David Cuartielles designed the board, and a student of Massimo’s, David Mellis, programmed the software to run the board. Massimo contacted a local engineer, Gianluca Martino, who also worked at the Design Institute helping students with their projects. Gianluca agreed to produce an initial run of 200 boards.
The new board was named Arduino after a local bar frequented by faculty members and students from the institute. The boards were sold in kit form for students to build themselves. The initial run was soon sold out, and more were produced to keep up with demand. Designers and artists from other areas heard about the Arduino and wanted to use it in their projects. Its popularity soon grew when the wider maker audience realized that the Arduino was an easy-to-use, low-cost system that could be used in their own projects, as well as a great introduction to programming microcontrollers. The original design was improved upon and new versions were introduced. Sales of official Arduinos have now reached