Practical Arduino - Springer

5 downloads 191 Views 373KB Size Report
Practical Arduino: Cool Projects for Open Source Hardware. Copyright ... Cover picture of Arduino Duemilanove used with permission of SparkFun Electronics.
Practical Arduino Cool Projects for Open Source Hardware

„„„ Jonathan Oxer Hugh Blemings

Practical Arduino: Cool Projects for Open Source Hardware Copyright © 2009 by Jonathan Oxer and Hugh Blemings 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. ISBN-13 (pbk): 978-1-4302-2477-8 ISBN-13 (electronic): 978-1-4302-2478-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Cover picture of Arduino Duemilanove used with permission of SparkFun Electronics. Publisher and President: Paul Manning Lead Editor: Michelle Lowman Technical Reviewers: Nathan Seidle, Andy Gelme, Thomas Sprinkmeier, Trent Lloyd, Scott Penrose, Marc Alexander, Philip Lindsay Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jim Markham Copy Editor: Vanessa Moore Compositor: Bytheway Publishing Services Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer+Business Media, LLC, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 this work. The source code for this book is available to readers at www.apress.com.

For everyone who looks at the everyday objects around them and sees the potential of what they could become... For all those in the Free & Open Source Software/Hardware Communities that create the tools to help realise those potentials.

„ CONTENTS

Contents at a Glance „ About the Authors................................................................................................ xiv „ About the Technical Reviewers ............................................................................ xv „ Acknowledgments .............................................................................................. xvii „ Chapter 1: Introduction...........................................................................................1 „ Chapter 2: Appliance Remote Control...................................................................17 „ Chapter 3: Time-Lapse Camera Controller............................................................37 „ Chapter 4: Virtual USB Keyboard ..........................................................................51 „ Chapter 5: PS/2 Keyboard or Mouse Input ...........................................................63 „ Chapter 6: Security/Automation Sensors .............................................................81 „ Chapter 7: Online Thermometer..........................................................................101 „ Chapter 8: Touch Control Panel ..........................................................................121 „ Chapter 9: Speech Synthesizer...........................................................................139 „ Chapter 10: Water Flow Gauge ...........................................................................163 „ Chapter 11: Oscilloscope/Logic Analyzer ...........................................................187 „ Chapter 12: Water Tank Depth Sensor................................................................211 „ Chapter 13: Weather Station Receiver................................................................241 „ Chapter 14: RFID Access Control System ...........................................................273 „ Chapter 15: Vehicle Telemetry Platform .............................................................301 „ Chapter 16: Resources........................................................................................397 „ Index ...................................................................................................................425

iv

„

Contents „ About the Authors................................................................................................ xiv „ About the Technical Reviewers ............................................................................ xv „ Acknowledgments .............................................................................................. xvii „ Chapter 1: Introduction...........................................................................................1 Fundamentals ....................................................................................................................2 Sharing Your Work.............................................................................................................2 Practical Electronics for Software Developers ..................................................................2 Current, Voltage, and Power..................................................................................................................... 2 Mains Is Nasty .......................................................................................................................................... 3 Reading Schematics................................................................................................................................. 4 Resistance and Resistors ......................................................................................................................... 6 Ohm’s Law and Current Limiting .............................................................................................................. 7 Choosing Wire........................................................................................................................................... 9 Diodes....................................................................................................................................................... 9 Power Supplies....................................................................................................................................... 10 Capacitance and Capacitors................................................................................................................... 12 ESD Precautions ..................................................................................................................................... 14 Tools ....................................................................................................................................................... 14 Parts ....................................................................................................................................................... 15 Further Reading...................................................................................................................................... 16

„ Chapter 2: Appliance Remote Control...................................................................17 Parts Required .................................................................................................................17

v

„ CONTENTS

Instructions......................................................................................................................19 Test and Investigate Appliance Remote ................................................................................................. 19 Assemble Reed Relay Shield .................................................................................................................. 21 Connect Reed Relay Shield to Remote Control....................................................................................... 25 Create Reed Relay Control Program ....................................................................................................... 27 Test Reed Relay Shield and Sketch........................................................................................................ 32

Variations.........................................................................................................................34 Wireless Link .......................................................................................................................................... 34 Automatic Trigger................................................................................................................................... 34 Socket Connections................................................................................................................................ 35

„ Chapter 3: Time-Lapse Camera Controller............................................................37 Parts Required .................................................................................................................37 Instructions......................................................................................................................39 Assemble Prototyping Shield.................................................................................................................. 39 Connect Camera Shutter Release........................................................................................................... 39 Configure Camera................................................................................................................................... 44 Calculate Photo Interval.......................................................................................................................... 44 Configure and Load Sketch .................................................................................................................... 44 Check Memory Capacity......................................................................................................................... 48 Set Up Your Shoot................................................................................................................................... 48 Process the Images ................................................................................................................................ 48

Variations.........................................................................................................................49 „ Chapter 4: Virtual USB Keyboard ..........................................................................51 Parts Required .................................................................................................................51 Instructions......................................................................................................................53 Populate Prototyping Shield ................................................................................................................... 53 Prepare the UsbKeyboard Library........................................................................................................... 56 Compile and Upload Sketch.................................................................................................................... 57

vi

„ CONTENTS

Variations.........................................................................................................................61 Chording Keyboard ................................................................................................................................. 61

„ Chapter 5: PS/2 Keyboard or Mouse Input ...........................................................63 Parts Required .................................................................................................................63 Instructions......................................................................................................................64 PS/2 Connections ................................................................................................................................... 65 Recycled 6-Pin Mini-DIN Sockets........................................................................................................... 66 6-Pin Mini-DIN Panel Sockets ................................................................................................................ 67 PS/2 Extension Cable.............................................................................................................................. 69 Keyboard Software ................................................................................................................................. 73 Mouse Software ..................................................................................................................................... 76 Variations................................................................................................................................................ 79

Resources........................................................................................................................80 „ Chapter 6: Security/Automation Sensors .............................................................81 Parts Required .................................................................................................................82 Instructions......................................................................................................................84 Security Sensor Basics........................................................................................................................... 84 Assemble Four-Channel Alarm Sensor Shield........................................................................................ 87 Install End-of-Line Resistors on Sensor ................................................................................................. 92 Load Test Program ................................................................................................................................. 95 PIR Placement ........................................................................................................................................ 98

Variations.........................................................................................................................98 Visual Display Written in Processing ...................................................................................................... 98 Home Security System ........................................................................................................................... 99 Multiple Buttons on One Input ................................................................................................................ 99

„ Chapter 7: Online Thermometer..........................................................................101 Parts Required ...............................................................................................................101 Instructions....................................................................................................................103

vii

„ CONTENTS

Mount PCB Plugs on Shield .................................................................................................................. 103 Connect Data Lines............................................................................................................................... 105 Assemble Sensors ................................................................................................................................ 106

Load Program ................................................................................................................108 Variations.......................................................................................................................119 „ Chapter 8: Touch Control Panel ..........................................................................121 Parts Required ...............................................................................................................122 Instructions....................................................................................................................123 How Resistive Touch Screens Work ..................................................................................................... 123 Basic Touch Screen Connection Test ................................................................................................... 126 Arduino TouchScreen Library ............................................................................................................... 128 Controlling a “Processing” Program..................................................................................................... 128 Construct Hardware for Home Automation Control Panel .................................................................... 131 Calibrate Hot Zones .............................................................................................................................. 134 Mount Arduino ...................................................................................................................................... 137

„ Chapter 9: Speech Synthesizer...........................................................................139 Parts Required ...............................................................................................................140 Instructions....................................................................................................................142 Instructions....................................................................................................................143 Speech Output Signal ........................................................................................................................... 143 Beginning Assembly ............................................................................................................................. 144 SpeakJet PWM "Audio" Output ............................................................................................................ 145 Quick Test............................................................................................................................................. 146 Fit Status Indicators.............................................................................................................................. 146 Making a Line-Level Output Cable........................................................................................................ 149 On-Board Amplifier............................................................................................................................... 150 Speech Synthesizer Software............................................................................................................... 152 Create Your Own Messages.................................................................................................................. 154

viii

„ CONTENTS

Variations.......................................................................................................................161 Read SpeakJet Status................................................................................................. 161 Resources............................................................................................................................................. 162

„ Chapter 10: Water Flow Gauge ...........................................................................163 Parts Required ...............................................................................................................164 Instructions....................................................................................................................165 Replace Sensor Connector ................................................................................................................... 165 Prepare Prototyping Shield................................................................................................................... 166 Prepare LCD Module............................................................................................................................. 168 Fit LCD to Case ..................................................................................................................................... 171 Fit Arduino in Case ............................................................................................................................... 173 Determine Scaling Factor ..................................................................................................................... 174 Configure, Compile, and Test Sketch ................................................................................................... 175 Flow Gauge Sketch............................................................................................................................... 178 Install Flow Sensor ............................................................................................................................... 185

Variations.......................................................................................................................186 Online Logging...................................................................................................................................... 186 Multiple Sensors................................................................................................................................... 186

Resources......................................................................................................................186 „ Chapter 11: Oscilloscope/Logic Analyzer ...........................................................187 Parts Required ...............................................................................................................189 Instructions....................................................................................................................191 Assemble the Case ............................................................................................................................... 192 Fit the Prototyping Shield ..................................................................................................................... 195 Make Test Probes................................................................................................................................. 196 Connection to Circuit under Test .......................................................................................................... 199 Install Software in Arduino ................................................................................................................... 200 Install Processing and Run Visualization Program ............................................................................... 205

ix

„ CONTENTS

Variations.......................................................................................................................207 Input Voltage Prescaler ........................................................................................................................ 207

Resources......................................................................................................................208 „ Chapter 12: Water Tank Depth Sensor................................................................211 Parts Required ...............................................................................................................212 Instructions....................................................................................................................213 Assemble the Shield............................................................................................................................. 216 Determine Sensor Installation Method ................................................................................................. 218 Assemble the Sensor Housing.............................................................................................................. 221 Assemble the Arduino Housing............................................................................................................. 225 Install the WiShield Library and Driver ................................................................................................. 228 Load the Tank-Level Sketch ................................................................................................................. 229 Prettier Web Interface .......................................................................................................................... 233 Calibrating the “Empty Tank” Level ..................................................................................................... 236 Install the Sensor and Arduino ............................................................................................................. 237 Calibrating the “Full Tank” Level.......................................................................................................... 238

Variations.......................................................................................................................238 Upload Data to Pachube ....................................................................................................................... 238 Control Pumps or Irrigation .................................................................................................................. 238 Local Level Display............................................................................................................................... 239 Reading Multiple Tanks ........................................................................................................................ 239

„ Chapter 13: Weather Station Receiver................................................................241 Parts Required ...............................................................................................................244 Instructions....................................................................................................................244 Test and Install the Weather Station .................................................................................................... 244 Understanding the Circuit..................................................................................................................... 245 Assemble the Receiver Shield .............................................................................................................. 248 Weather Station Receiver Program ...................................................................................................... 252

x

„ CONTENTS

Twitter Weather Updates...................................................................................................................... 269

Variations.......................................................................................................................271 Private Online Weather Station............................................................................................................. 271

„ Chapter 14: RFID Access Control System ...........................................................273 Parts Required ...............................................................................................................276 Instructions....................................................................................................................278 Assemble the Prototyping Shield.......................................................................................................... 279 Manual Release Button......................................................................................................................... 286 Assemble the ID-12 Reader Module ..................................................................................................... 286 Assemble the RDM630 Reader............................................................................................................. 288 Simple Stand-Alone Sketch.................................................................................................................. 289 Install the Strike Plate .......................................................................................................................... 296 Install the Reader ................................................................................................................................. 296 Install the Arduino ................................................................................................................................ 297

Variations.......................................................................................................................298 Extensible Read Head........................................................................................................................... 298 Speech Synthesizer Feedback ............................................................................................................. 298 Intelligent Desk Pad.............................................................................................................................. 299

Resources......................................................................................................................299 „ Chapter 15: Vehicle Telemetry Platform .............................................................301 Parts Required ...............................................................................................................304 Instructions....................................................................................................................307 Check the Vehicle Interface.................................................................................................................. 307 Obtain a USB/OBD-II or RS-232 Adapter............................................................................................... 308 Test the USB/OBD-II Adapter ................................................................................................................ 309 Understanding OBD-II Modes and Parameters..................................................................................... 310 Prepare the USB/OBD-II Adapter .......................................................................................................... 315 Assemble the OBD-II Cable................................................................................................................... 319

xi

„ CONTENTS

Prepare the GPS Module....................................................................................................................... 321 Assemble the Power Supply on the Shield........................................................................................... 326 Fit the Serial Connections on the Shield............................................................................................... 330 Prepare the VDIP1 Module.................................................................................................................... 331 The LCD Module.................................................................................................................................... 335 Logging Control Button and Status LEDs.............................................................................................. 339 Mount in Sub-Assemblies in the Case.................................................................................................. 340 OBDuino Mega Sketch.......................................................................................................................... 346 Using the OBDuinoMega Sketch........................................................................................................... 388

Variations.......................................................................................................................393 Mobile Internet Connection .................................................................................................................. 393 Vehicle Control...................................................................................................................................... 394 Speech Synthesizer Output .................................................................................................................. 394 3D Accelerometer................................................................................................................................. 394 Digital Compass.................................................................................................................................... 394 “Knight Rider”–Style Alarm Status....................................................................................................... 394 Battery Condition Check ....................................................................................................................... 394

Resources......................................................................................................................395 „ Chapter 16: Resources........................................................................................397 Simple Voltage Regulators.............................................................................................397 Power Dissipation................................................................................................................................. 398 Dropout Voltage .................................................................................................................................... 398 Efficiency.............................................................................................................................................. 398

Driving LEDs ..................................................................................................................398 Multiplexing.......................................................................................................................................... 399 Charlieplexing....................................................................................................................................... 401

Output Circuitry and Isolation ........................................................................................402 Semiconductor and Mechanical Switches ........................................................................................... 403

xii

„ CONTENTS

Digital Input/Output Expansion ......................................................................................407 Shift Registers as Outputs.................................................................................................................... 407 Shift Registers as Inputs ...................................................................................................................... 409

Input Conditioning..........................................................................................................410 Voltage Dividers and Nonisolated Input Conditioning........................................................................... 410 Isolated Input Conditioning................................................................................................................... 411

Inexpensive DIY Prototyping Shields .............................................................................413 Writing an Arduino Library.............................................................................................415 Develop Functionality as a Sketch........................................................................................................ 416 Create the Library Header File.............................................................................................................. 417 Create the Library Class File................................................................................................................. 419 Create the Example Sketch .................................................................................................................. 422 Create Supporting Files ........................................................................................................................ 423 Platform-Specific Variations................................................................................................................. 423

„ Index ...................................................................................................................425

xiii

About the Authors

„ Jonathan Oxer, who has been labeled “Australia's Geekiest Man,” has been hacking on both hardware and software since he was a little child. He is a former President of Linux Australia, and founder and technical director of Internet Vision Technologies. He is author of a number of books including How to Build a Website and Stay Sane, Ubuntu Hacks, and Quickstart Guide to Google AdWords. Oxer is set to host an upcoming TV show called SuperHouse (www.superhouse.tv), which features high-tech home renovations, open source automation systems, and domestic hardware hacking. He has appeared on toprating TV shows and been interviewed on dozens of radio stations about his home-automation system. He was technical supervisor for the first season of the reality TV show The Phone, has connected his car to the Internet for www.geekmyride.org, and has even been surgically implanted with an RFID chip. Oxer is also a member of the core team of Lunar Numbat (www.lunarnumbat.org), an Australian group working with the European team White Label Space (www.whitelabelspace.com) on an unmanned moon mission for the Google Lunar X-Prize. „ Hugh Blemings took a radio apart when he was about eight years old and never recovered. From this start and an interest in ham radio, an early career in hardware and embedded software development easily followed, back when 68HC11s were the latest and greatest. Blemings has been working on Free Software since the mid 1990s for fun, and in a (still fun!) professional capacity since 1999. He was co-author of the gnokii project and developed kernel device drivers for the Keyspan USB-serial adaptors. Blemings worked at IBM's Linux Technology Centre as a open source hacker in the Canberra-based OzLabs team for just shy of eight years doing everything from first-line management to Linux kernel porting for embedded PowerPC platforms. He now works on Ubuntu Linux at Canonical in the kernel team, but remains firmly of the view that any day that involves a soldering iron, a 'scope, and emacs is a good day.

xiv

About the Technical Reviewers

„ Nathan Seidle is the founder of SparkFun Electronics based in Boulder, Colorado. When he's not building large blinky things for BurningMan, Nathan designs development tools to enable users to build their wild imaginations.

„ Andy Gelme is a distributed systems designer, currently working for Geekscape in Melbourne, Australia. Throughout his career, he has enjoyed playing at the extremes of the computing landscape, from networks of embedded microprocessors to supercomputers. His current focus is on software and hardware development based around the Internet of Things, in particular the Aiko platform. He is also a co-founder of the Connected Community HackerSpace (Melbourne). „ Thomas Sprinkmeier graduated from the University of South Australia in 1992 with a degree in electronics engineering. It was there that he was seduced by PCs early in his first year. He was intrigued by “free as in beer” about a decade ago, and subverted by “free as in speech” soon after. Thomas is a recovering sysadmin.

„ Trent Lloyd lives in Perth, Australia, and works as MySQL Technical Support Engineer for Sun Microsystems. He is also the director and lead developer for Web in a Box. In addition, he is co-author of the Avahi project, an amateur poker player, and a Star Trek fan. „ Scott Penrose is a full-time developer in Linux. He is the principal architect at myinternet Limited and the owner of Digital Dimensions. He lives with his wife Amanda and his beautiful daughter Teha in Melbourne, Australia.

xv

„ ABOUT THE TECHNICAL REVIEWERS

„ Marc Alexander is an embedded electronics engineer, programmer, and gadget-head. He has worked on projects from the Apple Newton as well as another favorite area—real-time control and engine-management systems, including the Wolf3D and Bike Interceptor. Alexander does automotive and consumer electronics design and loves devices that “just work,” hiding the thorough development underneath.

„ Philip Lindsay has a particular interest in the intersection of software, hardware, craft, and art. He has integrated network and USB technologies into the Arduino ecosystem. Occasionally, he can be heard mumbling how a prominent industry figure once called him a “troublemaker” for his Google Maps reverse engineering.

xvi

Acknowledgments From the Authors, our thanks to: The tech reviewers who provided us the benefit of their expertise and years of experience: Andy Gelme, Marc Alexander, Nathan Seidle, Trent Lloyd, Scott Penrose, Thomas Sprinkmeier, and Philip J Lindsay. The core Arduino team whose vision conjured the whole Arduino ecosystem into existence: Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, David Mellis, and Nicholas Zambetti. The parts suppliers who were so helpful when it came to sourcing the random assortment of bits needed for developing these projects: SparkFun, AsyncLabs, and NKC Electronics. Arduino developers everywhere. The amazing success of Arduino is due to the strong community that has blossomed around it. It's a beautiful thing when imaginative people have new tools placed in their hands, and the results have been inspirational to us both. Michelle Lowman and James Markham, our editors at Apress who had to turn out of bed early to catch us in a totally different timezone on our weekly update call. From Jon: More thanks than I can express to Ann, Amelia, and Thomas. Their patience during this project has been amazing. Thankyou to my parents, Michael and Jenny, who never imposed restrictions on my wild ideas and taught me that no dream is too big to pursue. And for buying me a subscription to an electronics magazine when I was just a little tacker with a burning curiosity about the way things work. And to Hugh Blemings, my partner in crime whose patient discussions during many late-night phone calls helped me understand far more about Arduino. From Hugh: A profound thankyou to Lucy and Rachael for their love and support, for tolerating the smell of solder from the end room and for making the greater journey so worthwhile. My thanks also to my parents, Brian and Rosemary Blemings, who raised Kay and I to have enquiring minds and happily answered innumerable “Why..?” questions during the formative years. My father also helped me take that first alarm clock apart :) Thank you Jon for inviting me to be a part of this project and for kind words during some tricky periods in the writing process.

xvii