- •Acknowledgments
- •Preface
- •Who Should Read This Book
- •What’s in This Book
- •Arduino Uno and the Arduino Platform
- •Code Examples and Conventions
- •Online Resources
- •The Parts You Need
- •Starter Packs
- •Complete Parts List
- •1. Welcome to the Arduino
- •What You Need
- •What Exactly Is an Arduino?
- •Exploring the Arduino Board
- •Installing the Arduino IDE
- •Meeting the Arduino IDE
- •Hello, World!
- •Compiling and Uploading Programs
- •What If It Doesn’t Work?
- •Exercises
- •2. Creating Bigger Projects with the Arduino
- •What You Need
- •Managing Projects and Sketches
- •Changing Preferences
- •Using Serial Ports
- •What If It Doesn’t Work?
- •Exercises
- •3. Building Binary Dice
- •What You Need
- •Working with Breadboards
- •Using an LED on a Breadboard
- •First Version of a Binary Die
- •Working with Buttons
- •Adding Your Own Button
- •Building a Dice Game
- •What If It Doesn’t Work?
- •Exercises
- •4. Building a Morse Code Generator Library
- •What You Need
- •Learning the Basics of Morse Code
- •Building a Morse Code Generator
- •Fleshing Out the Morse Code Generator’s Interface
- •Outputting Morse Code Symbols
- •Installing and Using the Telegraph Class
- •Publishing Your Own Library
- •What If It Doesn’t Work?
- •Exercises
- •5. Sensing the World Around Us
- •What You Need
- •Measuring Distances with an Ultrasonic Sensor
- •Increasing Precision Using Floating-Point Numbers
- •Increasing Precision Using a Temperature Sensor
- •Creating Your Own Dashboard
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Wiring Up the Accelerometer
- •Bringing Your Accelerometer to Life
- •Finding and Polishing Edge Values
- •Building Your Own Game Controller
- •More Projects
- •What If It Doesn’t Work?
- •Exercises
- •7. Writing a Game for the Motion-Sensing Game Controller
- •Writing a GameController Class
- •Creating the Game
- •What If It Doesn’t Work?
- •Exercises
- •8. Generating Video Signals with an Arduino
- •What You Need
- •How Analog Video Works
- •Building a Digital-to-Analog Converter (DAC)
- •Connecting the Arduino to Your TV Set
- •Using the TVout Library
- •Building a TV Thermometer
- •Working with Graphics in TVout
- •What If It Doesn’t Work?
- •Exercises
- •9. Tinkering with the Wii Nunchuk
- •What You Need
- •Wiring a Wii Nunchuk
- •Talking to a Nunchuk
- •Building a Nunchuk Class
- •Using Our Nunchuk Class
- •Creating Your Own Video Game Console
- •Creating Your Own Video Game
- •What If It Doesn’t Work?
- •Exercises
- •10. Networking with Arduino
- •What You Need
- •Using Your PC to Transfer Sensor Data to the Internet
- •Registering an Application with Twitter
- •Tweeting Messages with Processing
- •Communicating Over Networks Using an Ethernet Shield
- •Using DHCP and DNS
- •What If It Doesn’t Work?
- •Exercises
- •11. Creating a Burglar Alarm with Email Notification
- •What You Need
- •Emailing from the Command Line
- •Emailing Directly from an Arduino
- •Detecting Motion Using a Passive Infrared Sensor
- •Bringing It All Together
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Understanding Infrared Remote Controls
- •Grabbing Remote Control Codes
- •Cloning a Remote
- •Controlling Infrared Devices Remotely with Your Browser
- •Building an Infrared Proxy
- •What If It Doesn’t Work?
- •Exercises
- •13. Controlling Motors with Arduino
- •What You Need
- •Introducing Motors
- •First Steps with a Servo Motor
- •Building a Blaminatr
- •What If It Doesn’t Work?
- •Exercises
- •Current, Voltage, and Resistance
- •Electrical Circuits
- •Learning How to Use a Wire Cutter
- •Learning How to Solder
- •Learning How to Desolder
- •The Arduino Programming Language
- •Bit Operations
- •Learning More About Serial Communication
- •Serial Communication Using Various Languages
- •What Are Google Chrome Apps?
- •Creating a Minimal Chrome App
- •Starting the Chrome App
- •Exploring the Chrome Serial API
- •Writing a SerialDevice Class
- •Index
Early praise for Arduino: A Quick-Start Guide, Second Edition
Buy this book only if you don’t mind being sucked into an amazing world of Arduino hacking, programming, games, controllers, motors, tweeting, networking, and lots of other mind-blowing things!
Kevin Beam
Software engineer, National Snow and Ice Data Center (NSIDC)
Maik Schmidt’s writing style is engaging and makes complex concepts accessible. When I finished the book, I was daydreaming about future Arduino projects I could create.
Matthew Sullivan
Senior Rails/Ruby developer, Paradigmisr
A very well-written, thorough introduction to the Arduino platform. The second edition is a nice refinement of the first, with much updated as a result of the changes to the platform since the initial release of the book.
Mike Riley
Author, Programming Your Home, Build an Awesome PC, and Developing Android on Android
Arduino: A Quick-Start Guide,
Second Edition
Maik Schmidt
The Pragmatic Bookshelf
Dallas, Texas • Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at https://pragprog.com.
All circuit diagrams were created with Fritzing (http://fritzing.org).
The team that produced this book includes:
Susannah Davidson Pfalzer (editor)
Potomac Indexing, LLC (indexer)
Cathleen Small (copyeditor)
Dave Thomas (typesetter)
Janet Furlow (producer)
Ellie Callahan (support)
For international rights, please contact rights@pragprog.com.
Copyright © 2015 The Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN-13: 978-1-94122-224-9
Encoded using the finest acid-free high-entropy binary digits.
Book version: P2.0—March 2015
Contents
|
Acknowledgments . . . . . . . . . |
. |
. |
xi |
|
Preface . . . . . . . . . . . . |
. |
. |
xiii |
|
The Parts You Need . . . . . . . . . |
. |
. |
xix |
|
Part I — Getting Started with Arduino |
|
|
|
1. |
Welcome to the Arduino . . . . . . . . |
. |
. |
3 |
|
What You Need |
|
|
4 |
|
What Exactly Is an Arduino? |
|
|
4 |
|
Exploring the Arduino Board |
|
|
6 |
|
Installing the Arduino IDE |
|
|
10 |
|
Meeting the Arduino IDE |
|
|
14 |
|
Hello, World! |
|
|
16 |
|
Compiling and Uploading Programs |
|
|
19 |
|
What If It Doesn’t Work? |
|
|
21 |
|
Exercises |
|
|
22 |
2. |
Creating Bigger Projects with the Arduino . . . |
. |
. |
23 |
|
What You Need |
|
|
23 |
|
Managing Projects and Sketches |
|
|
24 |
|
Changing Preferences |
|
|
26 |
|
Using Serial Ports |
|
|
28 |
|
What If It Doesn’t Work? |
|
|
35 |
|
Exercises |
|
|
36 |
Contents • vi
Part II — Eleven Arduino Projects
3. |
Building Binary Dice . . . . . . . . |
. |
. |
. |
39 |
||
|
What You Need |
|
|
|
|
|
39 |
|
Working with Breadboards |
|
|
|
|
|
40 |
|
Using an LED on a Breadboard |
|
|
|
|
|
41 |
|
First Version of a Binary Die |
|
|
|
|
|
45 |
|
Working with Buttons |
|
|
|
|
|
48 |
|
Adding Your Own Button |
|
|
|
|
|
53 |
|
Building a Dice Game |
|
|
|
|
|
55 |
|
What If It Doesn’t Work? |
|
|
|
|
|
59 |
|
Exercises |
|
|
|
|
|
60 |
4. |
Building a Morse Code Generator Library |
. . |
. |
. |
. |
61 |
|
|
What You Need |
|
|
|
|
|
61 |
|
Learning the Basics of Morse Code |
|
|
|
|
62 |
|
|
Building a Morse Code Generator |
|
|
|
|
|
62 |
|
Fleshing Out the Morse Code Generator’s Interface |
|
|
|
64 |
||
|
Outputting Morse Code Symbols |
|
|
|
|
|
65 |
|
Installing and Using the Telegraph Class |
|
|
|
|
67 |
|
|
Publishing Your Own Library |
|
|
|
|
|
71 |
|
What If It Doesn’t Work? |
|
|
|
|
|
73 |
|
Exercises |
|
|
|
|
|
73 |
5. |
Sensing the World Around Us |
. . . . . |
. |
. |
. |
77 |
|
|
What You Need |
|
|
|
|
|
78 |
|
Measuring Distances with an Ultrasonic Sensor |
|
|
|
78 |
||
|
Increasing Precision Using Floating-Point Numbers |
|
|
|
84 |
||
|
Increasing Precision Using a Temperature Sensor |
|
|
|
86 |
||
|
Creating Your Own Dashboard |
|
|
|
|
|
92 |
|
What If It Doesn’t Work? |
|
|
|
|
|
97 |
|
Exercises |
|
|
|
|
|
98 |
6. |
Building a Motion-Sensing Game Controller . . |
. |
. |
. |
99 |
||
|
What You Need |
|
|
|
|
|
100 |
|
Wiring Up the Accelerometer |
|
|
|
|
|
100 |
|
Bringing Your Accelerometer to Life |
|
|
|
|
102 |
|
|
Finding and Polishing Edge Values |
|
|
|
|
103 |
|
|
Building Your Own Game Controller |
|
|
|
|
106 |
|
|
More Projects |
|
|
|
|
|
109 |
|
|
|
Contents • vii |
|
|
What If It Doesn’t Work? |
|
|
110 |
|
Exercises |
|
|
110 |
7. Writing a Game for the Motion-Sensing Game Controller |
. |
111 |
||
|
Writing a GameController Class |
|
|
112 |
|
Creating the Game |
|
|
114 |
|
What If It Doesn’t Work? |
|
|
124 |
|
Exercises |
|
|
124 |
8. |
Generating Video Signals with an Arduino . . . . |
. |
127 |
|
|
What You Need |
|
|
128 |
|
How Analog Video Works |
|
|
128 |
|
Building a Digital-to-Analog Converter (DAC) |
|
130 |
|
|
Connecting the Arduino to Your TV Set |
|
131 |
|
|
Using the TVout Library |
|
|
133 |
|
Building a TV Thermometer |
|
|
134 |
|
Working with Graphics in TVout |
|
|
139 |
|
What If It Doesn’t Work? |
|
|
143 |
|
Exercises |
|
|
144 |
9. |
Tinkering with the Wii Nunchuk |
. . . . . . |
. |
145 |
|
What You Need |
|
|
145 |
|
Wiring a Wii Nunchuk |
|
|
146 |
|
Talking to a Nunchuk |
|
|
147 |
|
Building a Nunchuk Class |
|
|
148 |
|
Using Our Nunchuk Class |
|
|
152 |
|
Creating Your Own Video Game Console |
|
153 |
|
|
Creating Your Own Video Game |
|
|
153 |
|
What If It Doesn’t Work? |
|
|
162 |
|
Exercises |
|
|
162 |
10. |
Networking with Arduino . . . . . . . . |
. |
163 |
|
|
What You Need |
|
|
164 |
|
Using Your PC to Transfer Sensor Data to the Internet |
|
164 |
|
|
Registering an Application with Twitter |
|
167 |
|
|
Tweeting Messages with Processing |
|
|
168 |
|
Communicating Over Networks Using an Ethernet Shield |
|
173 |
|
|
Using DHCP and DNS |
|
|
177 |
|
What If It Doesn’t Work? |
|
|
180 |
|
Exercises |
|
|
181 |
|
|
|
Contents • viii |
|
11. |
Creating a Burglar Alarm with Email Notification . . |
. |
183 |
|
|
What You Need |
|
|
184 |
|
Emailing from the Command Line |
|
|
184 |
|
Emailing Directly from an Arduino |
|
|
189 |
|
Detecting Motion Using a Passive Infrared Sensor |
|
193 |
|
|
Bringing It All Together |
|
|
197 |
|
What If It Doesn’t Work? |
|
|
200 |
|
Exercises |
|
|
200 |
12. |
Creating Your Own Universal Remote Control . . . |
. |
201 |
|
|
What You Need |
|
|
202 |
|
Understanding Infrared Remote Controls |
|
202 |
|
|
Grabbing Remote Control Codes |
|
|
203 |
|
Cloning a Remote |
|
|
207 |
|
Controlling Infrared Devices Remotely with Your Browser |
|
212 |
|
|
Building an Infrared Proxy |
|
|
216 |
|
What If It Doesn’t Work? |
|
|
223 |
|
Exercises |
|
|
223 |
13. |
Controlling Motors with Arduino . . . . . . |
. |
225 |
|
|
What You Need |
|
|
225 |
|
Introducing Motors |
|
|
226 |
|
First Steps with a Servo Motor |
|
|
227 |
|
Building a Blaminatr |
|
|
230 |
|
What If It Doesn’t Work? |
|
|
235 |
|
Exercises |
|
|
235 |
|
Part III — Appendixes |
|
|
|
A1. |
Electronics and Soldering Basics . . . . . . |
. |
239 |
|
|
Current, Voltage, and Resistance |
|
|
239 |
|
Electrical Circuits |
|
|
239 |
|
Learning How to Use a Wire Cutter |
|
|
243 |
|
Learning How to Solder |
|
|
243 |
|
Learning How to Desolder |
|
|
247 |
A2. |
Advanced Arduino Programming |
. . . . . . |
. |
249 |
|
The Arduino Programming Language |
|
249 |
|
|
Bit Operations |
|
|
251 |
|
|
|
|
Contents • ix |
|
A3. |
Advanced Serial Programming . . . . . |
. |
. |
. |
253 |
|
Learning More About Serial Communication |
|
|
|
253 |
|
Serial Communication Using Various Languages |
|
|
|
255 |
A4. |
Controlling the Arduino with a Browser . . . |
. |
. |
. |
267 |
|
What Are Google Chrome Apps? |
|
|
|
267 |
|
Creating a Minimal Chrome App |
|
|
|
269 |
|
Starting the Chrome App |
|
|
|
270 |
|
Exploring the Chrome Serial API |
|
|
|
271 |
|
Writing a SerialDevice Class |
|
|
|
274 |
A5. |
Bibliography . . . . . . . . . |
. |
. |
. |
281 |
|
Index . . . . . . . . . . . |
. |
. |
. |
283 |
Acknowledgments
Susannah Davidson Pfalzer was the editor of the first edition of this book. When planning the second edition, I hadn’t forgotten how difficult it was to write the first one, but I also remembered how great it was to work with her. Again, she turned this endeavor into a real pleasure. Thank you very much!
This is not the first book I’ve written for the Pragmatic Bookshelf, so I knew already how professional and nice everyone on the team is. Still, they get even better every time, and I’d like to thank everyone for making this book happen.
This book would not have been possible without the stunning work of the whole Arduino team. Thank you so much for creating Arduino!
A big thank you goes to all the people who contributed material to this book: Christian Rattat took all the book’s photos, Kaan Karaca created the Blaminatr’s display, and Kassandra Perch improved the JavaScript code in the
“Creating Your Own Universal Remote Control” chapter.
I created all circuit diagrams with Fritzing,1 and I’d like to thank the Fritzing team for making such a great tool available for free.
For the games I developed for this book, I needed some artwork, and I’ve found amazing things on the OpenGameArt website.2 I used some graphics contributed by www.kenney.nl3 and a great song created by Alex Smith4 for my breakout clone. Thank you for putting these into the public domain.
The background image of the browser game comes from ESA/Hubble, NASA, Digitized Sky Survey, MPG/ESO (acknowledgment: Davide de Martin). The image showing how raster scan works was created by Ian Harvey.
1.http://fritzing.org/
2.http://opengameart.org/
3.http://opengameart.org/content/puzzle-game-art
4.http://opengameart.org/content/awake-megawall-10
report erratum • discuss
Acknowledgments • xii
For an author, there’s nothing more motivating and valuable than feedback. I’d like to thank my reviewers: Kevin Beam, Jessica Janiuk, Kassandra Perch, Mike Riley, Sam Rose, and Matthew Sullivan. This book is so much better because of your insightful comments and suggestions!
Finally, I have to thank my wonderful wife, Tanja, and my adorable son, Mika, for being patient and understanding whenever I had to write yet another page.
report erratum • discuss