Tải bản đầy đủ (.pdf) (394 trang)

Arduino workshop a hands on introduction with 65 projects by john boxall

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.86 MB, 394 trang )

LEARN THE BASICS,
BUILD THE PROJECTS,

The Arduino is a cheap, flexible, open source microcontroller platform designed to make it easy for hobbyists
to use electronics in homemade projects. With an almost
unlimited range of input and output add-ons, sensors, indicators, displays, motors, and more, the Arduino offers you
countless ways to create devices that interact with the
world around you.
In Arduino Workshop, you’ll learn how these add-ons
work and how to integrate them into your own projects.
You’ll start off with an overview of the Arduino system
but quickly move on to coverage of various electronic
components and concepts. Hands-on projects throughout
the book reinforce what you’ve learned and show you
how to apply that knowledge. As your understanding grows,
the projects increase in complexity and sophistication.
Among the book’s 65 projects are useful devices like:

• A handy tester that lets you check the voltage of any
single-cell battery
• A keypad-controlled lock that requires a secret code
to open
You’ll also learn to build Arduino toys and games like:
• An electronic version of the classic six-sided die
• A binary quiz game that challenges your number
conversion skills
• A motorized remote control tank with collision detection
to keep it from crashing
Arduino Workshop will teach you the tricks and design
principles of a master craftsman. Whatever your skill level,
you’ll have fun as you learn to harness the power of the


Arduino for your own DIY projects.

• A digital thermometer that charts temperature changes
on an LCD

ABOUT THE AUTHOR

• A GPS logger that records data from your travels, which
can be displayed on Google Maps

John Boxall ( ) has been
writing Arduino tutorials, projects, and kit and accessory
reviews for years. Arduino Workshop is his first book.

ARDUINO WORKSHOP

CREATE YOUR OWN

T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
w w w.nostarch.com

$29.95 ($31.95 CDN)
SHELVE IN:
HARDWARE/ELECTRONICS

BOXALL

“ I L I E F L AT .”
This book uses RepKover — a durable binding that won’t snap shut.


ARDUINO

WORKSHOP
A

HANDS-ON INTRODUCTION
W I T H 65 PROJECTS
JOHN BOXALL


www.electronicbo.com

Arduino Workshop



A Hands-On Introduction
with 65 Projects

by John Boxall

San Francisco

www.electronicbo.com

Ar d ui n o
Wo r k s h o p


Arduino Workshop. Copyright © 2013 by John Boxall.

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the
prior written permission of the copyright owner and the publisher.
Printed in USA
First printing
17 16 15 14 13   1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-448-3
ISBN-13: 978-1-59327-448-1
Publisher: William Pollock
Production Editor: Serena Yang
Cover Illustration: Charlie Wylie
Interior Design: Octopod Studios
Developmental Editor: William Pollock
Technical Reviewer: Marc Alexander
Copyeditor: Lisa Theobald
Compositor: Susan Glinert Stevens
Proofreader: Emelie Battaglia
Circuit diagrams made using Fritzing ( />For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
38 Ringold Street, San Francisco, CA 94103
phone: 415.863.9900; fax: 415.863.9950; ; www.nostarch.com
Library of Congress Cataloging-in-Publication Data
A catalog record of this book is available from the Library of Congress.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and
company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark
symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to
the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been
taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.



www.electronicbo.com

For the two people who have always believed in me:
my mother and my dearest Kathleen



Brief Contents

Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Exploring the Arduino Board and the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 3: First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Chapter 4: Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Chapter 5: Working with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter 6: Numbers, Variables, and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter 7: Liquid Crystal Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Chapter 8: Expanding Your Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Chapter 9: Numeric Keypads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Chapter 10: Accepting User Input with Touchscreens . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Chapter 11: Meet the Arduino Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Chapter 12: Motors and Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Chapter 13: Using GPS with Your Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Chapter 14: Wireless Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Chapter 15: Infrared Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Chapter 16: Reading RFID Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

www.electronicbo.com


Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix


Chapter 17: Data Buses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Chapter 18: Real-time Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Chapter 19: The Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Chapter 20: Cellular Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

viii   Brief Contents


Conte nt s in De ta il
Acknowledgments

xix

1
Getting Started

1
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

2
Exploring the Arduino Board and the IDE
The Arduino Board . . . . . . . . . . . . . . . . . . . .
Taking a Look Around the IDE . . . . . . . . . . . . .
The Command Area . . . . . . . . . . . . .
The Text Area . . . . . . . . . . . . . . . . .
The Message Window Area . . . . . . .
Creating Your First Sketch in the IDE . . . . . . . .
Comments . . . . . . . . . . . . . . . . . . . .
The Setup Function . . . . . . . . . . . . . .
Controlling the Hardware . . . . . . . . .
The Loop Function . . . . . . . . . . . . . .
Verifying Your Sketch . . . . . . . . . . . .
Uploading and Running Your Sketch .
Modifying Your Sketch . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . .

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.

. 2
. 6
. 6
. 7
. 7
11
15
18
18

19

3
First Steps
Planning Your Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
About Electricity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Current . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Electronic Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Light-Emitting Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Solderless Breadboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #1: Creating a Blinking LED Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19
25
25
26
26
27
27
28
28
28
30
31
31
31

33
34
34
34
35

35
35
35
39
41
43
43
43

www.electronicbo.com

The Possibilities Are Endless . . . . . . . .
Strength in Numbers . . . . . . . . . . . . .
Parts and Accessories . . . . . . . . . . . .
Required Software . . . . . . . . . . . . . . .
Mac OS X . . . . . . . . . . . . . .
Windows XP and Later . . . . .
Ubuntu Linux 9.04 and Later .
Safety . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . .


The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #2: Repeating with for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Varying LED Brightness with Pulse-Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #3: Demonstrating PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Electric Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Rectifier Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Higher-Voltage Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4
Building Blocks
Using Schematic Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Identifying Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wires in Schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dissecting a Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Measuring the Capacity of a Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Capacitor Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of Capacitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #4: Demonstrating a Digital Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Constants with #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading Digital Input Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making Decisions with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making More Decisions with if-then-else . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Making Two or More Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #5: Controlling Traffic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Analog vs. Digital Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #6: Creating a Single-Cell Battery Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
x 

Contents in Detail

43
44
45
45
46
47
49
49
50
50
51
52
53


55
56
56
58
59
60
60
61
61
63
65
65
65
65
69
70
70
70
70
71
71
72
72
73
74
74
74
75
75

76
79
79
80
81
81
81


5
Working with Functions

81
82
83
84
84
84
85
86
86
87
88
88
90
90
90
91
91
93

93

www.electronicbo.com

The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Doing Arithmetic with an Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Float Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Comparison Operators for Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Improving Analog Measurement Precision with a Reference Voltage . . . . . . . . . . . . . . .
Using an External Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Internal Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Variable Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Piezoelectric Buzzers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Piezo Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #7: Trying Out a Piezo Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #8: Creating a Quick-Read Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hacking the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

95

Project #9: Creating a Function to Repeat an Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Project #10: Creating a Function to Set the Number of Blinks . . . . . . . . . . . . . . . . . . . . . . 97
Creating a Function to Return a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Project #11: Creating a Quick-Read Thermometer That Blinks the Temperature . . . . . . . . . 98

The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Displaying Data from the Arduino in the Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . 101
The Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Project #12: Displaying the Temperature in the Serial Monitor . . . . . . . . . . . . . . . . . . . . 103
Debugging with the Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Making Decisions with while Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Sending Data from the Serial Monitor to the Arduino . . . . . . . . . . . . . . . . . . . . . . . . . 106
Project #13: Multiplying a Number by Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
long Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Project #14: Using long Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

6
Numbers, Variables, and Arithmetic
Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Ambient Current to Generate a Random Number . . . . . . . . . . . . . . . .
Project #15: Creating an Electronic Die . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

111
112
112
113
114

114
115
116

Contents in Detail 

xi


A Quick Course in Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Byte Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Increasing Digital Outputs with Shift Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #16: Creating an LED Binary Number Display . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting the 74HC595 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #17: Making a Binary Quiz Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Referring to Values in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing to and Reading from Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seven-Segment LED Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #18: Creating a Single-Digit Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Double Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Project #19: Controlling Two Seven-Segment LED Display Modules . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #20: Creating a Digital Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LED Matrix Display Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The LED Matrix Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making the Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bitwise Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise AND Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise OR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise XOR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Bitwise NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bitshift Left and Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #21: Creating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #22: Creating Images on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #23: Displaying an Image on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #24: Animating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7
Liquid Crystal Displays
Character LCD Modules . . . . . . . . . . . . . . . .
Using a Character LCD in a Sketch .
Displaying Text . . . . . . . . . . . . . . .
Displaying Variables or Numbers . . .


xii 

Contents in Detail

116
117
118
119
119
119
121
122
122
122
124
124
125
125
126
127
129
129
129
130
131
131
131
132
133
134

134
134
135
136
137
139
139
139
140
140
140
141
142
144
145
145
146

147
.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.

.
.
.

.
.
.
.

.
.
.
.

148
149
150
151


8
Expanding Your Arduino
Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ProtoShields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #28: Creating a Custom Shield with Eight LEDs . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Layout of the ProtoShield Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Soldering the Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Modifying the Custom Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Expanding Sketches with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Importing a Shield’s Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MicroSD Memory Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing Your MicroSD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #29: Writing Data to the Memory Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #30: Creating a Temperature-Logging Device . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Timing Applications with millis() and micros() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #31: Creating a Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interrupt Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Configuring Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Activating or Deactivating Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #32: Using Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

152
153
154
155
155
155
156
157

158
158
158
160
160
160

161
162
164
165
165
165
166
166
167
169
169
169
173
174
175
177
177
177
179
181
181
181
182

184
184
185
185
185
185
186

Contents in Detail 

www.electronicbo.com

Project #25: Defining Custom Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Graphic LCD Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting the Graphic LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling the Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #26: Seeing the Text Functions in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating More Complex Display Effects . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #27: Creating a Temperature History Monitor . . . . . . . . . . . . . . . . . . . . . . . . . .
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modifying the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xiii



9
Numeric Keypads
Using a Numeric Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wiring a Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Programming for the Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Making Decisions with switch-case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #33: Creating a Keypad-Controlled Lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10
Accepting User Input with Touchscreens
Touchscreens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #34: Addressing Areas on the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mapping the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #35: Creating a Two-Zone On/Off Touch Switch . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #36: Creating a Three-Zone Touch Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Touchscreen Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11
Meet the Arduino Family
Project #37: Creating Your Own Breadboard Arduino . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running a Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Many Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Freetronics Eleven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Freeduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Boarduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Arduino Nano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Arduino LilyPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Arduino Mega 2560 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Freetronics EtherMega . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Arduino Due . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xiv 

Contents in Detail

187
187
188
189
189
190

190
191
192
193
193

195
195
196
197
197
197
198
199
200
200
202
202
202
203
203
205
205

207
208
208
211
214
217

219
219
220
220
221
221
222
222
223
224


Making Small Motions with Servos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Selecting a Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting a Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Putting a Servo to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #38: Building an Analog Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TIP120 Darlington Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #39: Controlling the Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #40: Building and Controlling a Tank Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Sensing Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #41: Detecting Tank Bot Collisions with a Microswitch . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Infrared Distance Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Wiring It Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the IR Distance Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #42: Detecting Tank Bot Collisions with IR Distance Sensor . . . . . . . . . . . . . . . . .
Ultrasonic Distance Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Connecting the Ultrasonic Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Ultrasonic Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the Ultrasonic Distance Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #43: Detecting Tank Bot Collisions with an Ultrasonic Distance Sensor . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13
Using GPS with Your Arduino
What Is GPS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Testing the GPS Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #44: Creating a Simple GPS Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying the Position on the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #45: Creating an Accurate GPS-based Clock . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

225
225

226
227
227
228
228
229
229
231
231
232
232
233
234
235
235
238
240
243
243
243
244
246
247
247
249
251
252
252
252
254

254
256

www.electronicbo.com

12
Motors and Movement

257
258
259
261
261
261
262
263
263
264

Contents in Detail 

xv


Project #46: Recording the Position of a Moving Object over Time . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying Locations on a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


14
Wireless Data

271

Using Low-cost Wireless Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #47: Creating a Wireless Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware for the Transmitter Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Transmitter Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware for the Receiver Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Receiver Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Transmitter Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Receiver Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using XBee Wireless Data Modules for Greater Range and Faster Speed . . . . . . . . . .
Project #48: Transmitting Data with an XBee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up the Computer to Receive Data . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #49: Building a Remote Control Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15
Infrared Remote Control

16
Reading RFID Tags


xvi 

Contents in Detail

271
272
273
273
274
274
275
276
277
279
279
279
281
281
281
282
283
284

285

What Is Infrared? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Up for Infrared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The IR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Testing the Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #50: Creating an IR Remote Control Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Expanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #51: Creating an IR Remote Control Tank . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Inside RFID Devices . . . . . . . . .
Testing the Hardware . . . . . . .
The Schematic . . . . . .
Testing the Schematic .

265
265
266
268
269

285
286
286
287
287
288
289
289
289

290
291
291
291
293

295
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

296
297
297
297


17
Data Buses

2

The I C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #54: Using an External EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #55: Using a Port Expander IC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The SPI Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pin Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing the SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Data to an SPI Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #56: Using a Digital Rheostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18
Real-time Clocks
Connecting the RTC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #57: Adding and Displaying Time and Date with an RTC . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #58: Creating a Simple Digital Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works and Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #59: Creating an RFID Time-Clock System . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

299
299
300
301
302
303
303
306
306

307
308
309
309
310
311
312
313
313
313
314

315
316
316
317
318
318
318
319
320

www.electronicbo.com

Project #52: Creating a Simple RFID Control System . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Storing Data in the Arduino’s Built-in EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading and Writing to the EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #53: Creating an RFID Control with “Last Action” Memory . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

321
322
322
322
323
325
326
326

327
330
330
331
331
335
336

Contents in Detail 

xvii


19
The Internet
What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #60: Building a Remote-Monitoring Station . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #61: Creating an Arduino Tweeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Your Arduino from the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #62: Setting Up a Remote Control for Your Arduino . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling Your Arduino Remotely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


20
Cellular Communications
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preparing the Power Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hardware Configuration and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing the Operating Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #63: Building an Arduino Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #64: Building an Arduino Texter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Project #65: Setting Up an SMS Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

337
337
339
339
339
341
342
343

343
343
344
345
345
346
347
348

349
350
351
352
354
356
356
356
357
358
358
359
359
360
360
361
361
363
364

Index365


xviii 

Contents in Detail


First of all, a huge thank you to the Arduino team:
Massimo Banzi, David Cuartielles, Tom Igoe,
Gianluca Martino, and David Mellis. Without your
vision, thought, and hard work, none of this would
have been possible.
Many thanks to my technical reviewer Marc Alexander for his contributions, expertise, suggestions, support, thoughts, and long conversations,
and for having the tenacity to follow through with such a large project.
I also want to thank the following organizations for their images and
encouragement: adafruit industries, Agilent Technologies, Gravitech,
Freetronics, Oomlout, Seeed Studio, Sharp Corporation, and SparkFun.
Furthermore, a big thanks to Freetronics for the use of their excellent
hardware products. And thank you to all those who have contributed their
time making Arduino libraries, which makes life much easier for everyone.
Kudos and thanks to the Fritzing team for their wonderful open source
circuit schematic design tool, which I’ve used throughout this book.

www.electronicbo.com

Acknowledgments


And a thank you to the following people (in no particular order) from
whom I’ve received encouragement, inspiration and support: Iraphne
Childs, Limor Fried, Jonathan Oxer, Philip Lindsay, Nicole Kilah, Ken

Shirriff, Nathan Kennedy, David Jones, and Nathan Seidle.
Finally, thank you to everyone at No Starch Press, including Sondra
Silverhawk for suggesting the book; Serena Yang for her dedicated editing,
endless patience, and suggestions; and Bill Pollock for his support and guidance and for convincing me that sometimes there is a better way to explain
something.

xx   Acknowledgments


Ge t ting S ta r te d

Have you ever looked at some gadget and wondered
how it really worked? Maybe it was a remote control
boat, the system that controls an elevator, a vending
machine, or an electronic toy? Or have you wanted
to create your own robot or electronic signals for a model railroad, or perhaps you’d like to capture and analyze weather data over time? Where and
how do you start?
The Arduino board (shown in Figure 1-1) can help you find some of
the answers to the mysteries of electronics in a hands-on way. The original
creation of Massimo Banzi and David Cuartielles, the Arduino system offers
an inexpensive way to build interactive projects, such as remote-controlled
robots, GPS tracking systems, and electronic games.
The Arduino project has grown exponentially since its introduction
in 2005. It’s now a thriving industry, supported by a community of people
united with the common bond of creating something new. You’ll find both
individuals and groups, ranging from interest groups and clubs to local
hackerspaces and educational institutions, all interested in toying with
the Arduino.

www.electronicbo.com


1


Figure 1-1: The Arduino board

To get a sense of the variety of Arduino projects in the wild, you can
simply search the Internet. You’ll find a list of groups offering introductory
programs and courses with like-minded, creative people.

The Possibilities Are Endless
A quick scan through this book will show you that you can use the Arduino
to do something as simple as blinking a small light, or even something
more complicated, such as interacting with a cellular phone—and many
different things in between.
For example, have a look at Philip Lindsay’s device, shown in Figure 1-2.
It can receive text messages from cellular phones and display them on a
large sign for use in dance halls. This device uses an Arduino board and a
cellular phone shield to receive text messages from other phones (similar
to Project 65). The text message is sent to a pair of large, inexpensive dotmatrix displays for everyone to see.

Figure 1-2: SMS (short message service) text marquee

2   Chapter 1


www.electronicbo.com

You can purchase large display boards that are easy to interface
with an Arduino, so you don’t have to make your own display from

scratch. (For more information, visit />p/project-sms-text-scroller.)
How about creating a unique marriage proposal? Tyler Cooper wanted
an original way to propose to his girlfriend, so he built what he calls a
“reverse geocache box”—a small box that contained an engagement ring, as
shown in Figure 1-3. When the box was taken to a certain area (measured
by the internal GPS), it unlocked to reveal a romantic message and the
ring. You can easily reproduce this device using an Arduino board, a GPS
receiver, and an LCD module (as used in Chapter 13), with a small servo
motor that acts as a latch to keep the box closed until it’s in the correct
location. The code required to create this is quite simple—something you
could create in a few hours. The most time-consuming part is choosing the
appropriate box in which to enclose the system. (For more information,
visit />
Figure 1-3: Marriage proposal via Arduino

Here’s another example. Kurt Schulz was interested in monitoring
the battery charge level of his moped. However, after realizing how simple
it is to work with Arduino, his project morphed into what he calls the
“Scooterputer”: a complete moped management system. The Scooterputer
can measure the battery voltage, plus it can display the speed, distance
traveled, tilt angle, temperature, time, date, GPS position, and more. It also
contains a cellular phone shield that can be controlled remotely, allowing remote tracking of the moped and engine shutdown in case it’s stolen.
The entire system can be controlled with a small touchscreen, shown in

Getting Started   3


Figure 1-4. Each feature can be considered a simple building block, and
anyone could create a similar system in a couple of weekends. (See http://
www.janspace.com/b2evolution/arduino.php/2010/06/26/scooterputer/.)


Figure 1-4: The Scooterputer display (courtesy of Kurt Schulz)

Then there’s John Sarik, who enjoys the popular Sudoku math puzzles;
he also likes working with Nixie numeric display tubes. With those two
drivers in mind, John created a huge 81-digit Sudoku game computer!
The user can play a full 9-by-9 game, with the Arduino in control of the
digits and checking for valid entries. Although this project might be considered a more advanced type, it is certainly achievable and the electronics are
not complex. The device is quite large and looks great mounted on a wall,
as shown in Figure 1-5. (See />nixie-sudoku/.)
The team at Oomlout even used the Arduino to create a TwypeWriter.
They fitted an Arduino board with an Ethernet shield interface connected
to the Internet, which searches Twitter for particular keywords. When a
keyword is found, the tweet is sent to an electric typewriter for printing. The
Arduino board is connected to the typewriter’s keyboard circuit, which
allows it to emulate a real person typing, as shown in Figure 1-6. (See
/>These are only a few random examples of what is possible using an
Arduino. You can create your own projects without much difficulty—and after
you’ve worked through this book, they are certainly not out of your reach.

4   Chapter 1


×