Arduino uno User manual

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 Ar-
duino 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
www.it-ebooks.info

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 gdevice are trade-
marks 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
.
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
www.it-ebooks.info

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
www.it-ebooks.info

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 •vi
www.it-ebooks.info

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 •vii
www.it-ebooks.info

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 •viii
www.it-ebooks.info

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
Contents •ix
www.it-ebooks.info

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 Blami-
natr’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,1and 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.2I used some graphics
contributed by www.kenney.nl3and a great song created by Alex Smith4for
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
www.it-ebooks.info

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.
Acknowledgments •xii
report erratum •discuss
www.it-ebooks.info

Preface
Welcome to Arduino, and welcome to the exciting world of physical computing!
Arduino1is an open-source project consisting of both hardware and software.
It was originally created to give designers and artists a prototyping platform
for interaction design courses. Today, hobbyists and experts all over the world
use it to create physical computing projects, and you can, too.
Arduino lets you get hands-on again with computers in a way you haven’t
been able to since the 1980s, when you could build your own computer. And
Arduino makes it easier than ever to develop handcrafted electronics projects
ranging from prototypes to sophisticated gadgets. Gone are the days when
you had to learn lots of theory about electronics and arcane programming
languages before you could even get an LED blinking. You can create your
first Arduino project in a few minutes without needing advanced electrical
engineering coursework.
In fact, you don’t need to know anything about electronics projects to read
this book, and you’ll get your hands dirty right from the beginning. You’ll not
only learn how to use some of the most important electronic parts in the first
pages, you’ll also learn how to write the software needed to bring your projects
to life.
This book dispenses with theory and stays hands-on throughout. I’ll explain
all the basics you need to build the book’s projects, and every chapter has a
troubleshooting section to help when things go wrong. This book is a quick-
start guide that gets you up to speed quickly and enables you to immediately
create your own projects.
Who Should Read This Book
If you are interested in electronics—and especially in building your own toys,
games, and gadgets—then this book is for you. Although Arduino is a nice
1.
http://arduino.cc
report erratum •discuss
www.it-ebooks.info

tool for designers and artists, only software developers are able to unleash
its full power. So, if you’ve already developed some software—preferably with
C/C++ or Java—then you’ll get a lot out of this book.
But there’s one more thing: you have to build, try, and modify the projects
in this book. Have fun. Don’t worry about making mistakes. The troubleshoot-
ing sections—and the hands-on experience you’ll gain as you become more
confident project by project—will make it all worthwhile. Reading about elec-
tronics without doing the projects yourself isn’t even half the battle. (You
know the old saying: we remember 5 percent of what we hear, 10 percent of
what we write, and 95 percent of what we personally suffer.) And don’t be
afraid: you really don’t need any previous electronics project experience!
If you’ve never written a piece of software before, start with a programming
course or read a beginner’s book about programming first. (Learn to Program,
Second Edition [Pin09] is a good starting point.) Then, learn to program in C
with The C Programming Language [KR98] or in C++ with The C++ Programming
Language [Str00].
What’s in This Book
This book consists of three parts (“Getting Started with Arduino,”“Eleven
Arduino Projects,”and the appendixes). In the first part, you’ll learn all the
basics you need to build the projects in the second part, so read the chapters
in order and do all the exercises. The chapters in the second part also build
on each other, reusing techniques and code from earlier chapters.
There’s one exception, though: in this book you’ll create several Google Chrome
apps that connect your web browser to Arduino. Appendix 4, Controlling the
Arduino with a Browser, on page 267, explains in detail how Chrome apps
work, so you should read it after you’ve read Chapter 4, Building a Morse
Code Generator Library, on page 61.
Here’s a short walkthrough:
•The book starts with the basics of Arduino development. You’ll learn how
to use the integrated development environment (IDE) and how to compile
and upload programs. You’ll quickly build your first project—electronic
dice—that shows you how to work with basic parts such as LEDs, buttons,
and resistors. By implementing a Morse code generator, you’ll see how
easy it is to create your own Arduino libraries.
•Then you’ll learn how to work with analog and digital sensors. You’ll use
a temperature sensor and an ultrasonic sensor to build a very accurate
Preface •xiv
report erratum •discuss
www.it-ebooks.info

digital metering ruler. Then you’ll use a three-axis accelerometer to build
your own motion-sensing game controller and a cool breakout game clone.
•At this point you’ve output data mostly using some LEDs and the Arduino’s
serial port. Now you’ll connect the Arduino to an actual TV set and gener-
ate your own video signals. You’ll create a graphical thermometer that
you can display on the TV set in your living room.
•In electronics, you don’t necessarily have to build gadgets yourself. You
can also tinker with existing hardware, and you’ll see how easy it is to
take full control of Nintendo’s Wii Nunchuk so you can use it in your own
applications. Soon, you’ll have everything you need to build your own
video game console.
•The Arduino does not have to work in isolation, and it works great with
different networking technologies. You’ll connect the Arduino to the
Internet in various ways, and you’ll learn how to send Twitter messages
and emails. You’ll build a burglar alarm that sends you an email whenever
someone is moving in your living room during your absence.
•Using a Nunchuk to control applications or devices is handy, but often
it’s more convenient to have a wireless remote control. So, you’ll learn
how to build your own universal remote control that you can even control
using a web browser.
•Finally, you’ll work with motors by creating a fun device for your next
software project. You can connect it to your continuous integration system,
so whenever the build fails, it will move an arrow to point to the name of
the developer who is responsible.
•In the appendixes, you’ll learn about the basics of electricity and soldering.
You’ll also find advanced information about programming a serial port
and programming the Arduino in general. And you’ll find an appendix
that explains how to control the Arduino using a web browser.
Every chapter starts with a detailed list of all the parts and tools you need to
build the chapter’s projects. All chapters contain lots of photos and diagrams
showing how everything fits together. You’ll get inspired by descriptions of
real-world Arduino projects in sidebars throughout the book.
Things won’t always work out as expected, and debugging circuits can be a
challenging task. So in every chapter, you’ll find a “What If It Doesn’t Work?”
section that explains the most common problems and their solutions.
report erratum •discuss
What’s in This Book •xv
www.it-ebooks.info

Before you read the solutions in the “What If It Doesn’t Work?”sections,
though, try to solve the problems yourself, because that’s the most effective
way to learn. In the unlikely case that you don’trun into any problems, you’ll
find a list of exercises at the end of every chapter to build your skills.
Arduino Uno and the Arduino Platform
After releasing several Arduino boards and Arduino IDE versions, the Arduino
team decided to specify a version 1.0 of the platform. Arduino’s version
numbering was counterintuitive before. At the beginning of the project the
developers increased the version number by 1 with each new release. They
did that up to number 23, and then they decided to use 1.0 as the version
number for the next release. That means Arduino 1.0 is more recent than
Arduino 23.
Arduino 1.0, released at the end of 2011, has since been the reference point
for all developments. The Arduino developers have also released the Arduino
Uno board, and they’ve continued to improve the IDE and its supporting
libraries.
In parallel, the Arduino team has created more Arduino boards, such as the
Arduino Due,2the Arduino Leonardo,3and the Arduino Yún.4These boards
either have more powerful microcontrollers or come with additional hardware,
such as a Wi-Fi module.
Most of the new boards use a different processor architecture designed by
ARM. This architecture isn’t compatible with the architecture of the older
board’s AVR processors. To overcome this gap, the Arduino team started to
develop version 1.5.x of the Arduino IDE in parallel with 1.0.x. This develop-
ment led to version 1.6.0, which supports the different processor architectures
transparently.
In addition to all that, there’s a separate IDE for the Arduino Galileo.5This
board was created by Intel and is compatible with the Arduino.
This book is current for versions 1.0.6 and 1.6.0 of the Arduino platform and
up to date for the Arduino Uno board. Most of the projects will also work on
other recent boards, such as the Leonardo or the Due. They will also work
2.
http://arduino.cc/en/Main/ArduinoBoardDue
3.
http://arduino.cc/en/Main/ArduinoBoardLeonardo
4.
http://arduino.cc/en/Main/ArduinoBoardYun
5.
http://arduino.cc/en/ArduinoCertified/IntelGalileo
Preface •xvi
report erratum •discuss
www.it-ebooks.info

on older Arduino boards, such as the Duemilanove or Diecimila. All code in
this book has been tested with Arduino 1.0.6 and 1.6.0.
Code Examples and Conventions
Although this is a book about open-source hardware and electronics, you will
find a lot of code examples. We need them to bring the hardware to life and
make it do what we want it to do.
We’ll use C/C++ for all programs that will eventually run on the Arduino. For
applications running on our PC, we’ll mainly use JavaScript and Processing.6
In Serial Communication Using Various Languages, on page 255, you’ll also
learn how to use several other programming languages to communicate with
an Arduino.
Online Resources
This book has its own web page at
http://pragprog.com/book/msard2
where you can
download the code for all examples. (If you have the ebook version of this
book, clicking the little gray box above each code example downloads that
source file directly.) You can also participate in a discussion forum and meet
other readers and me. If you find bugs, typos, or other annoyances, please
let me and the world know about them on the book’s errata page.7
On the web page you will also find a link to a Flickr8photo set. It contains
all the book’s photos in high resolution. There you can also see photos of
reader projects, and we’d really like to see photos of your projects, too!
Let’s get started!
6.
http://processing.org
7.
http://www.pragprog.com/book/msard2/errata
8.
http://flickr.com
report erratum •discuss
Code Examples and Conventions •xvii
www.it-ebooks.info

The Parts You Need
Here’s a list of the parts you need to work through all the projects in this
book. In addition, each chapter lists the parts you’ll need for that chapter’s
projects, so you can try projects chapter by chapter without buying all the
components at once. Although there appears to be a lot of components here,
they’re all fairly inexpensive, and you can buy all the parts you need for all
of the projects in this book for about $200.
Starter Packs
Many online shops sell Arduino components and electronic parts. Some of
the best are Maker Shed1and Adafruit.2They have awesome starter packs,
and I strongly recommend buying one of these.
At the time of this writing, the best and cheapest solution is to buy the Adafruit
Experimentation Kit for Arduino (product ID 170). It contains many of the
parts you need to build the book’s examples, as well as many more useful
parts that you can use for your own side projects. Check the current contents
of the kit, but usually you have to buy the following parts separately:
•Parallax PING))) sensor
•ADXL335 accelerometer breakout board
•6-pin 0.1-inch standard header
•Nintendo Nunchuk controller
•A passive infrared sensor
•An infrared LED
•An infrared receiver
•An Ethernet shield
•A Proto shield
•An RCA (composite video) cable
1.
http://makershed.com
2.
http://adafruit.com
report erratum •discuss
www.it-ebooks.info

All shops constantly improve their starter packs, so it’s a good idea to scan
their online catalogs carefully.
Complete Parts List
If you prefer to buy parts piece by piece (or chapter by chapter) rather than
in a starter pack, here is a list of all the parts used in the book. Each chapter
also has a parts list and a photo with all parts needed for that chapter. Sug-
gested websites where you can buy the parts are listed here for your conve-
nience, but many of these parts are available elsewhere also, so feel free to
shop around.
Good shops for buying individual components parts are Digi-Key,3SparkFun,4
and Mouser.5Over the years Amazon6has become an excellent shop for
electronic parts, too.
•An Arduino board, such as the Uno, available from Adafruit or Maker
Shed.
•A USB cable. Depending on the Arduino board you’re using, you will either
need a standard A-B cable or a standard A-micro-B cable. You might
already have a few. If not, you can order it at Amazon, for example.
•A half-size breadboard from Maker Shed (search for breadboard) or from
Adafruit (product ID 64).
•Three LEDs. (You need four additional ones for an optional exercise.)
Buying LEDs one at a time isn’t too useful; a better idea is to buy a pack
of 20 or more. Search for LED pack at any of the online shops mentioned
in this chapter.
•One 100Ωresistor, one 330Ωresistor, two 10kΩresistors, and three 1kΩ
resistors. It’s also not too useful to buy single resistors; buy a value pack,
such as catalog number 10969 from SparkFun.
•Two pushbuttons. Don’t buy a single button switch; buy at least four
instead, available at Digi-Key (part number 450-1650-ND) or Mouser (101-
TS6111T1602-EV).
•Some wires, preferably breadboard jumper wires. You can buy them at
Maker Shed (product code MKSEEED3) or Adafruit (product ID 153).
3.
http://digikey.com
4.
http://sparkfun.com
5.
http://www.mouser.com
6.
http://amazon.com
The Parts You Need •xx
report erratum •discuss
www.it-ebooks.info

•A Parallax PING))) sensor (product code MKPX5) from Maker Shed.
•A passive infrared sensor (product ID 189) from Adafruit.
•A TMP36 temperature sensor from Analog Devices.7You can get it from
Adafruit (product ID 165).
•An ADXL335 accelerometer breakout board. You can buy it at Adafruit
(product ID 163).
•A 6-pin 0.1-inch standard header (included if you order the ADXL335
from Adafruit). Alternatively, you can order from SparkFun (search for
breakaway headers). Usually, you can only buy strips that have more
pins. In this case, you have to cut it accordingly.
•An Arduino Proto shield from Adafruit (product ID 2077). You’ll also need
a tiny breadboard (product ID 65 at Adafruit). The Proto shield is optional,
but I highly recommend it, especially for building the motion-sensing
game controller. Note that this shield comes as a kit, so you have to solder
it yourself.
•A Nintendo Nunchuk controller. You can buy it at nearly every toy store
or at
http://www.amazon.com/
, for example.
•An Arduino Ethernet shield (product ID 201) from Adafruit.
•An infrared sensor, such as the TSOP38238. You can buy it a Adafruit
(product ID 157) or Digi-Key (search for TSOP38238).
•An infrared LED. You can get it from SparkFun (search for infrared LED)
or from Adafruit (product ID 387).
•An RCA (composite video) cable. You can get it at Adafruit (product ID
863), for example.
•A 5V servo motor, such as the Hitec HS-322HD or the Vigor Hextronik.
You can get one from Adafruit (product id 155) or SparkFun. Search for
standard servos with an operating voltage of 4.8V–6V.
For some of the exercises, you’ll need some optional parts:
•A piezo speaker or buzzer. Search for piezo buzzer at Maker Shed (product
code MSPT01) or get it from Adafruit (product ID 160).
7.
http://www.analog.com/en/sensors/digital-temperature-sensors/tmp36/products/product.html
report erratum •discuss
Complete Parts List •xxi
www.it-ebooks.info
Other manuals for uno
4
This manual suits for next models
10
Table of contents
Other Arduino Motherboard manuals

Arduino
Arduino ESP 8266 User manual

Arduino
Arduino Nano ESP32 User manual

Arduino
Arduino MEGA 2560 R3 User manual

Arduino
Arduino uno User manual

Arduino
Arduino Nano A000005 User manual

Arduino
Arduino MKR Vidor 4000 User manual

Arduino
Arduino ABX00053 User manual

Arduino
Arduino WiFI Shield User manual

Arduino
Arduino MEGA 2560 User manual