




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































The most comprehensive book on the Arduino platform I have read. Loaded with excellent examples and references, Arduino: A Quick-Start Guide gets beginners up and running in no time and provides experi- enced developers with a wealth of inspiration for their own projects.
Haroon Baig Creator of the Twitwee Clock, http://www.haroonbaig.com
Excellently paced for those who have never experimented with elec- tronics or microcontrollers before and packed with valuable tidbits even for advanced Arduino tinkerers.
Georg Kaindl Creator, Arduino DHCP, DNS, and Bonjour libs
The Arduino platform is a great way for anyone to get into embedded systems, and this book is the road map. From first baby steps to com- plex sensors and even game controllers, there is no better way to get going on the Arduino.
Tony Williamitis Senior embedded systems engineer
I recommend this engaging and informative book to software develop- ers who want to learn the basics of electronics, as well as to anyone looking to interface their computers with the physical world.
René Bohne Software developer and creator of LumiNet
Many of the designations used by manufacturers and sellers to distinguish their prod- ucts 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 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 http://www.pragprog.com.
The team that produced this book includes:
Editor: Susannah Pfalzer Indexing: Potomac Indexing, LLC Copy edit: Kim Wimpsett Layout: Samuel Langhorne Production: Janet Furlow Customer support: Ellie Callahan International: Juliet Benda
Copyright © 2011 Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, 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-10: 1-934356-66- ISBN-13: 978-1-934356-66- Printed on acid-free paper. P1.0 printing, Janurary, 2011 Version: 2011-1-
For Yvonne.
The greatest little sister on earth.
Report erratum
Report erratum
Writing books doesn’t get easier the more often I do it—I think there will never be a time when I can do it on my own. I will always depend on the help of others, and a lot of wonderful people contributed to this book.
I have to start by thanking my unbelievably talented editor, Susannah Davidson Pfalzer. Only because of her insightful advice, her patience, and her encouragement have I finished this book. I owe her so much!
Also, the Pragmatic Bookshelf team again has been amazingly profes- sional, and my publishers have been very sympathetic when I went through some hard times. I am so thankful for that!
This book would not have been possible without the stunning work of the whole Arduino team! Thank you so much for creating the 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 Tod E. Kurt kindly allowed me to use his excellent C code for accessing an Arduino via serial port.
I have 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 an author, there’s nothing more motivating than feedback. I’d like to thank my reviewers: René Bohne, Stefan Christoph, Georg Kaindl, Kaan Karaca, Christian Rattat, Stefan Rödder, Christoph Schwaeppe, Federico Tomassetti, and Tony Williamitis. This book is so much better because of your insightful comments and suggestions! I am also grate- ful to all readers who have sent in errata during the beta book period.
When I had written the first half of this book, my mother passed away in February 2010. It has been one of the hardest times in my life, and
Welcome to the Arduino, and welcome to the exciting world of physical computing! Arduino 2 is an open source project consisting of both hard- ware and software. It was originally created to give designers and artists a prototyping platform for interaction design courses. Today hobby- ists and experts all over the world use it to create physical computing projects, and you can too.
The Arduino lets us get hands-on again with computers in a way we haven’t been able to since the 1980s, when you could build your own computer. And Arduino makes it easier than ever to develop hand- crafted 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 course work.
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 begin- ning. You’ll not only learn how to use some of the most important elec- tronic 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.
W HO S HOULD R EAD T HIS B OOK 14
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 the Arduino is a nice 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 troubleshooting sections—and the hands-on experience you’ll gain as you become more confident project by project—will make it all worth- while. Reading about electronics without doing the projects yourself isn’t even half the battle (you know the old saying: we remember 5 per- cent 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 program- ming course or read a beginner’s book about programming first (Learn to Program [Pin06] is a nice 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,” “Eight 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.
Here’s a short walk-through:
Report erratum this copy is (P1.0 printing, Janurary, 2011)
A RDUINO U NO AND THE A RDUINO P LATFORM 16
into any problems, you’ll find a list of exercises to build your skills at the end of every chapter.
All the projects in this book have been tested on the Arduino Uno, the Arduino Duemilanove, and with the Arduino IDE versions 18 to 21. If possible, you should always use the latest version.
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. It will be the reference for all future developments, and they announced it on the first day of 2010.^3 Since then, they have released the Arduino Uno, and they have also improved the IDE and its supporting libraries step-by-step.
At the moment of this writing, it is still not completely clear what Arduino 1.0 will look like. The Arduino team tries to keep this release as backward compatible as possible. This book is up-to-date for the new Arduino Uno boards. All the projects will also work with older Arduino boards such as the Duemilanove or Diecimila. This book is current for version 21 of the Arduino platform. You can follow the progress of the Arduino platform online. 4
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 use C/C++ for all programs that will eventually run on the Arduino. For applications running on our PC, we use Processing, 5 but in Sec- tion C.2, Serial Communication Using Various Programming Languages, on page 253, you’ll also learn how to use several other programming languages to communicate with an Arduino.
Whenever you find a slippery road icon beside a paragraph, slow down and read carefully. They announce difficult or dangerous techniques.
Report erratum this copy is (P1.0 printing, Janurary, 2011)
O NLINE R ESOURCES 17
Online Resources
This book has its own web page at http://pragprog.com/titles/msard where you can download the code for all examples (if you have the ebook ver- sion of this book, clicking the little gray box above each code example downloads that source file directly). You can also participate in a dis- cussion 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. 6
On the web page you will also find a link to a Flickr^7 photo 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!
Report erratum this copy is (P1.0 printing, Janurary, 2011)
C OMPLETE P AR TS L IST 19
Alternatively, Adafruit also sells an Arduino Starter Pack (product ID 170). It’s cheaper, but it doesn’t contain as many parts. For example, it doesn’t have a Protoshield or a tilt sensor.
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 a starter pack, here is a list of all the parts used in the book. Each chapter also has a parts list and photo with all parts needed for that chapter. Suggested websites where you can buy the parts are listed here for your convenience, but many of these parts are available elsewhere also, so feel free to shop around.
Good shops for buying individual components parts are RadioShack, 10 Digi-Key,^11 sparkfun, 12 and Mouser. 13
Report erratum this copy is (P1.0 printing, Janurary, 2011)
C OMPLETE P AR TS L IST 20
For some of the exercises, you’ll need some optional parts:
Report erratum this copy is (P1.0 printing, Janurary, 2011)