Arduino in Action.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
IN ACTION Martin Evans Joshua Noble Jordan Hochenbaum MANNING 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 Hello Arduino 3 1 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 Digital input and output 21 2 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 Simple projects: input and output 41 3 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 Extending Arduino 61 4 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 Arduino in motion 81 5 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 x CONTENTS 5.3 Stepper motors: one step at a time 94 Unipolar or bipolar 95 ■ Connecting the hardware 98 Stepper motor library functions 99 ■ Sketch to control a stepper motor 101 ■ Upload and test 101 5.4 Try not to get in a flap with servomotors 102 Controlling a servomotor 102 ■ Servomotor functions and methods 103 ■ Sketch to control a servomotor 104 Connecting the hardware 105 ■ Upload and test 105 5.5 Mighty power comes in small packages with brushless DC motors 106 Why go brushless 106 ■ Gaining control 107 ■ Sketch to control a brushless motor 108 ■ Connecting the hardware 109 Upload and test 110 ■ Reverse 110 ■ Sketch to reverse a brushless motor 110 ■ Connecting the hardware 111 Upload and test 111 5.6 The motor control shield for more motors 112 5.7 Summary 113 Object detection 114 6 6.1 Object detection with ultrasound 115 Choosing an ultrasonic sensor 115 ■ Three wires or four 116 Sketches for ultrasonic object finding 116 ■ Connecting the hardware 118 ■ Upload and test 118 6.2 Infrared for range finding 119 Infrared and ultrasound together 120 ■ The Sharp GP2D12 range finder 121 ■ Nonlinear algorithm for calculating distance 121 ■ Sketch for range finding 122 Connecting the hardware 123 ■ Upload and test 123 6.3 Passive infrared to detect movement 124 Using the Parallax PIR sensor 125 ■ Sketch for infrared motion detection 125 ■ Connecting the hardware 127 Upload and test 128 6.4 Summary 128 LCD displays 129 7 7.1 Introduction to LCDs 130 String variables: String type vs. char type 130 7.2 Parallel character LCDs: the Hitachi HD44780 133 4-bit or 8-bit? 133 ■ Library and functions 133 Circuit diagram 134 ■ Connecting everything up www.it-ebooks.info CONTENTS xi in 4-bit mode 135 ■ Sketch for writing to the Hitachi HD44780 137 ■ Upload and test 138 7.3 Serial LCD weather station 139 Serial vs. parallel LCDs 139 ■ SerLCD library and functions 139 ■ The Maxim IC DS18B20 temperature sensor 141 ■ OneWire and DallasTemperature libraries 141 ■ Circuit diagram 142 ■ Connecting everything up 143 ■ Sketch for an LCD weather station 144 Upload and test 145 7.4 Graphic LCDs: the Samsung KS0108 GLCD 146 Library and functions 146 ■ Circuit diagram 147 Connecting everything up 148 ■ Sketch for drawing to a GLCD 150 ■ Upload and test 151 7.5 Summary 151 Communications 152 8 8.1 Ethernet 153 The Ethernet library 154 ■ Ethernet Shield with SD data card 155 8.2 Arduino web server 156 Setting up the server 156 ■ Sketch for creating a web server 158 Upload and test 159 ■ Troubleshooting 159 8.3 Tweet tweet: talking to Twitter 159 Of Twitter and tokens 160 ■ Libraries and functions 160 Circuit diagram and connecting the hardware 161 ■ Sketch for the Twitter button-press tweeter 161 ■ Upload and test 163 8.4 Wi-Fi 163 Arduino Wifi Shield 164 ■ WiFi library and functions 165 Gestures: wireless accelerometers 167 ■ Connecting the hardware 168 ■ Sketch for Bluetooth communication 168 Upload and test 171 8.5 Bluetooth wireless 171 ArduinoBT 172 ■ Adding Bluetooth 172 Establishing a Bluetooth connection 173 Sketch for Bluetooth communication 174 8.6 Serial peripheral interface (SPI) 175 SPI library 176 ■ SPI devices and digital potentiometers 176 Circuit diagram and connecting the hardware 177 Sketch for a digital LED dimmer 178 www.it-ebooks.info xii CONTENTS 8.7 Data logging 179 Types of memory 180 ■ SD cards and SD library 180 Sketch for an SD card sensor logger 181 8.8 Cosm 182 Sign up for an account