Arduino Workshop A Hands On Introduction with 65 Projects 1st Edition John Boxall - Read the ebook online or download it to own the complete version
Arduino Workshop A Hands On Introduction with 65 Projects 1st Edition John Boxall - Read the ebook online or download it to own the complete version
https://ebookfinal.com/download/arduino-robotic-projects-1st-edition-
grimmett/
https://ebookfinal.com/download/a-hands-on-introduction-to-machine-
learning-1st-edition-shah/
https://ebookfinal.com/download/count-like-an-egyptian-a-hands-on-
introduction-to-ancient-mathematics-1st-edition-reimer/
Arduino Projects to Save the World 1st Edition Emery
Premeaux
https://ebookfinal.com/download/arduino-projects-to-save-the-
world-1st-edition-emery-premeaux/
https://ebookfinal.com/download/30-arduino-projects-for-the-evil-
genius-1st-edition-simon-monk/
https://ebookfinal.com/download/arduino-robotics-1st-edition-john-
david-warren/
https://ebookfinal.com/download/microservices-for-java-developers-a-
hands-on-introduction-to-frameworks-and-containers-first-edition-
edition-posta/
https://ebookfinal.com/download/warhammer-40-000-codex-supplement-
iron-hands-8th-edition-games-workshop/
Arduino Workshop A Hands On Introduction with 65
Projects 1st Edition John Boxall Digital Instant Download
Author(s): John Boxall
ISBN(s): 9781593274481, 1593274483
Edition: 1st
File Details: PDF, 10.14 MB
Year: 2013
Language: english
ARDUINO
LEARN THE BASICS,
BUILD THE PROJECTS,
CREATE YOUR OWN
WORKSHOP
ARDUINO WORKSHOP
The Arduino is a cheap, flexible, open source micro- • A handy tester that lets you check the voltage of any A HANDS-ON INTRODUCTION
controller platform designed to make it easy for hobbyists single-cell battery
W I T H 65 PROJECTS
to use electronics in homemade projects. With an almost • A keypad-controlled lock that requires a secret code
unlimited range of input and output add-ons, sensors, indi- to open
cators, displays, motors, and more, the Arduino offers you
countless ways to create devices that interact with the You’ll also learn to build Arduino toys and games like: JOHN BOXALL
world around you. • An electronic version of the classic six-sided die
In Arduino Workshop, you’ll learn how these add-ons • A binary quiz game that challenges your number
work and how to integrate them into your own projects. conversion skills
You’ll start off with an overview of the Arduino system
but quickly move on to coverage of various electronic • A motorized remote control tank with collision detection
components and concepts. Hands-on projects throughout to keep it from crashing
the book reinforce what you’ve learned and show you Arduino Workshop will teach you the tricks and design
how to apply that knowledge. As your understanding grows, principles of a master craftsman. Whatever your skill level,
the projects increase in complexity and sophistication. you’ll have fun as you learn to harness the power of the
Among the book’s 65 projects are useful devices like: Arduino for your own DIY projects.
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
BOXALL
by John Boxall
San Francisco
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
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
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 infor-
mation contained in it.
For the two people who have always believed in me:
my mother and my dearest Kathleen
Brief Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
viii Brief Contents
Conte nt s in De ta il
Acknowledgments xix
1
Getting Started 1
The Possibilities Are Endless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Strength in Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Parts and Accessories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Required Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Windows XP and Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Ubuntu Linux 9.04 and Later . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2
Exploring the Arduino Board and the IDE 19
The Arduino Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Taking a Look Around the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Command Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Text Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Message Window Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating Your First Sketch in the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Setup Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Controlling the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
The Loop Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Verifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Uploading and Running Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Modifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3
First Steps 33
Planning Your Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
About Electricity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Current . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Electronic Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Light-Emitting Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
The Solderless Breadboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Project #1: Creating a Blinking LED Wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Project #2: Repeating with for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Varying LED Brightness with Pulse-Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Project #3: Demonstrating PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
More Electric Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
The Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
The Rectifier Diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
The Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Higher-Voltage Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4
Building Blocks 55
Using Schematic Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Identifying Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Wires in Schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Dissecting a Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Measuring the Capacity of a Capacitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Reading Capacitor Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Types of Capacitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Digital Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Project #4: Demonstrating a Digital Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Modifying Your Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Understanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Creating Constants with #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Reading Digital Input Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Making Decisions with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Making More Decisions with if-then-else . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Making Two or More Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Project #5: Controlling Traffic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Running the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Analog vs. Digital Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Project #6: Creating a Single-Cell Battery Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
x Contents in Detail
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Doing Arithmetic with an Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Float Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Comparison Operators for Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Improving Analog Measurement Precision with a Reference Voltage . . . . . . . . . . . . . . . 84
Using an External Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Using the Internal Reference Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The Variable Resistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Piezoelectric Buzzers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Piezo Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Project #7: Trying Out a Piezo Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Project #8: Creating a Quick-Read Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Hacking the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5
Working with Functions 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 111
Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Using Ambient Current to Generate a Random Number . . . . . . . . . . . . . . . . 112
Project #15: Creating an Electronic Die . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Modifying the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Contents in Detail xi
A Quick Course in Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Byte Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Increasing Digital Outputs with Shift Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Project #16: Creating an LED Binary Number Display . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Connecting the 74HC595 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Project #17: Making a Binary Quiz Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Referring to Values in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Writing to and Reading from Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Seven-Segment LED Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Controlling the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Project #18: Creating a Single-Digit Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Displaying Double Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Project #19: Controlling Two Seven-Segment LED Display Modules . . . . . . . . . . . . . . . . 131
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Modulo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Project #20: Creating a Digital Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
LED Matrix Display Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
The LED Matrix Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Making the Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Bitwise Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
The Bitwise AND Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
The Bitwise OR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
The Bitwise XOR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
The Bitwise NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Bitshift Left and Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Project #21: Creating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Project #22: Creating Images on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Project #23: Displaying an Image on an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Project #24: Animating an LED Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
7
Liquid Crystal Displays 147
Character LCD Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Using a Character LCD in a Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Displaying Variables or Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
8
Expanding Your Arduino 161
Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
ProtoShields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Project #28: Creating a Custom Shield with Eight LEDs . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Layout of the ProtoShield Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Soldering the Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Modifying the Custom Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Expanding Sketches with Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Importing a Shield’s Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
MicroSD Memory Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Testing Your MicroSD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Project #29: Writing Data to the Memory Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Project #30: Creating a Temperature-Logging Device . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Timing Applications with millis() and micros() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Project #31: Creating a Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Interrupt Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Configuring Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Activating or Deactivating Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Project #32: Using Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
10
Accepting User Input with Touchscreens 195
Touchscreens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Connecting the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Project #34: Addressing Areas on the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Mapping the Touchscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Project #35: Creating a Two-Zone On/Off Touch Switch . . . . . . . . . . . . . . . . . . . . . . . . 200
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Testing the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Project #36: Creating a Three-Zone Touch Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
The Touchscreen Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11
Meet the Arduino Family 207
Project #37: Creating Your Own Breadboard Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . 208
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Running a Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
The Many Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Freetronics Eleven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
The Freeduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
The Boarduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
The Arduino Nano . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
The Arduino LilyPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
The Arduino Mega 2560 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
The Freetronics EtherMega . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
The Arduino Due . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
13
Using GPS with Your Arduino 257
What Is GPS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Testing the GPS Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Project #44: Creating a Simple GPS Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Displaying the Position on the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Project #45: Creating an Accurate GPS-based Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Contents in Detail xv
Project #46: Recording the Position of a Moving Object over Time . . . . . . . . . . . . . . . . . 265
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Displaying Locations on a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
14
Wireless Data 271
Using Low-cost Wireless Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Project #47: Creating a Wireless Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
The Hardware for the Transmitter Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
The Transmitter Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
The Hardware for the Receiver Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
The Receiver Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
The Transmitter Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Receiver Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Using XBee Wireless Data Modules for Greater Range and Faster Speed . . . . . . . . . . 277
Project #48: Transmitting Data with an XBee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Setting Up the Computer to Receive Data . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Project #49: Building a Remote Control Thermometer . . . . . . . . . . . . . . . . . . . . . . . . . . 281
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
The Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
15
Infrared Remote Control 285
What Is Infrared? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Setting Up for Infrared . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
The IR Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
The Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
A Test Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Testing the Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Project #50: Creating an IR Remote Control Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Expanding the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Project #51: Creating an IR Remote Control Tank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
16
Reading RFID Tags 295
Inside RFID Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Testing the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Testing the Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
17
Data Buses 307
2
The I C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Project #54: Using an External EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
The Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Project #55: Using a Port Expander IC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
The SPI Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Pin Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Implementing the SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Sending Data to an SPI Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Project #56: Using a Digital Rheostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
18
Real-time Clocks 321
Connecting the RTC Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Project #57: Adding and Displaying Time and Date with an RTC . . . . . . . . . . . . . . . . . . . 322
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Project #58: Creating a Simple Digital Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
How It Works and Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Project #59: Creating an RFID Time-Clock System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
20
Cellular Communications 349
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Preparing the Power Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Hardware Configuration and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Changing the Operating Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Project #63: Building an Arduino Dialer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Project #64: Building an Arduino Texter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Project #65: Setting Up an SMS Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
The Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Looking Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Index 365
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com