TECHNOLOGY IN ACTION™ Beginning Arduino Arduino 50 ARDUINO PROJECTS WITH STEP-BY-STEP INSTRUCTIONS AND EASY-TO-FOLLOW DIAGRAMS LEARN HOW TO WORK WITH MOTORS, SENSORS, DISPLAYS, AND NETWORKING CREATE SIMPLE BUT PRACTICAL PROJECTS SUCH AS A RANGEFINDER, AN RFID READER, AND AN INTERNET WEATHER DISPLAY NO PROGRAMMING OR ELECTRONICS EXPERIENCE NEEDED McRoberts Michael McRoberts Download from Wow! eBook Beginning Arduino ■■■ Michael McRoberts Beginning Arduino Copyright © 2010 by Michael McRoberts 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-3240-7 ISBN-13 (electronic): 978-1-4302-3241-4 Printed and bound in the United States of America 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 The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights President and Publisher: Paul Manning Lead Editor: Michelle Lowman Technical Reviewer: Josh Adams Editorial Board: 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: Jennifer L Blackwell Copy Editor: Mary Behr Production Support: Patrick Cunningham Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com For information on translations, please e-mail rights@apress.com, 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 I would like to dedicate this book to my mother for her encouragement throughout the book process and for being the best Mum anyone could ask for, and to my grandfather, Reginald Godfrey, for igniting the spark for science and electronics in me at a young age Without all those kits from Radio Shack at Christmas I may never have reached the point where I ended up writing a book about microcontrollers and electronics Thank you both Contents at a Glance About the Author xvii About the Technical Reviewer xviii Acknowledgments xix Introduction xx ■Chapter 1: Introduction ■Chapter 2: Light ’Em Up 21 ■Chapter 3: LED Effects 51 ■Chapter 4: Simple Sounders and Sensors 81 ■Chapter 5: Driving a DC Motor 99 ■Chapter 6: Binary Counters 111 ■Chapter 7: LED Displays 129 ■Chapter 8: Liquid Crystal Displays 171 ■Chapter 9: Servos 191 ■Chapter 10: Steppers and Robots 207 ■Chapter 11: Pressure Sensors 231 ■Chapter 12: Touch Screens 259 ■Chapter 13: Temperature Sensors 279 ■Chapter 14: Ultrasonic Rangefinders 293 ■Chapter 15: Reading and Writing to an SD Card 317 ■Chapter 16: Making an RFID Reader 343 ■Chapter 17: Communicating over Ethernet 359 Index 417 iv Contents About the Author xvii About the Technical Reviewer xviii Acknowledgments xix Introduction xx ■Chapter 1: Introduction How to Use This Book What You Will Need What Exactly is an Arduino? Getting Started Windows XP Installation Windows & Vista Installation Mac OSX Installation Board and Port Selection Upload Your First Sketch 10 The Arduino IDE 12 ■Chapter 2: Light ’Em Up 21 Project – LED Flasher 21 Parts Required 21 Connecting Everything 22 Enter the Code 23 v ■ CONTENTS Project – LED Flasher – Code Overview 23 Project – LED Flasher – Hardware Overview 27 Project – S.O.S Morse Code Signaler 31 Project – S.O.S Morse Code Signaler – Code Overview 33 Project – Traffic Lights 34 Parts Required 35 Connect It Up 35 Enter the Code 36 Project – Interactive Traffic Lights 38 Parts Required 38 Connect It Up 38 Enter the Code 39 Project – Code Overview 41 Project – Interactive Traffic Lights - Hardware Overview 45 Logic States 45 Pull-Down Resistors 46 Pull-Up Resistors 47 The Arduino’s Internal Pull-Up Resistors 48 Summary 48 ■Chapter 3: LED Effects 51 Project – LED Chase Effect 51 Parts Required 51 Connect It Up 51 Enter the Code 52 Project – LED Chase Effect – Code Overview 53 Project – Interactive LED Chase Effect 54 Parts Required 54 Connect It Up 55 vi ■ CONTENTS Enter The Code 56 Project – Interactive LED Chase Effect – Code Overview 56 Project – Interactive LED Chase Effect – Hardware Overview 57 Project – Pulsating Lamp 58 Parts Required 58 Connect It Up 58 Enter the Code 59 Project – Pulsating Lamp – Code Overview 60 Project – RGB Mood Lamp 61 Parts Required 61 Connect It Up 61 Enter the Code 61 Project – RGB Mood Lamp – Code Overview 63 Project – LED Fire Effect 66 Parts Required 66 Connect It Up 66 Enter the Code 67 Project – LED Fire Effect – Code Overview 68 Project 10 – Serial Controlled Mood Lamp 68 Enter the Code 69 Project 10 – Serial Controlled Mood Lamp – Code Overview 71 Summary 79 ■Chapter 4: Simple Sounders and Sensors 81 Project 11 – Piezo Sounder Alarm 81 Parts Required 81 Connect It Up 81 Enter the Code 82 vii ■ CONTENTS Project 11 – Piezo Sounder Alarm – Code Overview 83 Project 11 – Piezo Sounder Alarm – Hardware Overview 84 Project 12 – Piezo Sounder Melody Player 85 Enter the Code 85 Project 12 – Piezo Sounder Melody Player – Code Overview 86 Project 13 – Piezo Knock Sensor 89 Parts Required 89 Connect It Up 90 Enter the Code 90 Project 13 – Piezo Knock Sensor – Code Overview 91 Project 14 – Light Sensor 92 Parts Required 92 Connect It Up 93 Enter the Code 93 Project 14 – Light Sensor – Hardware Overview 94 Summary 96 Subjects and Concepts covered in Chapter 4: 97 ■Chapter 5: Driving a DC Motor 99 Project 15 – Simple Motor Control 99 Parts Required 99 Connect It Up 100 Enter The Code 101 Project 15 – Simple Motor Control – Code Overview 101 Project 15 – Simple Motor Control – Hardware Overview 102 Project 16 – Using an L293D Motor Driver IC 104 Parts Required 104 Connect It Up 105 Enter the Code 105 viii ...Download from Wow! eBook Beginning Arduino ■■■ Michael McRoberts Beginning Arduino Copyright © 2010 by Michael McRoberts All rights reserved No part... and cheaply Mike’s fascination with the Arduino had begun Since then he has gone on to make countless projects using the Arduino He had also founded an Arduino starter kit and component online... found contributing to the Arduino Forum He also likes to lurk on IRC in the Arduino, high altitude and london-hack-space channels (as “earthshine”), and on Twitter as “TheArduinoGuy.” When he is