IN ACTION Martin Evans Joshua Noble Jordan Hochenbaum M A N N I N G www.it-ebooks.info Arduino in Action www.it-ebooks.info www.it-ebooks.info Arduino in Action MARTIN EVANS JOSHUA NOBLE JORDAN HOCHENBAUM MANNING SHELTER ISLAND www.it-ebooks.info 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: [email protected] ©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. Manning Publications Co. Development editor: Cynthia Kane 20 Baldwin Road Copyeditor: Andy Carroll PO Box 261 Proofreader: Katie Tennant Shelter Island, NY 11964 Typesetter: Dennis Dalinnik Cover designer: Marija Tudor 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 www.it-ebooks.info brief contents PART 1 GETTING STARTED. ......................................................1 1 ■ Hello Arduino 3 2 ■ Digital input and output 21 3 ■ Simple projects: input and output 41 PART 2 PUTTING ARDUINO TO WORK......................................59 4 ■ Extending Arduino 61 5 ■ Arduino in motion 81 6 ■ Object detection 114 7 ■ LCD displays 129 8 ■ Communications 152 9 ■ Game on 188 10 ■ Integrating the Arduino with iOS 216 11 ■ Making wearables 244 12 ■ Adding shields 261 13 ■ Software integration 278 v www.it-ebooks.info www.it-ebooks.info contents preface xv acknowledgments xvii about this book xix about the cover illustration xxii PART 1 GETTING STARTED. ...........................................1 1 Hello Arduino 3 1.1 A brief history of the Arduino 4 1.2 The Arduino hardware 5 Arduino Uno 5 ■ Arduino Duemilanove 5 Arduino Ethernet 6 ■ Arduino Mega 6 Other Arduino boards 7 ■ Attack of the clones 8 Getting an Arduino 9 1.3 Setting up your working environment 10 Software for Arduino 10 ■ Basic hardware setup 10 Your Arduino toolbox 11 1.4 Make something happen! 11 Your first blinking LED 11 ■ Sketch to make an LED blink 12 Connecting everything 12 ■ Uploading and testing 13 vii www.it-ebooks.info viii CONTENTS 1.5 Touring the IDE 14 The main editor 14 ■ Serial monitor 15 ■ Catching errors 16 Process 16 1.6 Anatomy of a sketch 17 A routine called setup 17 ■ The endless loop 18 1.7 Commenting code 18 1.8 Summary 20 2 Digital input and output 21 2.1 Getting started 21 Using a breadboard 22 ■ Circuit diagram 22 Adding the LEDs 24 ■ Connecting the hardware 24 Sketch to flash five LEDs 25 ■ Upload and test 27 2.2 Gaining control 27 Circuit diagram 27 ■ Connections 28 Interrupts butting in 29 ■ Sketch to control the LEDs with a push button 30 ■ Upload and test 32 ■ Time for a break 32 Upload and test 33 2.3 Reaction tester 33 Circuit diagram 33 ■ Connections 33 ■ Sketch to test reaction speed 33 ■ Upload and test 36 2.4 Reactometer: Who really has the fastest reaction time? 37 Sketch to measure reaction speed 37 ■ Upload and test 38 2.5 Summary 39 3 Simple projects: input and output 41 3.1 Time to get analog 42 What’s the difference between analog and digital? 42 Reading a potentiometer 43 ■ Connecting the hardware 43 Sketch to read a potentiometer 44 ■ Upload and test 45 3.2 A piezoelectric transducer 46 The circuit diagram 47 ■ Connecting the hardware 48 Sketch to measure output from a piezoelectric transducer 49 Upload and test 51 ■ Circuit with added speaker 51 Connecting the hardware 51 ■ Sketch to generate a tone 53 Upload and test 53 www.it-ebooks.info CONTENTS ix 3.3 Making a pentatonic or five-tone keyboard 54 Circuit diagram 54 ■ Connecting the hardware 54 Sketch to create a pentatonic keyboard 56 ■ Upload and test 57 3.4 Summary 58 PART 2 PUTTING ARDUINO TO WORK ..........................59 4 Extending Arduino 61 4.1 Extending the Arduino with libraries 62 4.2 Core library 62 4.3 Standard libraries 63 Test-driven development with ArduinoTestSuite 63 Storing values using EEPROM 64 ■ Storing more data with SD 65 ■ Get connected with Ethernet 67 Serial communication with Firmata 68 ■ Displaying data using the LiquidCrystal library 69 ■ Controlling a servo motor 70 ■ Turning a stepper motor 70 Communicating with SPI peripherals 71 Communicating with the two-wire interface 72 Get more serial ports with SoftwareSerial 74 4.4 Contributed libraries 75 Installing a new library 76 4.5 Expanding the Arduino with shields 76 Common shields 77 ■ Gotchas: will it work with my Arduino? 80 4.6 Summary 80 5 Arduino in motion 81 5.1 Getting up to speed with DC motors 82 Stopping and starting 83 ■ Sketch to turn a small DC motor on and off 84 ■ Connecting the hardware 84 Upload and test 86 5.2 Speed control and reverse 87 PWM to the rescue 87 ■ The H-bridge for motor control 89 The L293D dual H driver 90 ■ Connecting the hardware 91 Sketch to control a motor with an L293D 92 Upload and test 93 ■ Changing motor speed 93 Upload and test 94 www.it-ebooks.info