Docsity

Arduino- A QUICK-START GUIDE, Notas de estudo de Cultura

Pré-visualização parcial do texto

Baixe Arduino- A QUICK-START GUIDE e outras Notas de estudo em PDF para Cultura, somente na Docsity!

What Readers Are Saying About

Arduino: A Quick-Start Guide

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.

Contents

Report erratum

Report erratum

  • 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
  • I Getting Started with Arduino
  • 1 Welcome to the Arduino
    • 1.1 What You Need
    • 1.2 What Exactly Is an Arduino?
    • 1.3 Exploring the Arduino Board
    • 1.4 Installing the Arduino IDE
    • 1.5 Meeting the Arduino IDE
    • 1.6 Compiling and Uploading Programs
    • 1.7 Working with LEDs
    • 1.8 What If It Doesn’t Work?
    • 1.9 Exercises
  • 2 Inside the Arduino
    • 2.1 What You Need
    • 2.2 Managing Projects and Sketches
    • 2.3 Changing Preferences
    • 2.4 Using Serial Ports
    • 2.5 What If It Doesn’t Work?
    • 2.6 Exercises - CONTENTS
  • II Eight Arduino Projects
  • 3 Building Binary Dice
    • 3.1 What You Need
    • 3.2 Working with Breadboards
    • 3.3 Using an LED on a Breadboard
    • 3.4 First Version of a Binary Die
    • 3.5 Working with Buttons
    • 3.6 Adding Our Own Button
    • 3.7 Building a Dice Game
    • 3.8 What If It Doesn’t Work?
    • 3.9 Exercises
  • 4 Building a Morse Code Generator Library
    • 4.1 What You Need
    • 4.2 Learning the Basics of Morse Code
    • 4.3 Building a Morse Code Generator
    • 4.4 Fleshing Out the Generator’s Interface
    • 4.5 Outputting Morse Code Symbols
    • 4.6 Installing and Using the Telegraph Class
    • 4.7 Final Touches
    • 4.8 What If It Doesn’t Work?
    • 4.9 Exercises
  • 5 Sensing the World Around Us
    • 5.1 What You Need
    • 5.2 Measuring Distances with an Ultrasonic Sensor
    • 5.3 Increasing Precision Using Floating-Point Numbers
    • 5.4 Increasing Precision Using a Temperature Sensor
      • cessing 5.5 Transferring Data Back to Your Computer Using Pro-
    • 5.6 Representing Sensor Data
    • 5.7 Building the Application’s Foundation
    • 5.8 Implementing Serial Communication in Processing
    • 5.9 Visualizing Sensor Data
    • 5.10 What If It Doesn’t Work?
    • 5.11 Exercises - CONTENTS
  • 6 Building a Motion-Sensing Game Controller
    • 6.1 What You Need
    • 6.2 Wiring Up the Accelerometer
    • 6.3 Bringing Your Accelerometer to Life
    • 6.4 Finding and Polishing Edge Values
    • 6.5 Building Your Own Game Controller
    • 6.6 Writing Your Own Game
    • 6.7 More Projects
    • 6.8 What If It Doesn’t Work?
    • 6.9 Exercises
  • 7 Tinkering with the Wii Nunchuk
    • 7.1 What You Need
    • 7.2 Wiring a Wii Nunchuk
    • 7.3 Talking to a Nunchuk
    • 7.4 Building a Nunchuk Class
    • 7.5 Using Our Nunchuk Class
    • 7.6 Rotating a Colorful Cube
    • 7.7 What If It Doesn’t Work?
    • 7.8 Exercises
  • 8 Networking with Arduino
    • 8.1 What You Need
      • net 8.2 Using Your PC to Transfer Sensor Data to the Inter-
    • 8.3 Registering an Application with Twitter
    • 8.4 Tweeting Messages with Processing
    • 8.5 Networking Using an Ethernet Shield
    • 8.6 Emailing from the Command Line
    • 8.7 Emailing Directly from an Arduino
    • 8.8 Detecting Motion Using a Passive Infrared Sensor
    • 8.9 Bringing It All Together
    • 8.10 What If It Doesn’t Work?
    • 8.11 Exercises
  • 9 Creating Your Own Universal Remote Control
    • 9.1 What You Need
    • 9.2 Understanding Infrared Remote Controls
    • 9.3 Grabbing Remote Control Codes
    • 9.4 Building Your Own Apple Remote
    • 9.5 Controlling Devices Remotely with Your Browser - CONTENTS
    • 9.6 Building an Infrared Proxy
    • 9.7 What If It Doesn’t Work?
    • 9.8 Exercises
  • 10 Controlling Motors with Arduino
    • 10.1 What You Need
    • 10.2 Introducing Motors
    • 10.3 First Steps with a Servo Motor
    • 10.4 Building a Blaminatr
    • 10.5 What If It Doesn’t Work?
    • 10.6 Exercises
  • III Appendixes
  • A Basics of Electronics
    • A.1 Current, Voltage, and Resistance
    • A.2 Learning How to Solder
  • B Advanced Arduino Programming
    • B.1 The Arduino Programming Language
    • B.2 Bit Operations
  • C Advanced Serial Programming
    • C.1 Learning More About Serial Communication
      • Languages C.2 Serial Communication Using Various Programming
  • D Bibliography
    • Index

Acknowledgments

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

  1. http://fritzing.org/

Preface

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.

  1. http://arduino.cc

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:

  • The book starts with the basics of Arduino development. You’ll learn how to use the IDE and how to compile and upload pro- grams. 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.

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.

  1. http://arduino.cc/blog/2010/01/01/uno-punto-zero/
  2. http://code.google.com/p/arduino/issues/list?q=milestone=1.
  3. http://processing.org

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!

  1. http://www.pragprog.com/titles/msard/errata
  2. http://flickr.com

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

  • An Arduino board such as the Uno, Duemilanove, or Diecimila available from Adafruit (product ID 50) or Makershed (product code MKSP4).
  • A standard A-B USB cable for USB 1.1 or 2.0. You might already have a few. If not, you can order it at RadioShack (catalog number 55011289).
  • A half-size breadboard from Makershed (product code MKKN2) or from Adafruit (product ID 64).
  • Three LEDs (four additional ones are needed for an optional exer- cise). Buying LEDs one at a time isn’t too useful; a better idea is to buy a pack of 20 at RadioShack (catalog number 276-1622).
  • One 100Ω 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 271-308 from RadioShack.
  • Two pushbuttons. Don’t buy a single button switch; buy at least four instead, available at RadioShack (catalog number 275-002).
  1. http://radioshack.com
  2. http://digikey.com
  3. http://sparkfun.com
  4. http://mouser.com

Report erratum this copy is (P1.0 printing, Janurary, 2011)

C OMPLETE P AR TS L IST 20

  • Some wires, preferably breadboard jumper wires. You can buy them at Makershed (product code MKSEEED3) or Adafruit (prod- uct ID 153).
  • A Parallax PING))) sensor (product code MKPX5) from Makershed.
  • A Passive Infrared Sensor (product code MKPX6) from Makershed.
  • A TMP36 temperature sensor from Analog Devices. 14 You can get it from Adafruit (product ID165).
  • An ADXL335 accelerometer breakout board. You can buy it at Adafruit (product ID 163).
  • A 6 pin 0.1" standard header (included, if you order the ADXL from Adafruit). Alternatively, you can order from sparkfun (search for breakaway headers). Usually, you can only buy stripes that have more pins. In this case, you have to cut it accordingly.
  • 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 code MKSP7) from Maker- shed.
  • An infrared sensor such as the PNA4602. You can buy it a Adafruit (product ID 157) or Digi-Key (search for PNA4602).
  • An infrared LED. You can get it from RadioShack (catalog number 276-143) or from sparkfun (search for infrared LED).
  • A 5V servo motor such as the Hitec HS-322HD or the Vigor Hex- tronic. 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:

  • An Arduino Proto Shield from Adafruit (product ID 51) or Maker- shed (product code MKAD6). You’ll also need a tiny breadboard (product code MKKN1 at Makershed). I highly recommend this shield!
  • A piezo speaker or buzzer. Search for piezo buzzer at RadioShack or get it from Adafruit (product ID 160).
  1. http://www.analog.com/en/sensors/digital-temperature-sensors/tmp36/products/product.html

Report erratum this copy is (P1.0 printing, Janurary, 2011)