www.elsolucionario.org LEARN THE BASICS, BUILD THE PROJECTS, The Arduino is a cheap, flexible, open source microcontroller platform designed to make it easy for hobbyists to use electronics in homemade projects With an almost unlimited range of input and output add-ons, sensors, indicators, displays, motors, and more, the Arduino offers you countless ways to create devices that interact with the world around you In Arduino Workshop, you’ll learn how these add-ons work and how to integrate them into your own projects You’ll start off with an overview of the Arduino system but quickly move on to coverage of various electronic components and concepts Hands-on projects throughout the book reinforce what you’ve learned and show you how to apply that knowledge As your understanding grows, the projects increase in complexity and sophistication Among the book’s 65 projects are useful devices like: • A handy tester that lets you check the voltage of any single-cell battery • A keypad-controlled lock that requires a secret code to open You’ll also learn to build Arduino toys and games like: • An electronic version of the classic six-sided die • A binary quiz game that challenges your number conversion skills • A motorized remote control tank with collision detection to keep it from crashing Arduino Workshop will teach you the tricks and design principles of a master craftsman Whatever your skill level, you’ll have fun as you learn to harness the power of the Arduino for your own DIY projects • A digital thermometer that charts temperature changes on an LCD ABOUT THE AUTHOR • A GPS logger that records data from your travels, which can be displayed on Google Maps John Boxall (http://www.tronixstuff.com/ ) has been writing Arduino tutorials, projects, and kit and accessory reviews for years Arduino Workshop is his first book ARDUINO WORKSHOP CREATE YOUR OWN T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™ w w w.nostarch.com $29.95 ($31.95 CDN) SHELVE IN: HARDWARE/ELECTRONICS BOXALL “ I L I E F L AT ” This book uses RepKover — a durable binding that won’t snap shut ARDUINO WORKSHOP A HANDS-ON INTRODUCTION W I T H 65 PROJECTS JOHN BOXALL Arduino Workshop www.elsolucionario.org A r d ui n o W or k s h o p A Hands-On Introduction with 65 Projects by John Boxall San Francisco Arduino Workshop Copyright © 2013 by John Boxall All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher Printed in USA First printing 17 16 15 14 13 ISBN-10: 1-59327-448-3 ISBN-13: 978-1-59327-448-1 Publisher: William Pollock Production Editor: Serena Yang Cover Illustration: Charlie Wylie Interior Design: Octopod Studios Developmental Editor: William Pollock Technical Reviewer: Marc Alexander Copyeditor: Lisa Theobald Compositor: Susan Glinert Stevens Proofreader: Emelie Battaglia Circuit diagrams made using Fritzing (http://fritzing.org/) For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc directly: No Starch Press, Inc 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com Library of Congress Cataloging-in-Publication Data A catalog record of this book is available from the Library of Congress No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc Other product and company names mentioned herein may be the trademarks of their respective owners Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The information in this book is distributed on an “As Is” basis, without warranty While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it For the two people who have always believed in me: my mother and my dearest Kathleen www.elsolucionario.org Brief Contents Acknowledgments xix Chapter 1: Getting Started Chapter 2: Exploring the Arduino Board and the IDE 19 Chapter 3: First Steps 33 Chapter 4: Building Blocks 55 Chapter 5: Working with Functions 95 Chapter 6: Numbers, Variables, and Arithmetic 111 Chapter 7: Liquid Crystal Displays 147 Chapter 8: Expanding Your Arduino 161 Chapter 9: Numeric Keypads 187 Chapter 10: Accepting User Input with Touchscreens 195 Chapter 11: Meet the Arduino Family 207 Chapter 12: Motors and Movement 225 Chapter 13: Using GPS with Your Arduino 257 Chapter 14: Wireless Data 271 Chapter 15: Infrared Remote Control 285 Chapter 16: Reading RFID Tags 295 Chapter 17: Data Buses 307 Chapter 18: Real-time Clocks 321 Chapter 19: The Internet 337 Chapter 20: Cellular Communications 349 Index 365 viii Brief Contents www.elsolucionario.org Conte nt s in De ta il Acknowledgments xix Getting Started The Possibilities Are Endless Strength in Numbers Parts and Accessories Required Software Mac OS X Windows XP and Later Ubuntu Linux 9.04 and Later Safety Looking Ahead Exploring the Arduino Board and the IDE The Arduino Board Taking a Look Around the IDE The Command Area The Text Area The Message Window Area Creating Your First Sketch in the IDE Comments The Setup Function Controlling the Hardware The Loop Function Verifying Your Sketch Uploading and Running Your Sketch Modifying Your Sketch Looking Ahead 11 15 18 18 19 First Steps Planning Your Projects About Electricity Current Voltage Power Electronic Components The Resistor The Light-Emitting Diode The Solderless Breadboard Project #1: Creating a Blinking LED Wave The Algorithm The Hardware 19 25 25 26 26 27 27 28 28 28 30 31 31 31 33 34 34 34 35 35 35 35 39 41 43 43 43 The Sketch Enter the following sketch into the Arduino IDE, but don’t upload it yet: // Project 64 - Building an Arduino Texter #include #include u SerialGSM cell(2,3); void setup() { pinMode(7, INPUT); delay(30000); // wait for the GSM module cell.begin(9600); } void textSomeone() { cell.Verbose(true); // used for debugging cell.Boot(); cell.FwdSMS2Serial(); v cell.Rcpt("+xxxxxxxxxxx"); // replace xxxxxxxxxxx with the // recipient's cell number w cell.Message("This is the contents of a text message"); cell.SendSMS(); } void loop() { x if (digitalRead(7) == HIGH) { textSomeone(); } if (cell.ReceiveSMS()) { Serial.println(cell.Message()); cell.DeleteAllSMS(); } } How It Works The GSM shield is set up as normal at u and in void setup() Button presses are detected at x, and the function textSomeone is called This simple function sends a text message to the cellular phone number stored at v Before uploading the sketch, replace xxxxxxxxxxx with the recipient’s cellular phone number in international format: the country code, the area code, and the number, without any spaces or brackets For example, to send a text to 212.555.1212 in the United States, you would store +12125551212 Cellular Communications 359 The text message to be sent is stored at w (Note that the maximum length for a message is 160 characters.) After you have stored a sample text message and a destination number, upload the sketch, wait 30 seconds, and then press the button In a moment, the message should arrive on the destination phone, as shown in Figure 20-10 Figure 20-10: Sample text message being received Project 64 can be integrated quite easily into other sketches, and various text messages could be sent by comparing data against a parameter with a switch-case function NOTE Remember that the cost of text messages can add up quickly, so when you’re experimenting, be sure that you’re using an unlimited or prepaid calling plan Project #65: Setting Up an SMS Remote Control In this project you’ll control the digital output pins on your Arduino by sending a text message from your cell phone You should be able to use your existing knowledge to add various devices to control We’ll allow for four separate digital outputs, but you can control more or less as required To turn on or off four digital outputs (pins 10 through 13 in this example), you’d send a text message to your Arduino in the following format: #axbxcxdx, replacing x with either a for off or a for on For example, to turn on all four outputs, you’d send #a1b1c1d1 The Hardware This project uses the hardware described at the start of the chapter, plus any extra circuitry you choose We’ll use four LEDs to indicate the status of the digital outputs being controlled Therefore, the following extra hardware is required: • • • • 360 Chapter 20 Four LEDs Four 560 Ω resistors Various connecting wires One breadboard www.elsolucionario.org The Schematic Connect the external circuitry, as shown in Figure 20-11 3V3 5V Power RST Vin IO REF Arduino Digital Input/Output N/C A0 A1 A4 D11 PWM D10 PWM D9 PWM LED4 D8 R2 560Ω LED3 R1 560Ω LED2 LED1 D7 D6 PWM D5 PWM D4 D3 Analog Input A3 R3 560Ω D12 AREF A2 R4 560Ω D13 PWM D2 D1 D0 TX RX SCL A5 GND SDA Figure 20-11: Circuitry for Project 65 The Sketch Enter and upload the following sketch: // Project 65 - Setting Up an SMS Remote Control #include SoftwareSerial cell(2,3); char inchar; void setup() { // set up digital output pins to control pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); Cellular Communications 361 digitalWrite(10, digitalWrite(11, digitalWrite(12, digitalWrite(13, LOW); // default state for I/O pins at power-up or reset, LOW); // change as you wish LOW); LOW); //Initialize the GSM module serial port for communication cell.begin(9600); delay(30000); cell.println("AT+CMGF=1"); delay(200); cell.println("AT+CNMI=3,3,0,0"); delay(200); u v } void loop() { // If a character comes in from the cellular module w if(cell.available() > 0) { inchar = cell.read(); x if (inchar == '#') // the start of our command { delay(10); inchar = cell.read(); y if (inchar == 'a') { delay(10); inchar = cell.read(); if (inchar == '0') { digitalWrite(10, LOW); } else if (inchar == '1') { digitalWrite(10, HIGH); } delay(10); inchar = cell.read(); if (inchar == 'b') { inchar = cell.read(); if (inchar == '0') { digitalWrite(11, LOW); } else if (inchar == '1') { digitalWrite(11, HIGH); } delay(10); inchar = cell.read(); if (inchar == 'c') 362 Chapter 20 { inchar = cell.read(); if (inchar == '0') { digitalWrite(12, LOW); } else if (inchar == '1') { digitalWrite(12, HIGH); } delay(10); inchar = cell.read(); if (inchar == 'd') { delay(10); inchar = cell.read(); if (inchar == '0') { digitalWrite(13, LOW); } else if (inchar == '1') { digitalWrite(13, HIGH); } delay(10); } } cell.println("AT+CMGD=1,4"); // delete all SMS } } } } } How It Works In this project the Arduino monitors every text character sent from the GSM module Thus, at u we tell the GSM shield to convert incoming SMS messages to text and send the contents to the virtual serial port at v Next, the Arduino simply waits for a text message to come from the GSM shield at w Because the commands sent from the cell phone and passed by the GSM module to control pins on the Arduino start with a #, the sketch waits for a hash mark (#) to appear in the text message at x At y, the first output parameter a is checked—if it is followed by a or 1, the pin is turned off or on, respectively The process repeats for the next three outputs controlled by b, c, and d Imagine how easy it would be to use this project to create a remote control for all manner of things, such as lights, pumps, alarms, and more Cellular Communications 363 www.elsolucionario.org Looking Ahead With the three projects in this chapter, you’ve created a great framework on which to build your own projects that can communicate over a cell network You’re limited only by your imagination—for example, you could receive a text message if your basement floods or turn on your air conditioner from your cell phone Once again, remember to take heed of network charges before setting your projects free At this point, after having read about (and hopefully built) the 65 projects in this book, you should have the understanding, knowledge, and confidence you need to create your own Arduino-based projects You know the basic building blocks used to create many projects, and I’m sure you will be able to apply the technology to solve all sorts of problems and have fun at the same time This is only the beginning You can find many more forms of hardware to work with, and with some thought and planning, you can work with them all You’ll find a huge community of Arduino users on the Internet (in such places as the Arduino forum at http://arduino.cc/forum/) and even at a local hackerspace or club So don’t just sit there—make something! 364 Chapter 20 Index Symbols & Numbers &, 139 &&, 73 *, 84 */, 27 ==, 71 !, 73 !=, 71 /, 84 /*, 27 //, 27 >, 84 >=, 84 #define, 70 #include, 149 -, 83