Make: Basic Arduino Projects

Transcription

Technology & Engineering / ElectronicsBy building and experimenting with the 26 projectsoutlined in this book, you will learn how to:CArduinoProjectsx e KitDon Wilcher» Use the Arduino as a programmable computer brain» Create electronic controllers for LEDs and servo motors» Draw colors on your computer screen by pushing a button» Detect changes in temperature and notify your computer» Use an LCD display to show text and special characters» Control a motor’s movements with a tilt sensor» Build four projects that combine Arduino with the ProcessingluthetoThis book is the perfect companion to the Make: Getting Startedwith Arduino-Deluxe Kit which contains 100 pieces—from thebasics, like the Arduino board, breadboards, resistors, capacitorsLEDs, and jumper wires, to the fun stuff like servos, switches,sensors, buttons, motors, LCD screen—and more! The kit willjump-start your projects!BASICBasic Arduino ProjectsWritten for new makers, Basic Arduino Projects is a practical guidecontaining 26 projects that you can start on immediately. Learn tobuild electronic circuits that change their behavior, a light-sensitiveswitch, and LED projects like the pocket stage light. You’ll learnhow to control servo motors (the building blocks of robots) and tocreate audio projects like a theremin, electronic cricket, andmetronome. The only restriction is your imagination.Get ke:tSta ingrtedwithArduinoDeMake:Basic Arduino Projectspanioom Ma nvisual programming language» Make your own ohmmeter and logic testersStart making today.US 19.99 CAN 20.99ISBN: 978-1-4493-6066-5Make:makezine.com26 Experiments withMicrocontrollers and ElectronicsDon Wilcher

Make: BasicArduinoProjects26 Experiments with Microcontrollersand ElectronicsDon Wilcher

Make: Basic Arduino Projectsby Don WilcherCopyright 2014 Don Wilcher. All rights reserved.Printed in the United States of America.Published by Maker Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.Maker Media books may be purchased for educational, business, or sales promotional use.Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact O’Reilly Media’s corporate/institutional sales department:800-998-9938 or corporate@oreilly.com.Editor: Patrick Di JustoProduction Editor: Kara EbrahimCopyeditor: Charles RoumeliotisProofreader: Jasmine KwitynIndexer: Ellen TroutmanCover Designer: Juliann BrownInterior Designer: David FutatoIllustrator: Rebecca DemarestPhotographers: Frank Teng and Don WilcherFebruary 2014:First EditionRevision History for the First Edition:2014-02-05:First release2014-03-07:Second release2015-10-23:Third releaseSee http://oreilly.com/catalog/errata.csp?isbn 9781449360665 for release details.The Make logo and Maker Media logo are registered trademarks of Maker Media, Inc. Make:Basic Arduino Projects and related trade dress are trademarks of Maker Media, Inc.Many of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and Maker Media,Inc., was aware of a trademark claim, the designations have been printed in caps or initialcaps.While every precaution has been taken in the preparation of this book, the publisher andauthor assume no responsibility for errors or omissions, or for damages resulting from theuse of the information contained herein.ISBN: 978-1-449-36066-5[LSI]

ContentsPreface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix1.The Trick Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Let’s Build a Trick Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Trick Switch with On/Off Indicators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.Sunrise-Sunset Light Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Let’s Build a Sunrise-Sunset Light Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Sunrise-Sunset Detector with Serial Monitor. . . . . . . . . . . . . . . . . . . . . . . . . 14Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.Tilt Sensing Servo Motor Controller. . . . . . . . . . . . . . . . . . . . . . . . . . 19Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Let’s Build a Tilt Sensing Servo Motor Controller. . . . . . . . . . . . . . . . . . . . . . 20Upload the Tilt Sensor Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23A Simple Animatronic Controller Using the Serial Monitor. . . . . . . . . . . . . 24Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284.Twin LEDs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31iii

Twin LED Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Build the Adjustable Twin LED Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35It’s Alive! Build a FrankenBot Toy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415.The Opposite Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44The Opposite Switch (aka the NOT Logic Gate). . . . . . . . . . . . . . . . . . . . . . . 45Build an Arduino NOT Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Upload the Arduino NOT Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . 46Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506.The AND Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52The Arduino AND Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Upload the Arduino AND Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . 57Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597.The OR Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62The Arduino OR Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Upload the Arduino OR Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . 66Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698.Tilt Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72The Up-Down Sensor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769.Multicolor RGB Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80The RGB Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86ivContents

10.The Magic Light Bulb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Let’s Build a Magic Light Bulb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Upload the Magic Light Bulb Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9211.Metal Checker: The Electronic Switch. . . . . . . . . . . . . . . . . . . . . . . . 95Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Let’s Build a Metal Checker. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Upload the Metal Checker Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10112.The Theremin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Let’s Build a Theremin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Upload the Theremin Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11013.An Arduino Ohmmeter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Let’s Build an Arduino Ohmmeter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Upload the Arduino Ohmmeter Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11714.The LCD News Reader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Let’s Build the LCD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Upload the LCD News Reader Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12915.A Logic Tester (with an RGB LED). . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Let’s Build a Logic Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Contentsv

Upload the Logic Tester Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13616.A Logic Tester(with an LCD). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Let’s Build a Logic Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Upload the Logic Tester Sketch. . . . . . . . . . . . . .

create audio projects like a theremin, electronic cricket, and metronome. The only restriction is your imagination. This book is the perfect companion to the Make: Getting Started with Arduino-Deluxe Kit which contains 100 pieces—from the basics, like the Arduino board, breadboards, resistors, capacitors