The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Make: Basic Arduino Projects. Experiments with Microcontrollers and Electronics.

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Elkhan Julian, 2016-09-12 09:56:27

Make: Basic Arduino Projects

Make: Basic Arduino Projects. Experiments with Microcontrollers and Electronics.

Keywords: make,make magazine,basic,arduino,projects,experiment,microcontroller,electronic,micro,controller

Make: Basic
Arduino
Projects

26 Experiments with Microcontrollers
and Electronics

Don Wilcher

Make: Basic Arduino Projects

by Don Wilcher

Copyright © 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://my.safaribooksonline.com). For more
information, contact O’Reilly Media’s corporate/institutional sales department:
800-998-9938 or [email protected]

Editor: Patrick Di Justo
Production Editor: Kara Ebrahim
Copyeditor: Charles Roumeliotis
Proofreader: Jasmine Kwityn
Indexer: Ellen Troutman
Cover Designer: Juliann Brown
Interior Designer: David Futato
Illustrator: Rebecca Demarest
Photographers: Frank Teng and Don Wilcher

February 2014: First Edition

Revision History for the First Edition:

2014-02-05: First release
2014-03-07: Second release

See 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 products
are 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 initial
caps.

While every precaution has been taken in the preparation of this book, the publisher and
author assume no responsibility for errors or omissions, or for damages resulting from the
use of the information contained herein.

ISBN: 978-1-449-36066-5
[LSI]

Contents

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

iii

Twin LED Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Build the Adjustable Twin LED Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
It’s Alive! Build a FrankenBot Toy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

5. The Opposite Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The Opposite Switch (aka the NOT Logic Gate). . . . . . . . . . . . . . . . . . . . . . . 45
Build an Arduino NOT Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Upload the Arduino NOT Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . 46
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

6. The AND Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
The Arduino AND Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Upload the Arduino AND Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . 57
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

7. The OR Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The Arduino OR Logic Gate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Upload the Arduino OR Logic Gate Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

8. Tilt Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The Up-Down Sensor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

9. Multicolor RGB Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The RGB Flasher. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

iv Contents

Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

10. The Magic Light Bulb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Let’s Build a Magic Light Bulb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Upload the Magic Light Bulb Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

11. Metal Checker: The Electronic Switch. . . . . . . . . . . . . . . . . . . . . . . . 95
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Let’s Build a Metal Checker. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Upload the Metal Checker Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

12. The Theremin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Let’s Build a Theremin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Upload the Theremin Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

13. An Arduino Ohmmeter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Let’s Build an Arduino Ohmmeter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Upload the Arduino Ohmmeter Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

14. The LCD News Reader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Let’s Build the LCD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Upload the LCD News Reader Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

15. A Logic Tester (with an RGB LED). . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

Contents v

Let’s Build a Logic Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Upload the Logic Tester Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

16. A Logic Tester(with an LCD). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Let’s Build a Logic Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Upload the Logic Tester Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Circuit Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

17. The Amazing Pushbutton (with Processing). . . . . . . . . . . . . . . . . 143
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Let’s Build an Amazing Pushbutton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Upload the Amazing Pushbutton Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Download and Install Processing Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Let’s Visualize Digital Data with Processing. . . . . . . . . . . . . . . . . . . . . . . . . . 148
Troubleshooting Tips for Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

18. The Terrific Tilt Switch (with Processing). . . . . . . . . . . . . . . . . . . . 157
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Let’s Build a Terrific Tilt Switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Upload the Terrific Tilt Switch Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Let’s Visualize Digital Data with Processing. . . . . . . . . . . . . . . . . . . . . . . . . . 162
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

19. The Rocket Launching Game (with Processing). . . . . . . . . . . . . . 169
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Let’s Build a Rocket Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Upload the MultiDigital4 Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Rocket Launcher with Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

20. Temperature Indicator (with Processing). . . . . . . . . . . . . . . . . . . . 183
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Let’s Build a Temperature Indicator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

vi Contents

Upload the Temperature Indicator Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . 185
The Negative Temperature Coefficient (NTC) Sensor with Processing. . . 188
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

21. Sweeping Servo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Let’s Build a Servo Motor Tester. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Upload the Sweeping Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

22. Electronic Cricket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Let’s Build an Electronic Cricket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Upload the Electronic Cricket Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

23. A Pocket Stage Light. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Let’s Build a Pocket Stage Light. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Upload the Pocket Stage Light Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

24. Electronic Pixel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Let’s Build an Electronic Pixel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Upload the Electronic Pixel Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

25. The Metronome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Let’s Build a Metronome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Upload the Metronome Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

26. The Secret Word Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Parts List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Let’s Build a Secret Word Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Upload the Secret Word Game Sketch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Rules for the Secret Word Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

Contents vii

Something to Think About. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

viii Contents

Preface

So, you’ve bought the Ultimate Microcontroller Pack to build some cool and fun
Arduino projects. Now all you need are some sample projects to build with it! The
Basic Arduino Projects book is here to help you! It’s got a wealth of cool devices and
gadgets to build with your Ultimate Microcontroller Pack. The projects in the book
explain the world of electronics using a fun and hands-on approach.
The motivation behind writing this book is based on several conversations with
Brian Jepson (Make: Books Senior Editor) and the need for a book that allows people
to explore the electronic parts and the Arduino within the Ultimate Microcontroller
Pack. The Arduino is a very popular Maker platform that allows you to explore elec-
tronics with an interactive approach. As awesome as a box of parts is, it’s difficult for
people with little electronics experience to begin making things with it. This book
solves that problem by letting you learn more about electronics while you make fun
projects with the parts in this kit. Basic Arduino Projects is a practical guide that
illustrates how a bunch of electronic parts, coupled with Arduino, can be trans-
formed into awesome devices and gadgets for education and play.
In addition, being an electrical engineer and educator, I’m very sensitive to deliver-
ing good instructional content to my students (adults and teenagers). This book was
written to attract young readers to the exciting world of electronics by building cool
and creative projects using the Ultimate Microcontroller Pack. This book is also in-
tended for Makers and novices who have heard about the Arduino but never ex-
perienced the fun and excitement that comes from building cool electronic gadgets
and devices with this open hardware platform.
By building and experimenting with the projects in this book, young readers, Mak-
ers, and electronic novices will learn how to:

• Read electronic circuit schematic and block diagrams.
• Assemble electronic circuits using the MakerShield prototyping board.

ix



book and quoting example code does not require permission. Incorporating a sig-
nificant amount of example code from this book into your product’s documentation
does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the
title, author, publisher, and ISBN. For example: “Make: Basic Arduino Projects by Don
Wilcher (Maker Media). Copyright 2014 Don Wilcher, 978-1-449-36066-5.”
If you feel your use of code examples falls outside fair use or the permission given
here, feel free to contact us at [email protected]

Safari® Books Online

Safari Books Online is an on-demand digital library that delivers
expert content in both book and video form from the world’s
leading authors in technology and business.

With a subscription, you can read any page and watch any video from our library
online. Read books on your cell phone and mobile devices. Access new titles before
they are available for print, get exclusive access to manuscripts in development, and
post feedback for the authors. Copy and paste code samples, organize your favorites,
download chapters, bookmark key sections, create notes, print out pages, and ben-
efit from tons of other time-saving features.
Maker Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from MAKE and other pub-
lishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:
MAKE
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

MAKE unites, inspires, informs, and entertains a growing community of resourceful
people who undertake amazing projects in their backyards, basements, and
garages. MAKE celebrates your right to tweak, hack, and bend any technology to
your will. The MAKE audience continues to be a growing culture and community
that believes in bettering ourselves, our environment, our educational system—our
entire world. This is much more than an audience, it’s a worldwide movement that
Make is leading—we call it the Maker Movement.

Preface xi

For more information about MAKE, visit us online:
MAKE magazine: http://makezine.com/magazine/
Maker Faire: http://makerfaire.com
Makezine.com: http://makezine.com
Maker Shed: http://makershed.com/

We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://oreil.ly/basic-arduino.
To comment or ask technical questions about this book, send email to bookques
[email protected]

Acknowledgments

I would like to thank Brian Jepson (Senior Editor) for believing in the book concept
and allowing me to explore the Ultimate Microcontroller Pack in creative ways. Also,
I would like to thank Patrick Di Justo (Editor) for pulling out the really cool projects
from the original book proposal and coaching me to present them in fun and en-
tertaining ways for young readers.
My final acknowledgment goes to my wife, Mattalene, who patiently worked with
me on editing this book, keeping me on task with the writing/project builds, and
reviewing the email revision messages from my editors. To my children, Tiana,
D’Vonn, and D’Mar, thanks for being great kids while I worked on the book during
family time.

xii Preface









Figure 1-3. Adding a green LED indicator to the Trick Switch circuit built on a full-size clear
breadboard

To complete the new product design, you need to make a few changes to the Push-
button sketch. Modify the sketch using the code changes shown in Example 1-2.
Example 1-2. Pushbutton sketch modified to include LED indicators

// constants won't change; they're used here to

// set pin numbers:

const int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 12; // the number of the LED pin

const int ledPin13 = 13; // onboard LED

void setup() {
// initialize the LED pins as outputs:
pinMode(ledPin, OUTPUT);
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

}

void loop(){
// read the state of the pushbutton value:
int buttonStatus;
buttonStatus = digitalRead(buttonPin);

// check if the pushbutton is pressed
// if it is, the buttonStatus is HIGH:
if (buttonStatus == HIGH) {

Chapter 1: The Trick Switch 5

// turn LED on:
digitalWrite(ledPin, HIGH);
// turn off onboard LED:
digitalWrite(ledPin13,LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
// turn on onboard LED:
digitalWrite(ledPin13, HIGH);
}
}

After you’ve saved the sketch changes and uploaded them to the Arduino, the green
LED will turn on. When you press the mini pushbutton, the green LED will turn off,
and the red LED will turn on. Pretty awesome stuff. Enjoy!
The block diagram in Figure 1-4 shows the electronic component blocks and the
electrical signal flow for the Trick Switch. A Fritzing electronic circuit schematic di-
agram of the switch is shown in Figure 1-5. Electronic circuit schematic diagrams
are used by electrical/electronic engineers to design and build cool electronic prod-
ucts for society.

Figure 1-4. Trick Switch block diagram

Something to Think About

Try different resistor and capacitor values and see what happens. Can you detect
any patterns? How can a small piezo buzzer be used with the Trick Switch?

6 Make: Basic Arduino Projects

Figure 1-5. Trick Switch circuit schematic diagram
Chapter 1: The Trick Switch 7





Figure 2-1. Sunrise-Sunset Light Switch circuit built on a full-size clear breadboard (the 100 uF
electrolytic capacitor and the red and green LED negative pins are wired to ground)

Let’s Build a Sunrise-Sunset Light
Switch

You can build a Sunrise-Sunset Light Switch by modifying the Trick Switch device
from Chapter 1. The main change you will make is to remove the mini pushbutton
and replace it with a photocell. You will also add a green LED to pin D13 of the
Arduino. Refer to the Parts List for all the electronic parts required for this project.
Here are the steps required to build the electronic device:

1. From the Ultimate Microcontroller Pack, place the required parts on your work-
bench or lab tabletop.

2. Wire the electronic parts using the Fritzing diagram of Figure 2-2 or the actual
Sunrise-Sunset Light Switch device shown in Figure 2-1.

3. Type Example 2-1 into the Arduino IDE.
4. Upload the Sunrise-Sunset sketch to the Arduino. The green LED will be on.

10 Make: Basic Arduino Projects

5. Wave your hand over the photocell for a moment. The red LED turns on. After
a few seconds, the red LED will turn off, and the green LED will turn on.

Figure 2-2. Sunrise-Sunset Light Switch Fritzing diagram
Example 2-1. Sunrise-Sunset Light Switch sketch
/*

Sunrise-Sunset Light Switch
Turns on and off a light-emitting diode (LED) connected to digital
pins 12 and 13 after 10 to 20 seconds, by waving a hand over a photocell
attached to pin 2.
23 Nov 2012
by Don Wilcher
*/
// constants won't change; they're used here to
// set pin numbers:
const int lightsensorPin = 2; // the number of the light sensor pin

Chapter 2: Sunrise-Sunset Light Switch 11

const int redledPin = 12; // the number of the red LED pin

const int greenledPin13 = 13; // onboard LED and green LED pin

// variables will change:

int sensorState = 0; // variable for reading light sensor status

void setup() {
// initialize the LED pins as outputs:
pinMode(redledPin, OUTPUT);
pinMode(greenledPin13, OUTPUT);
// initialize the light sensor pin as an input:
pinMode(lightsensorPin, INPUT);

}

void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(lightsensorPin);

// check if the light sensor is activated
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {

// turn red LED on:
digitalWrite(redledPin, HIGH);
// turn off onboard LED and green LED:
digitalWrite(greenledPin13, LOW);
}
else {
// turn red LED off:
digitalWrite(redledPin, LOW);
// turn on onboard LED and green LED;
digitalWrite(greenledPin13, HIGH);
}
}

Circuit Theory

The Sunrise-Sunset Light circuit operates like the Smart Switch, except you don’t
have to use a mini pushbutton to start the timing function. The mini pushbutton
has instead been replaced with a light sensor called a photocell. A photocell is a
variable resistor that changes its resistance based on the amount of light touching
its surface. Light falling on a photocell will decrease its resistance value. No light will
increase its resistance value. Figure 2-3 shows the resistor-capacitor (RC) timing
circuit with a photocell variable resistor symbol.

12 Make: Basic Arduino Projects





pinMode(greenledPin13, OUTPUT);
// initialize the light sensor pin as an input:
pinMode(lightsensorPin, INPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600); // Add code instruction here!
}

void loop(){
// read the state of the light sensor value:
sensorState = digitalRead(lightsensorPin);

// check if the light sensor is activated
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {

// turn red LED on:
digitalWrite(redledPin, HIGH);
// turn off onboard LED and green LED:
digitalWrite(greenledPin13, LOW);
// display message
Serial.println("Sunset\n"); // Add code instruction here!

}
else {

// turn red LED off:
digitalWrite(redledPin, LOW);
// turn on onboard LED and green LED;
digitalWrite(greenledPin13,HIGH);
// display message
Serial.println("Sunrise\n"); // Add code instruction here!
}
}

With the modifications made to the original sketch, upload it to the Arduino and
open the Serial Monitor. As you wave your hand over the photocell, you see the
messages “Sunrise” (no hand over the sensor) and “Sunset” (hand over the sensor)
displayed on the Serial Monitor. Figure 2-5 shows the two messages displayed on
the Serial Monitor.
Experiment with the location of the Sunrise-Sunset detector to obtain the best cir-
cuit response. Enjoy!
The block diagram in Figure 2-6 shows the electronic component blocks and the
electrical signal flow for the Sunrise-Sunset Light Switch. A Fritzing electronic circuit
schematic diagram of the switch is shown in Figure 2-7. Electronic circuit schematic
diagrams are used by electrical/electronic engineers to design and build cool elec-
tronic products for society.

Chapter 2: Sunrise-Sunset Light Switch 15



Figure 2-7. Sunrise-Sunset Light Switch circuit schematic diagram
Chapter 2: Sunrise-Sunset Light Switch 17





Figure 3-1. Tilt Sensing Servo Motor Controller built on a full-size clear breadboard

Let’s Build a Tilt Sensing Servo Motor
Controller

You can control a servo motor’s rotation direction through orientation detection
using a tilt control switch. In this project, you will build a Tilt Sensing Servo Motor
Controller. Refer to the Parts List for all the electronic components required for this
project. Here are the steps used to build the electronic device:

1. From the Ultimate Microcontroller Pack, place the required parts on your work-
bench or lab tabletop.

2. Assemble the servo motor with the appropriate mechanical assembly attach-
ment, as shown in Figure 3-2 (left).

3. Strip insulation from three ¼-inch solid wires and insert them into the servo
motor’s mini connector, as shown in Figure 3-2 (right).

20 Make: Basic Arduino Projects

Figure 3-2. Servo motor with mechanical assembly attachment and modified servo motor wire
connector (left); close-up of modified servo motor wire connector (right)

4. Place and secure the servo motor on the full-size clear breadboard with hookup
wire, as shown in Figure 3-3.

5. Insert the modified servo motor wire connector into the full-size clear bread-
board, as shown in Figure 3-4.

6. Wire the electronic parts using the Fritzing diagram of Figure 3-5, or the actual
project shown in Figure 3-1.

Chapter 3: Tilt Sensing Servo Motor Controller 21

Figure 3-3. Placing and securing the servo motor on the full-size clear breadboard
Figure 3-4. Modified servo motor wire connector inserted into the full-size clear breadboard
22 Make: Basic Arduino Projects





Figure 3-6. Digital data from tilt control switch: open tilt control switch (left), closed tilt control
switch (right)

Example 3-2. Tilt Control Switch with Serial Monitor

/* This sketch controls a servo motor using a tilt control switch!
* Serial Monitor displays digital data from Tilt Control Switch.
*
* 15 December 2012
* by Don Wilcher
*
*/

#include<Servo.h> // include Servo library
int inPin = 2; // the Arduino input pin tilt control switch is wired to D2
int reading; // the current reading from the input pin
Servo myservo; // create servo motor object

void setup()

{

myservo.attach(9); // attach servo motor to pin 9 of Arduino

pinMode(inPin, INPUT); // make pin 2 an input

Serial.begin(9600); // open communication port

}

void loop() // store digital data in variable
{ // check it against target value (HIGH)

reading = digitalRead(inPin);
if(reading == HIGH) {

myservo.write(90); // if digital data equals target value,

// servo motor rotates 90 degrees

Serial.println(reading); // print tilt control switch digital data

delay(15); // wait 15ms for rotation

}

else { // if it's not equal to target value...

Chapter 3: Tilt Sensing Servo Motor Controller 25



Figure 3-8. Tilt Sensing Servo Motor Controller circuit schematic diagram: orange wire (D9), red
wire (+5V), and brown wire (GND)

Circuit Theory

A tilt control switch is an electrical device used to detect orientation. Like using a
mini pushbutton and a light detector, a tilt control switch is another way to interact
with and control the Arduino.
The tilt control switch is a pair of small metal balls that make contact with pins and
close the circuit when the electrical device is held in an upright position. Figure 3-9
shows a typical tilt control switch. The tilt control switch can be wired to a resistor
to make an orientation detection sensor circuit.
Figure 3-10 shows an orientation detection sensor circuit and its electrical operating
conditions. The Arduino’s D2 pin is wired to the 1KΩ resistor in order to receive either
a zero or five volt control signal, based on the tilt control switch orientation. With
the tilt control switch pins open, the voltage across the 1KΩ resistor is zero volts.
When the switch pins are closed, the 1KΩ resistor has a five volt signal across it.

Chapter 3: Tilt Sensing Servo Motor Controller 27





Figure 4-1. Variety of LEDs
Figure 4-2. Twin LEDs block diagram
30 Make: Basic Arduino Projects

Circuit Theory

An LED is an electronic part that emits light when properly wired in an electric circuit.
The LED has positive and negative leads protruding through a plastic body, as shown
in Figure 4-1. You can use the Arduino in electronic projects to operate multiple
LEDs. Figure 4-3 shows two LEDs wired to the Arduino D13 pin. The Arduino output
pins are capable of providing 40 mA (milliamperes) of electrical current, sufficient
to turn on two LED circuits wired in parallel.

Figure 4-3. Two LED circuits wired in parallel to the Arduino D13 pin; the arrows indicate the
LEDs are on

Twin LED Flasher

The circuit theory diagram shown in Figure 4-3 can easily be converted into a cool
electronic gadget. You can build a Twin LED Flasher using an Arduino, two 330 ohm
resistors, and LEDs, as shown in Figure 4-4. The Twin LED Flasher circuit schematic
diagram is shown in Figure 4-5. To make the flasher device compact, you can build
it on the MakerShield, as shown in Figure 4-6. Uploading the Blink sketch to the
Arduino allows you to test the MakerShield and the Twin LED Flasher. The Blink
sketch for the electronic flasher is shown in Example 4-1.

Chapter 4: Twin LEDs 31





Figure 4-6. MakerShield Twin LED Flasher

Example 4-1. Blink sketch

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {

// initialize the digital pin as an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

34 Make: Basic Arduino Projects

Build the Adjustable Twin LED Flasher

To make the Adjustable Twin LED Flasher, simply add a 10K ohm potentiometer to
the device. The flash rate can be adjusted to make the on/off toggling slower or
faster. The Fritzing diagram in Figure 4-7 along with the circuit schematic diagram
shown in Figure 4-8 will allow you to build the Adjustable Twin LED Flasher. The
MakerShield Adjustable Twin LED Flasher is shown in Figure 4-9 and the Adjustable
Twin LED Flasher sketch is shown in Example 4-2.

Figure 4-7. Adjustable Twin LED Flasher Fritzing diagram
Chapter 4: Twin LEDs 35

Figure 4-8. Adjustable Twin LED Flasher circuit schematic diagram

Example 4-2. Adjustable Twin LED Flasher sketch
/*

Adjustable Twin LED Flasher
Two LEDs will flash at a specified rate
based on the 10K potentiometer setting.

01 Jan 2013
by Don Wilcher

*/

// Two LEDs with 330 ohm series resistors wired
// in parallel connected to pin 9.
int led = 9; // pin D9 assigned to led variable.

// A 10K potentiometer center pin wired to pin A0.
// One pin is wired to +5V with the other connected to GND.
int PotIn = A0; // pin A0 assigned to PotIn variable.

int Flash; // Flash variable to be used with "delay" instruction.

// the setup routine runs once when you press reset:
void setup() {

36 Make: Basic Arduino Projects


Click to View FlipBook Version
Previous Book
Make Magazine - Volume 19
Next Book
2016BBAHOF_Final