J O H N B O X A L L ARDUINO WORKSHOP A H A N D S - O N I N T R O D U C T I O N W I T H 6 5 P R O J E C T S B O X A L L www.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT ™ “ I L I E F LAT .” Th is book uses RepKover — a durab le b ind ing that won’t snap shut. $29.95 ($31.95 CDN) SHELVE IN : HARDW ARE/ELECTRONICS A R D U IN O W O R K S H O P A R D U IN O W O R K S H O P L E A R N T H E B A S I C S , B U I L D T H E P R O J E C T S , C R E A T E Y O U R O W N The Arduino is a cheap, flexible, open source micro- controller platform designed to make it easy for hobbyists to use electronics in homemade projects. With an almost world around you. countless ways to create devices that interact with the unlimited range of input and output add-ons, sensors, indi- cators, displays, motors, and more, the Arduino offers you In Arduino Workshop, you’ll learn how these add-ons 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. reviews for years. Arduino Workshop is his first book. A B O U T T H E A U T H O R John Boxall (http://www.tronixstuff.com/ ) has been writing Arduino tutorials, projects, and kit and accessory • 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 but quickly move on to coverage of various electronic You’ll start off with an overview of the Arduino system components and concepts. Hands-on projects throughout work and how to integrate them into your own projects. 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: on an LCD • A digital thermometer that charts temperature changes • A GPS logger that records data from your travels, which can be displayed on Google Maps
(This page has no text content)
A r d u i n o W o r k s h o p A h a n d s - o n i n t r o d u c t i o n w i t h 6 5 p r o j e c t s San Francisco by John Boxa l l
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 1 2 3 4 5 6 7 8 9 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 infor- mation contained in it.
For the two people who have always believed in me: my mother and my dearest Kathleen
(This page has no text content)
B r i e f C o n t e n t s Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 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
viii Brief Contents Chapter 17: Data Buses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Chapter 18: Real-time Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Chapter 19: The Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Chapter 20: Cellular Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
C o n t e n t s i n d e t A i l AcknoWledgments xix 1 getting stArted 1 The Possibilities Are Endless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Strength in Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Parts and Accessories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Required Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Windows XP and Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Ubuntu Linux 9 .04 and Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 exploring the Arduino BoArd And the ide 19 The Arduino Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Taking a Look Around the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 The Command Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 The Text Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Message Window Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Creating Your First Sketch in the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The Setup Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Controlling the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Loop Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Verifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Uploading and Running Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Modifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3 First steps 33 Planning Your Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 About Electricity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Current . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Electronic Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 The Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 The Light-Emitting Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 The Solderless Breadboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Project #1: Creating a Blinking LED Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
x Contents in Detail The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Project #2: Repeating with for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Varying LED Brightness with Pulse-Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . 47 Project #3: Demonstrating PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49 More Electric Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 The Rectifier Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 The Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Higher-Voltage Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4 Building Blocks 55 Using Schematic Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Identifying Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Wires in Schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Dissecting a Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 The Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Measuring the Capacity of a Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Reading Capacitor Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Types of Capacitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Project #4: Demonstrating a Digital Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Modifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Understanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Creating Constants with #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Reading Digital Input Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Making Decisions with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Making More Decisions with if-then-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Making Two or More Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Project #5: Controlling Traffic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Analog vs . Digital Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Project #6: Creating a Single-Cell Battery Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Contents in Detail xi The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Doing Arithmetic with an Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Float Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Comparison Operators for Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Improving Analog Measurement Precision with a Reference Voltage . . . . . . . . . . . . . . . 84 Using an External Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Using the Internal Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The Variable Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Piezoelectric Buzzers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Piezo Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Project #7: Trying Out a Piezo Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 Project #8: Creating a Quick-Read Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Hacking the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5 Working With Functions 95 Project #9: Creating a Function to Repeat an Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Project #10: Creating a Function to Set the Number of Blinks . . . . . . . . . . . . . . . . . . . . . .97 Creating a Function to Return a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Project #11: Creating a Quick-Read Thermometer That Blinks the Temperature . . . . . . . . .98 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Displaying Data from the Arduino in the Serial Monitor . . . . . . . . . . . . . . . . . . . . . . 101 The Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Project #12: Displaying the Temperature in the Serial Monitor . . . . . . . . . . . . . . . . . . . .103 Debugging with the Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Making Decisions with while Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Sending Data from the Serial Monitor to the Arduino . . . . . . . . . . . . . . . . . . . . . . . . 106 Project #13: Multiplying a Number by Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 long Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Project #14: Using long Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 6 numBers, VAriABles, And Arithmetic 111 Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Using Ambient Current to Generate a Random Number . . . . . . . . . . . . . . . . 112 Project #15: Creating an Electronic Die . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Modifying the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
xii Contents in Detail A Quick Course in Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Byte Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Increasing Digital Outputs with Shift Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Project #16: Creating an LED Binary Number Display . . . . . . . . . . . . . . . . . . . . . . . . . . .119 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Connecting the 74HC595 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Project #17: Making a Binary Quiz Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Referring to Values in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Writing to and Reading from Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Seven-Segment LED Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Controlling the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Project #18: Creating a Single-Digit Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Displaying Double Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Project #19: Controlling Two Seven-Segment LED Display Modules . . . . . . . . . . . . . . . . .131 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Project #20: Creating a Digital Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 LED Matrix Display Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 The LED Matrix Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Making the Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Bitwise Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 The Bitwise AND Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 The Bitwise OR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 The Bitwise XOR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 The Bitwise NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Bitshift Left and Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Project #21: Creating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 Project #22: Creating Images on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Project #23: Displaying an Image on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 Project #24: Animating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 7 liquid crystAl displAys 147 Character LCD Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Using a Character LCD in a Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Displaying Variables or Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Contents in Detail xiii Project #25: Defining Custom Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152 Graphic LCD Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Connecting the Graphic LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Using the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Controlling the Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Project #26: Seeing the Text Functions in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 Creating More Complex Display Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Project #27: Creating a Temperature History Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . .157 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Modifying the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 8 expAnding your Arduino 161 Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 ProtoShields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Project #28: Creating a Custom Shield with Eight LEDs . . . . . . . . . . . . . . . . . . . . . . . . . .165 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 The Layout of the ProtoShield Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Soldering the Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Modifying the Custom Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Expanding Sketches with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Importing a Shield’s Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 MicroSD Memory Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Testing Your MicroSD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Project #29: Writing Data to the Memory Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Project #30: Creating a Temperature-Logging Device . . . . . . . . . . . . . . . . . . . . . . . . . . .177 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Timing Applications with millis() and micros() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Project #31: Creating a Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Interrupt Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Configuring Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Activating or Deactivating Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Project #32: Using Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
xiv Contents in Detail 9 numeric keypAds 187 Using a Numeric Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Wiring a Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Programming for the Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Making Decisions with switch-case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Project #33: Creating a Keypad-Controlled Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 10 Accepting user input With touchscreens 195 Touchscreens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Connecting the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Project #34: Addressing Areas on the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Mapping the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Project #35: Creating a Two-Zone On/Off Touch Switch . . . . . . . . . . . . . . . . . . . . . . . . .200 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Project #36: Creating a Three-Zone Touch Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 The Touchscreen Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 11 meet the Arduino FAmily 207 Project #37: Creating Your Own Breadboard Arduino . . . . . . . . . . . . . . . . . . . . . . . . . .208 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Running a Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 The Many Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Freetronics Eleven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 The Freeduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 The Boarduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 The Arduino Nano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 The Arduino LilyPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 The Arduino Mega 2560 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 The Freetronics EtherMega . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 The Arduino Due . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Contents in Detail xv 12 motors And moVement 225 Making Small Motions with Servos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Selecting a Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Connecting a Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Putting a Servo to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Project #38: Building an Analog Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Using Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 The TIP120 Darlington Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Project #39: Controlling the Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Project #40: Building and Controlling a Tank Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Sensing Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Project #41: Detecting Tank Bot Collisions with a Microswitch . . . . . . . . . . . . . . . . . . . . .243 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Infrared Distance Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Wiring It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Testing the IR Distance Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Project #42: Detecting Tank Bot Collisions with IR Distance Sensor . . . . . . . . . . . . . . . . .249 Ultrasonic Distance Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Connecting the Ultrasonic Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Using the Ultrasonic Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Testing the Ultrasonic Distance Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Project #43: Detecting Tank Bot Collisions with an Ultrasonic Distance Sensor . . . . . . . . .254 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 13 using gps With your Arduino 257 What Is GPS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Testing the GPS Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Project #44: Creating a Simple GPS Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Displaying the Position on the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Project #45: Creating an Accurate GPS-based Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
xvi Contents in Detail Project #46: Recording the Position of a Moving Object over Time . . . . . . . . . . . . . . . . .265 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Displaying Locations on a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 14 Wireless dAtA 271 Using Low-cost Wireless Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Project #47: Creating a Wireless Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 The Hardware for the Transmitter Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . 273 The Transmitter Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 The Hardware for the Receiver Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 The Receiver Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 The Transmitter Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 The Receiver Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Using XBee Wireless Data Modules for Greater Range and Faster Speed . . . . . . . . . . 277 Project #48: Transmitting Data with an XBee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Setting Up the Computer to Receive Data . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Project #49: Building a Remote Control Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . .281 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 15 inFrAred remote control 285 What Is Infrared? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Setting Up for Infrared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 The IR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 The Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 A Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Testing the Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Project #50: Creating an IR Remote Control Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . .289 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Expanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Project #51: Creating an IR Remote Control Tank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 16 reAding rFid tAgs 295 Inside RFID Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Testing the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Testing the Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Contents in Detail xvii Project #52: Creating a Simple RFID Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Storing Data in the Arduino’s Built-in EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Reading and Writing to the EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Project #53: Creating an RFID Control with “Last Action” Memory . . . . . . . . . . . . . . . . .303 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 17 dAtA Buses 307 The I2C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Project #54: Using an External EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 Project #55: Using a Port Expander IC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 The SPI Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Pin Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Implementing the SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Sending Data to an SPI Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Project #56: Using a Digital Rheostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 18 reAl-time clocks 321 Connecting the RTC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Project #57: Adding and Displaying Time and Date with an RTC . . . . . . . . . . . . . . . . . . . .322 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Project #58: Creating a Simple Digital Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 How It Works and Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Project #59: Creating an RFID Time-Clock System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
xviii Contents in Detail 19 the internet 337 What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Project #60: Building a Remote-Monitoring Station . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Project #61: Creating an Arduino Tweeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Controlling Your Arduino from the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Project #62: Setting Up a Remote Control for Your Arduino . . . . . . . . . . . . . . . . . . . . . .345 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 Controlling Your Arduino Remotely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 20 cellulAr communicAtions 349 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Preparing the Power Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Hardware Configuration and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Changing the Operating Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Project #63: Building an Arduino Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Project #64: Building an Arduino Texter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .358 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Project #65: Setting Up an SMS Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360 The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 index 365
A C k n o W l e d g m e n t s First of all, a huge thank you to the Arduino team: Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, and David Mellis. Without your vision, thought, and hard work, none of this would have been possible. Many thanks to my technical reviewer Marc Alexander for his contribu- tions, expertise, suggestions, support, thoughts, and long conversations, and for having the tenacity to follow through with such a large project. I also want to thank the following organizations for their images and encouragement: adafruit industries, Agilent Technologies, Gravitech, Freetronics, Oomlout, Seeed Studio, Sharp Corporation, and SparkFun. Furthermore, a big thanks to Freetronics for the use of their excellent hardware products. And thank you to all those who have contributed their time making Arduino libraries, which makes life much easier for everyone. Kudos and thanks to the Fritzing team for their wonderful open source circuit schematic design tool, which I’ve used throughout this book.
Comments 0
Loading comments...
Reply to Comment
Edit Comment