The Open Source Electronics Prototyping PlatformMassimo Banzi co-founder of Arduino & Michael Shiloh Hardware/Programming with Arduino In Getting Started with Arduino, you’ll learn about
Trang 1The Open Source Electronics Prototyping Platform
Massimo Banzi co-founder of Arduino
& Michael Shiloh
Hardware/Programming
with Arduino
In Getting Started with Arduino, you’ll learn about:
Arduino is the open source electronics prototyping platform that
has taken the Maker Movement by storm This thorough
introduction, updated for the latest Arduino release, helps you
start prototyping right away From obtaining the required
components to putting the final touches on your project, all the
information you need is here!
Getting started with Arduino is a snap To use the introductory
examples in this guide, all you need is an Arduino Uno or
Leonardo, along with a USB cable and an LED The easy-to-use,
free Arduino development environment runs on Mac, Windows,
and Linux.
Join hundreds of thousands of hobbyists who have discovered
this incredible (and educational) platform
» Interaction design and physical computing
» The Arduino board and its software environment
» Basics of electricity and electronics
» Prototyping on a solderless breadboard
» Drawing a schematic diagram
» Talking to a computer—and the cloud—from Arduino
» Building a custom plant-watering system
Make:
makezine.com
US $19.99 CAN $20.99
ISBN: 978-1-4493-6333-8
Written by Massimo Banzi, the co-founder of Arduino
and Michael Shiloh, Arduino’s educational lead.
Getting Started with Arduino Make:
Getting Started with Arduino
Trang 2The Open Source Electronics Prototyping Platform
Massimo Banzi co-founder of Arduino
& Michael Shiloh
Hardware/Programming
with Arduino
In Getting Started with Arduino, you’ll learn about:
Arduino is the open source electronics prototyping platform that
has taken the Maker Movement by storm This thorough
introduction, updated for the latest Arduino release, helps you
start prototyping right away From obtaining the required
components to putting the final touches on your project, all the
information you need is here!
Getting started with Arduino is a snap To use the introductory
examples in this guide, all you need is an Arduino Uno or
Leonardo, along with a USB cable and an LED The easy-to-use,
free Arduino development environment runs on Mac, Windows,
and Linux.
Join hundreds of thousands of hobbyists who have discovered
this incredible (and educational) platform
» Interaction design and physical computing
» The Arduino board and its software environment
» Basics of electricity and electronics
» Prototyping on a solderless breadboard
» Drawing a schematic diagram
» Talking to a computer—and the cloud—from Arduino
» Building a custom plant-watering system
Make:
makezine.com
US $19.99 CAN $20.99
ISBN: 978-1-4493-6333-8
Written by Massimo Banzi, the co-founder of Arduino
and Michael Shiloh, Arduino’s educational lead.
Getting Started with Arduino Make:
Getting Started with Arduino
Trang 3THIRD EDITION
Getting
Started with Arduino
Massimo Banzi and
Michael Shiloh
www.it-ebooks.info
Trang 4[LSI]
Getting Started with Arduino
by Massimo Banzi and Michael Shiloh
Copyright © 2015 Massimo Banzi and Michael Shiloh All rights reserved.
Printed in the United States of America.
Published by Maker Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
Maker Media books may be purchased for educational, business, or sales tional use Online editions are also available for most titles (http://www.safaribook- sonline.com) For more information, contact our corporate/institutional sales depart- ment: 800-998-9938 or corporate@oreilly.com.
promo-Editor: Brian Jepson
Production Editor: Nicole Shelby
Copyeditor: Kim Cofer
Proofreader: Sharon Wilkey
Indexer: WordCo Indexing Services
Interior Designer: David Futato
Cover Designer: Brian Jepson
Illustrator: Judy Aime’ Castro
December 2014: Third Edition
Revision History for the Third Edition
2014-12-09: First Release
2014-12-19: Second Release
See http://oreilly.com/catalog/errata.csp?isbn=9781449363338 for release details Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc.
The Maker Media logo is a trademark of Maker Media, Inc Getting Started with
Ardu-ino and related trade dress are trademarks of Maker Media, Inc.
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 Maker Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages result- ing from the use of the information contained herein.
www.it-ebooks.info
Trang 5Preface vii
1/Introduction 1
Intended Audience 2
What Is Interaction Design? 3
What Is Physical Computing? 3
2/The Arduino Way 5
Prototyping 5
Tinkering 6
Patching 7
Circuit Bending 9
Keyboard Hacks 11
We Love Junk! 12
Hacking Toys 13
Collaboration 14
3/The Arduino Platform 15
The Arduino Hardware 15
The Software Integrated Development Environment (IDE) 18
Installing Arduino on Your Computer 19
Installing the IDE: Macintosh 19
Configuring the Drivers: Macintosh 19
Port Identification: Macintosh 20
Installing the IDE: Windows 21
Configuring the Drivers: Windows 21
Port Identification: Windows 22
4/Really Getting Started with Arduino 25
Anatomy of an Interactive Device 25
Sensors and Actuators 25
Blinking an LED 26
Pass Me the Parmesan 31
iii www.it-ebooks.info
Trang 6Arduino Is Not for Quitters 31
Real Tinkerers Write Comments 32
The Code, Step by Step 32
What We Will Be Building 36
What Is Electricity? 37
Using a Pushbutton to Control the LED 40
How Does This Work? 44
One Circuit, a Thousand Behaviours 45
5/Advanced Input and Output 53
Trying Out Other On/Off Sensors 53
Homemade (DIY) Switches 56
Controlling Light with PWM 56
Use a Light Sensor Instead of the Pushbutton 64
Analogue Input 66
Try Other Analogue Sensors 69
Serial Communication 70
Driving Bigger Loads (Motors, Lamps, and the Like) 72
Complex Sensors 74
6/The Arduino Leonardo 77
How Is This Arduino Different from All Other Arduinos? 77
Other Differences Between the Arduino Leonardo and the Arduino Uno 78
Leonardo Keyboard Message Example 80
How Does This Work? 82
Leonardo Button Mouse Control Example 83
How Does This Work? 86
More Leonardo Differences 87
7/Talking to the Cloud 91
Planning 93
Coding 94
Assembling the Circuit 101
Here’s How to Assemble It 103
8/Automatic Garden-Irrigation System 105
Planning 107
Testing the Real Time Clock (RTC) 110
Testing the Relays 116
Electronic Schematic Diagrams 119
Testing the Temperature and Humidity Sensor 132
iv Contents
www.it-ebooks.info
Trang 7Coding 137
Setting the On and Off Times 137
Checking Whether It’s Time to Turn a Valve On or Off 143
Checking for Rain 148
Putting It All Together 149
Assembling the Circuit 158
The Proto Shield 162
Laying Out Your Project on the Proto Shield 164
Soldering Your Project on the Proto Shield 170
Testing Your Assembled Proto Shield 184
Assembling Your Project into a Case 186
Testing the Finished Automatic Garden Irrigation System 190
Things to Try on Your Own 191
Irrigation Project Shopping List 191
9/Troubleshooting 193
Understanding 193
Simplification and Segmentation 194
Exclusion and Certainty 194
Testing the Arduino Board 194
Testing Your Breadboarded Circuit 196
Isolating Problems 198
Problems Installing Drivers on Windows 199
Problems with the IDE on Windows 199
Identifying the Arduino COM Port on Windows 200
Other Debugging Techniques 201
How to Get Help Online 203
A/The Breadboard 207
B/Reading Resistors and Capacitors 211
C/Arduino Quick Reference 215
D/Reading Schematic Diagrams 233
Index 237
Contents v www.it-ebooks.info
Trang 9The third edition of Getting Started with Arduino adds two new
chapters: Chapter 8 is an ambitious project, which illustrates amore complex circuit and program This chapter also talksabout project design, testing, and construction, and makes use
of schematic diagrams, which were (and still are) described in
Appendix D
The second chapter, Chapter 6, introduces the Arduino nardo The Leonardo is a different sort of Arduino, because theUSB controller is implemented in software, and not in a separatechip as had been the case prior to the Leonardo This allows theUSB behaviour of the board to be modified
Leo-Apart from these new chapters, other updates have taken place:The Third Edition is written for version 1.0.5 of the IDE In antici-pation of the imminent release of version 1.5, differencesbetween 1.0.5 and 1.5 have been noted
Numerous suggestions from students and readers have beenincorporated
In keeping with the spirit of the original text, British spelling isused throughout
—Michael
Preface to the Second Edition
A few years ago I was given a very interesting challenge: teachdesigners the bare minimum in electronics so that they couldbuild interactive prototypes of the objects they were designing
I started following a subconscious instinct to teach electronicsthe same way I was taught in school Later on I realised that itsimply wasn’t working as well as I would like, and started toremember sitting in a class, bored like hell, listening to all that
viiwww.it-ebooks.info
Trang 10theory being thrown at me without any practical applicationfor it.
In reality, when I was in school I already knew electronics in avery empirical way: very little theory, but a lot of hands-on expe-rience
I started thinking about the process by which I really learnedelectronics:
• I took apart any electronic device I could put my hands on
• I slowly learned what all those components were
• I began to tinker with them, changing some of the tions inside of them and seeing what happened to thedevice: usually something between an explosion and a puff
connec-of smoke
• I started building some kits sold by electronics magazines
• I combined devices I had hacked, and repurposed kits andother circuits that I found in magazines to make them donew things
As a little kid, I was always fascinated by discovering how thingswork; therefore, I used to take them apart This passion grew as
I targeted any unused object in the house and then took it apartinto small bits Eventually, people brought all sorts of devices for
me to dissect My biggest projects at the time were a washer and an early computer that came from an insuranceoffice, which had a huge printer, electronics cards, magneticcard readers, and many other parts that proved very interestingand challenging to completely take apart
dish-After quite a lot of this dissecting, I knew what electronic ponents were and roughly what they did On top of that, myhouse was full of old electronics magazines that my father musthave bought at the beginning of the 1970s I spent hours readingthe articles and looking at the circuit diagrams without under-standing very much
com-This process of reading the articles over and over, with the fit of knowledge acquired while taking apart circuits, created aslow, virtuous circle
bene-viii Preface
www.it-ebooks.info
Trang 11A great breakthrough came one Christmas, when my dad gave
me a kit that allowed teenagers to learn about electronics Everycomponent was housed in a plastic cube that would magneti-cally snap together with other cubes, establishing a connection;the electronic symbol was written on top Little did I know thatthe toy was also a landmark of German design, because DieterRams designed it back in the 1960s
With this new tool, I could quickly put together circuits and trythem out to see what happened The prototyping cycle was get-ting shorter and shorter
After that, I built radios, amplifiers, circuits that would producehorrible noises and nice sounds, rain sensors, and tiny robots.I’ve spent a long time looking for an English word that wouldsum up that way of working without a specific plan, starting withone idea and ending up with a completely unexpected result
Finally, tinkering came along I recognised how this word has
been used in many other fields to describe a way of operatingand to portray people who set out on a path of exploration Forexample, the generation of French directors who gave birth to
the Nouvelle Vague were called the tinkerers The best definition
of tinkering that I’ve ever found comes from an exhibition held atthe Exploratorium in San Francisco:
Tinkering is what happens when you try
some-thing you don’t quite know how to do, guided by
whim, imagination, and curiosity When you
tinker, there are no instructions—but there are
also no failures, no right or wrong ways of doing
things It’s about figuring out how things work
and reworking them
Contraptions, machines, wildly mismatched
objects working in harmony—this is the stuff of
tinkering
Tinkering is, at its most basic, a process that
marries play and inquiry
From my early experiments I knew how much experience youwould need in order to be able to create a circuit that would dowhat you wanted, starting from the basic components
Preface ixwww.it-ebooks.info
Trang 12Another breakthrough came in the summer of 1982, when Iwent to London with my parents and spent many hours visitingthe Science Museum They had just opened a new wing dedica-ted to computers, and by following a series of guided experi-ments, I learned the basics of binary math and programming.There I realised that in many applications, engineers were nolonger building circuits from basic components, but wereinstead implementing a lot of the intelligence in their productsusing microprocessors Software was replacing many hours ofelectronic design, and would allow a shorter tinkering cycle.When I came back, I started to save money, because I wanted tobuy a computer and learn how to program.
My first and most important project after that was using mybrand-new ZX81 computer to control a welding machine I know
it doesn’t sound like a very exciting project, but there was aneed for it and it was a great challenge for me, because I hadjust learned how to program At this point, it became clear thatwriting lines of code would take less time than modifying com-plex circuits
Twenty-odd years later, I’d like to think that this experienceallows me to teach people who don’t even remember taking anymath class and to infuse them with the same enthusiasm andability to tinker that I had in my youth and have kept ever since
—Massimo
Acknowledgments for Massimo
Banzi
This book is dedicated to Ombretta
Acknowledgments for Michael
Shiloh
This book is dedicated to my brother and my parents
First of all I’d like to thank Massimo for inviting me to work onthe Third Edition of this book, and for inviting me to join Arduino
x Preface
www.it-ebooks.info
Trang 13in general It’s been a real privilege and joy to participate in thisproject.
Brian Jepson for guidance, oversight, encouragement, and port Frank Teng for keeping me on track Kim Cofer and NicoleShelby for doing a wonderful job of copyediting and productionediting, respectively
sup-My daughter Yasmine for thinking so highly of me, for her ending support and encouragement of my pursuing my inter-ests, and for still thinking that I’m kinda cool in spite of being herdad I could not have done this without her support
never-Last but not least, my partner Judy Aime’ Castro for the endlesshours she spent turning my scribbles into fine illustrations, fordiscussing various aspects of the book, and for her endlesspatience with me I could not have done this without her supporteither
Conventions Used in This Book
The following typographical conventions are used in this book:
Constant width bold
Shows commands or other text that should be typed literally
by the user
Constant width italic
Shows text that should be replaced with user-supplied ues or by values determined by context
val-Preface xi
This icon signifies a tip, suggestion, or general note
www.it-ebooks.info
Trang 14Using Code Examples
This book is here to help you get your job done In general, youmay use the code in this book in your programs and documen-tation You do not need to contact us for permission unlessyou’re reproducing a significant portion of the code For exam-ple, writing a program that uses several chunks of code fromthis book does not require permission Selling or distributing aCD-ROM of examples from Make: books does require permis-sion Answering a question by citing this book and quotingexample code does not require permission Incorporating a sig-nificant amount of example code from this book into your prod-uct’s documentation does require permission
We appreciate, but do not require, attribution An attributionusually includes the title, author, publisher, and ISBN For exam-
ple: “Getting Started With Arduino, Third Edition, by Massimo
Banzi and Michael Shiloh (Maker Media) Copyright 2015 simo Banzi and Michael Shiloh, 978-1-4493-6333-8.”
Mas-If you feel your use of code examples falls outside fair use or thepermission given here, feel free to contact us at bookpermis- sions@makermedia.com
Safari® Books Online
Safari Books Online is an on-demand digital library that deliversexpert content in both book and video form from the world’sleading authors in technology and business
Technology professionals, software developers, web designers,and business and creative professionals use Safari Books Online
as their primary resource for research, problem solving, ing, and certification training
learn-Safari Books Online offers a range of plans and pricing for prise, government, education, and individuals
enter-xii Preface
This icon indicates a warning or caution
www.it-ebooks.info
Trang 15Members have access to thousands of books, training videos,and prepublication manuscripts in one fully searchable data-base from publishers like Maker Media, O’Reilly Media, PrenticeHall Professional, Addison-Wesley Professional, Microsoft Press,Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley
& Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt,Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more.For more information about Safari Books Online, please visit us
com-an audience, it’s a worldwide movement that Make is leading—
we call it the Maker Movement
Preface xiiiwww.it-ebooks.info
Trang 16For more information about Make:, visit us online:
Make: magazine: http://makezine.com/magazine
Maker Faire: http://makerfaire.com
Makezine.com: http://makezine.com
Maker Shed: http://makershed.com
We have a kit with the components needed to try most of theexamples (through the end of Chapter 7) available from the
Maker Shed
We also have a web page for this book, where we list errata,examples, corrections to the code, and any additional informa-tion You can access this page at http://bit.ly/start_arduino_3e.For more information about Arduino, including discussionforums and further documentation, see http://www.arduino.cc
To comment or ask technical questions about this book, sendemail to: bookquestions@oreilly.com
xiv Preface
www.it-ebooks.info
Trang 17Arduino is an open source physical computing platform for
cre-ating interactive objects that stand alone or collaborate withsoftware on your computer Arduino was designed for artists,designers, and others who want to incorporate physical com-puting into their designs without having to first become electri-cal engineers
The Arduino hardware and software is open source The opensource philosophy fosters a community that shares its knowl-edge generously This is great for beginners as help is oftenavailable geographically nearby and always online, at many dif-ferent skill levels, and on a bewildering array of topics Exampleprojects are presented not just as pictures of the finishedproject, but include instructions for making your own or as astarting point for incorporation into your derivative or relatedprojects
The Arduino software, known as the Integrated DevelopmentEnvironment (IDE), is free You can download it from www.ardu- ino.cc The Arduino IDE is based on the Processing language,which was developed to help artists create computer art withouthaving to first become software engineers The Arduino IDE canrun on Windows, Macintosh, and Linux
The Arduino board is inexpensive (about $30) and quite tolerant
of common novice mistakes If you do somehow manage todamage the main component on the Arduino Uno, it can bereplaced for as little as $4
The Arduino project was developed in an educational ment and is a very popular educational tool The same opensource philosophy that created the community which gener-ously shares information, answers, and projects also sharesteaching methods, curricula, and other information Arduinohas a special mailing list to facilitate discussion among anyoneinterested in teaching with or about Arduino
environ-1www.it-ebooks.info
Trang 18Because the Arduino hardware and software are open source,you can download the Arduino hardware design and build yourown, or use it as a starting point for your own project, based on(or incorporating) Arduino within its design, or simply to under-stand how Arduino works You can do the same things with thesoftware.
This book is designed to help beginners with no prior experienceget started with Arduino
Intended Audience
This book was written for the “original” Arduino users: ers and artists Therefore, it tries to explain things in a way thatmight drive some engineers crazy Actually, one of them calledthe introductory chapters of the first draft “fluff” That’s pre-cisely the point Let’s face it: most engineers aren’t able toexplain what they do to another engineer, let alone a regularhuman being Let’s now delve deep into the fluff
design-This book is not meant to be a textbook for teaching electronics
or programming, but you will learn something about electronicsand programming while reading this book
After Arduino started to become popular, I
real-ised how experimenters, hobbyists, and hackers
of all sorts were starting to use it to create
beau-tiful and crazy objects I realised that you’re all
artists and designers in your own right, so this
book is for you as well
—Massimo
2 Getting Started with Arduino
Arduino builds upon the thesis work Hernando ragan did on the Wiring platform while studyingunder Casey Reas and me (Massimo) at InteractionDesign Institute Ivrea (IDII)
Bar-www.it-ebooks.info
Trang 19What Is Interaction Design?
Arduino was born to teach Interaction Design, a design pline that puts prototyping at the centre of its methodology.There are many definitions of Interaction Design, but the onethat we prefer is this:
disci-Interaction Design is the design of any interactive experience.
In today’s world, Interaction Design is concerned with the ation of meaningful experiences between us (humans) andobjects It is a good way to explore the creation of beautiful—and maybe even controversial—experiences between us andtechnology Interaction Design encourages design through aniterative process based on prototypes of ever-increasing fidelity.This approach—also part of some types of conventional design
cre-—can be extended to include prototyping with technology; inparticular, prototyping with electronics
The specific field of Interaction Design involved with Arduino isphysical computing (or Physical Interaction Design)
What Is Physical Computing?
Physical computing uses electronics to prototype new objectsfor designers and artists It involves the design of interactiveobjects that can communicate with humans by using sensorsand actuators controlled by a behaviour implemented as soft-ware running inside a microcontroller (a small computer on asingle chip)
In the past, using electronics meant having to deal with neers all the time, and building circuits one small component at
engi-a time; these issues kept creengi-ative people from plengi-aying engi-aroundwith the medium directly Most of the tools were meant for engi-neers and required extensive knowledge
In recent years, microcontrollers have become cheaper and ier to use At the same time, computers have become faster andmore powerful, allowing the creation of better (and easier)development tools
eas-The progress that we have made with Arduino is to bring thesetools one step closer to the novice, allowing people to start
Introduction 3www.it-ebooks.info
Trang 20building stuff after only two or three days of a workshop WithArduino, a designer or artist can get to know the basics of elec-tronics and sensors very quickly and can start building proto-types with very little investment.
4 Getting Started with Arduino
www.it-ebooks.info
Trang 212/The Arduino Way
The Arduino philosophy is based on making designs rather thantalking about them It is a constant search for faster and morepowerful ways to build better prototypes We have exploredmany prototyping techniques and developed ways of thinkingwith our hands
Classic engineering relies on a strict process for getting from A
to B; the Arduino Way delights in the possibility of getting lost onthe way and finding C instead
This is the tinkering process that we are so fond of—playing withthe medium in an open-ended way and finding the unexpected
In this search for ways to build better prototypes, we alsoselected a number of software packages that enable the pro-cess of constant manipulation of the software and hardwaremedium
The next few sections present some philosophies, events, andpioneers that have inspired the Arduino Way
Prototyping
Prototyping is at the heart of the Arduino Way: we make thingsand build objects that interact with other objects, people, andnetworks We strive to find a simpler and faster way to prototype
in the cheapest possible way
A lot of beginners approaching electronics for the first timethink that they have to learn how to build everything fromscratch This is a waste of energy: what you want is to be able toconfirm that something’s working very quickly so that you canmotivate yourself to take the next step or maybe even motivatesomebody else to give you a lot of cash to do it
This is why we developed opportunistic prototyping: why spend
time and energy building from scratch, a process that requirestime and in-depth technical knowledge, when we can take
5www.it-ebooks.info
Trang 22ready-made devices and hack them in order to exploit the hardwork done by large companies and good engineers?
Our hero is James Dyson, who made 5127 prototypes of his uum cleaner before he was satisfied that he’d gotten it right
vac-Tinkering
We believe that it is essential to play with technology, exploringdifferent possibilities directly on hardware and software—some-times without a very defined goal
Reusing existing technology is one of the best ways of tinkering.Getting cheap toys or old discarded equipment and hackingthem to make them do something new is one of the best ways toget to great results
6 Getting Started with Arduino
www.it-ebooks.info
Trang 23I have always been fascinated by modularity and
the ability to build complex systems by
connect-ing together simple devices This process is very
well represented by Robert Moog and his
ana-logue synthesizers Musicians constructed
sounds, trying endless combinations by
patch-ing together different modules with cables This
approach made the synthesizer look like an old
telephone switch, but combined with the
numerous knobs, that was the perfect platform
for tinkering with sound and innovating music
Moog described it as a process between
“wit-nessing and discovering” I’m sure most
musi-cians at first didn’t know what all those
hun-dreds of knobs did, but they tried and tried,
refining their own style with no interruptions in
the flow
—Massimo
Reducing the number of interruptions to the flow is very tant for creativity—the more seamless the process, the moretinkering happens
impor-This technique has been translated into the world of software byvisual programming environments like Max, Pure Data, or VVVV
These tools can be visualised as boxes for the different alities that they provide, letting the user build patches by con-
function-necting these boxes together These environments let the userexperiment with programming without the constant interrup-tion typical of the usual cycle: “type program, compile, damn—there is an error, fix error, compile, run” If you are more visuallyminded, we recommend that you try them out
The Arduino Way 7www.it-ebooks.info
Trang 248 Getting Started with Arduino
www.it-ebooks.info
Trang 25Circuit Bending
Circuit bending is one of the most interesting forms of tinkering.It’s the creative short-circuiting of low-voltage, battery-poweredelectronic audio devices such as guitar effect pedals, children’stoys, and small synthesizers to create new musical instrumentsand sound generators The heart of this process is the “art ofchance” It began in 1966 when Reed Ghazala, by chance,shorted-out a toy amplifier against a metal object in his deskdrawer, resulting in a stream of unusual sounds Circuit bendersexcel in their ability to create the wildest devices by tinkeringaway with technology without necessarily understanding whatthey are doing on the theoretical side
The Arduino Way 9www.it-ebooks.info
Trang 26It’s a bit like the Sniffin’ Glue fanzine shown here: during the
punk era, knowing three chords on a guitar was enough to start
a band Don’t let the experts in one field tell you that you’ll never
be one of them Ignore them and surprise them
10 Getting Started with Arduino
www.it-ebooks.info
Trang 27Keyboard Hacks
Computer keyboards are still the main way to interact with acomputer after more than 60 years Alex Pentland, academichead of the MIT Media Laboratory, once remarked: “Excuse the
The Arduino Way 11www.it-ebooks.info
Trang 281 Quoted in Sara Reese Hedberg’s “MIT Media Lab’s quest for perceptive
computers,” Intelligent Systems and Their Applications, IEEE, Jul/Aug
soft-We Love Junk!
People throw away a lot of technology these days: old printers,computers, weird office machines, technical equipment, andeven a lot of military stuff There has always been a big marketfor this surplus technology, especially among young and/orpoorer hackers and those who are just starting out This marketbecame evident in Ivrea, where we developed Arduino The cityused to be the headquarters of the Olivetti company They hadbeen making computers since the 1960s; in the mid 1990s, theythrew everything away in junkyards in the area These are full ofcomputer parts, electronic components, and weird devices of allkinds We spent countless hours there, buying all sorts of con-traptions for very little money and hacking into our prototypes.When you can buy a thousand loudspeakers for very littlemoney, you’re bound to come up with some idea in the end.Accumulate junk and go through it before starting to buildsomething from scratch
12 Getting Started with Arduino
www.it-ebooks.info
Trang 29Hacking Toys
Toys are a fantastic source of cheap technology to hack andreuse, as evidenced by the practise of circuit bending men-tioned earlier With the current influx of thousands of very cheaphigh-tech toys from China, you can build quick ideas with a fewnoisy cats and a couple of light swords
I have been doing this for a few years to get my
students to understand that technology is not
scary or difficult to approach One of my
favour-ite resources is the booklet “Low Tech Sensors
and Actuators” by Usman Haque and Adam
Somlai-Fischer I think they have perfectly
The Arduino Way 13www.it-ebooks.info
Trang 30described this technique in that handbook, and I
have been using it ever since
—Massimo
Collaboration
Collaboration between users is one of the key principles in theArduino world—through the forum at www.arduino.cc, peoplefrom different parts of the world help each other learn about theplatform The Arduino team encourages people to collaborate at
a local level as well by helping them set up users’ groups inevery city they visit We also set up a wiki called “Playground”
where users document their findings It’s so amazing to see howmuch knowledge these people pour out on the Web for every-body to use
This culture of sharing and helping each other is
one of the things that I’m most proud of in
regard to Arduino
—Massimo
14 Getting Started with Arduino
www.it-ebooks.info
Trang 313/The Arduino
Platform
Arduino is composed of two major parts: the Arduino board,which is the piece of hardware you work on when you build yourobjects; and the Arduino Integrated Development Environment,
or IDE, the piece of software you run on your computer You use
the IDE to create a sketch (a little computer program) that you
upload to the Arduino board The sketch tells the board what
to do
Not too long ago, working on hardware meant building circuitsfrom scratch, using hundreds of different components withstrange names like resistor, capacitor, inductor, transistor, and
so on Every circuit was wired to do one specific application, andmaking changes required you to cut wires, solder connections,and more
With the appearance of digital technologies and sors, these functions, which were once implemented with wires,were replaced by software Software is easier to modify thanhardware With a few keypresses, you can radically change thelogic of a device and try two or three versions in the sameamount of time that it would take you to solder a couple ofresistors
microproces-The Arduino Hardware
The Arduino board is a small microcontroller board, which is asmall circuit (the board) that contains a whole computer on asmall chip (the microcontroller)
15www.it-ebooks.info
Trang 32This computer is at least a thousand times less
powerful than the MacBook I’m using to write
this, but it’s a lot cheaper and very useful for
building interesting devices
—Massimo
Look at the Arduino Uno board: you’ll see a rectangular blackpiece of plastic with 28 “legs” (or possibly a tiny square piece ofplastic if you have the SMD edition)—that chip is theATmega328, the heart of your board
We (the Arduino team) have placed on this board all the nents that are required for this microcontroller to work properlyand to communicate with your computer There are many ver-sions of this board; the one we’ll use throughout this book is theArduino Uno, which is the simplest one to use and the best onefor learning on Almost everything we’ll talk about applies to allArduinos, including the most recent ones as well as the earlierones Figure 3-1 shows the Arduino Uno
compo-In Figure 3-1, you see that the Arduino has a row of strips at thetop and the bottom with lots of labels These strips are the con-
nectors, which are used to attach to sensors and actuators (An
actuator is the opposite of a sensor: a sensor senses something
in the physical world and converts it to a signal a computer canunderstand, while an actuator converts a signal from a com-puter into an act in the physical world You’ll learn much moreabout sensors and actuators in this book.)
At first, all those connectors might be a little confusing Here is
an explanation of the input and output pins you’ll learn to use inthis book Don’t worry if you’re still confused after reading this—there are many new concepts in this book that might take you awhile to get used to We’ll repeat these explanations a number of
16 Getting Started with Arduino
In fact, there are a variety of Arduino boards, but themost common one by far is the Arduino Uno, which
is described here In Chapter 6 you’ll learn about one
of the other Arduino boards
www.it-ebooks.info
Trang 33different ways, and they’ll especially start making sense to youonce you start building circuits and experiencing the results.
14 Digital I/O pins (pins 0–13)
These pins can be either inputs or outputs Inputs are used
to read information from sensors, while outputs are used tocontrol actuators You will specify the direction (in or out) inthe sketch you create in the IDE Digital inputs can only readone of two values, and digital outputs can only output one oftwo values (HIGH and LOW)
6 Analogue In pins (pins 0–5)
The analogue input pins are used for reading voltage urements from analogue sensors In contrast to digitalinputs, which can distinguish between only two different lev-els (HIGH and LOW), analogue inputs can measure 1,024 dif-ferent levels of voltage
meas-6 Analogue Out pins (pins 3, 5, meas-6, 9, 10, and 11)
These are actually six of the digital pins that can perform athird function: they can provide analogue output As with thedigital I/O pins, you specify what the pin should do in yoursketch
The board can be powered from your computer’s USB port,most USB chargers, or an AC adapter (9 volts recommended,2.1 mm barrel tip, center positive) Whenever power is provided
at the power socket, Arduino will use that, and if there is nopower at the power socket, Arduino will use power from the USBsocket It’s safe to have power at both the power socket and theUSB socket
The Arduino Platform 17www.it-ebooks.info
Trang 34Figure 3-1 The Arduino Uno
The Software Integrated
Development Environment (IDE)
The IDE is a special program running on your computer thatallows you to write sketches for the Arduino board in a simplelanguage modeled after the Processing language The magichappens when you press the button that uploads the sketch tothe board: the code that you have written is translated into the Clanguage (which is generally quite hard for a beginner to use),
and is passed to the avr-gcc compiler, an important piece of
open source software that makes the final translation into thelanguage understood by the microcontroller This last step isquite important, because it’s where Arduino makes your lifesimple by hiding away most of the complexities of programmingmicrocontrollers
The programming cycle on Arduino is basically as follows:
1 Plug your board into a USB port on your computer
18 Getting Started with Arduino
www.it-ebooks.info
Trang 352 Write a sketch that will bring the board to life.
3 Upload this sketch to the board through the USB connectionand wait a couple of seconds for the board to restart
4 The board executes (performs) the sketch that you wrote
Installing Arduino on Your
Computer
To program the Arduino board, you must first install the IDE bydownloading the appropriate file from the Arduino website.Choose the right version for your operating system, and thenproceed with the appropriate instructions in the following sec-tions
Installing the IDE: Macintosh
When the file download has finished, double-click to open it,which will open a disk image that contains the Arduino applica-tion
Drag the Arduino application into your Applications folder.
Configuring the Drivers: Macintosh
The Arduino Uno uses a driver provided by the Macintosh ating system, so there is nothing to install
oper-Now that the IDE is installed, connect your Arduino Uno to yourMacintosh via a USB cable
The green LED labeled PWR on the board should come on, and the yellow LED labeled L should start blinking.
The Arduino Platform 19
See the “Learning Linux” section on the Arduino site for Linux installation instructions
web-www.it-ebooks.info
Trang 36Now that you’ve configured the software, you need to select theproper port to communicate with the Arduino Uno.
Port Identification: Macintosh
Invoke the Arduino IDE, either through the Applications folder or
by using Spotlight
From the Tools menu in the Arduino IDE, select Serial Port and
then select the port that begins with /dev/cu.usbmodem
or /dev/tty.usbmodem Both of these ports refer to your
Ardu-ino board, and it makes no difference which one you select
Figure 3-2 shows the list of ports
Figure 3-2 The Arduino IDE’s list of serial ports on a Macintosh
20 Getting Started with Arduino
You might see a pop-up window telling you that anew network interface has been detected
If that happens, Click Network Preferences, andwhen it opens, click Apply The Uno will show up asNot Configured, but it’s working properly Quit Sys-tem Preferences
www.it-ebooks.info
Trang 37You’re almost done! The final thing you should check is thatArduino is configured for the type of board you’re using.
From the Tools menu in the Arduino IDE, select Board, and thenselect Arduino Uno If you have a different board, you’ll need toselect that board type (the name of the board is printed next tothe Arduino symbol)
Congratulations! Your Arduino software is installed, configured,and ready to use You’re ready to go on to Chapter 4
Installing the IDE: Windows
When the file download has finished, double-click to open theinstaller
You will be shown a license Read the license, and if you agreewith it, click the I Agree button
You will be given a list of components to install, and, by default,all of them will be selected Leave them all selected and clickNext
You will be asked to select an installation folder, and the installerwill propose a default for that Unless you have a good reasonnot to, accept the default and click Install
The installer will display its progress as it extracts and installsthe files
After the files are installed, a window will pop up asking for mission to install the drivers Click Install
per-When the installer has completed, click Close to finish
Configuring the Drivers: Windows
Now that the IDE is installed, connect your Arduino Uno to yourcomputer via a USB cable
The Arduino Platform 21
If you have trouble with any of these steps, see
Chapter 9, Troubleshooting
www.it-ebooks.info
Trang 38The green LED labeled PWR on the board should come on, and the yellow LED labeled L should start blinking.
The Found New Hardware Wizard window comes up, and dows should automatically find the right drivers
Win-Now that the driver has been configured, you need to select theproper port to communicate with the Arduino Uno
Port Identification: Windows
Run the Arduino IDE, either using a desktop shortcut or theStart menu
From the Tools menu in the Arduino IDE, select Serial Port Youwill see one or more COM ports with different numbers Make anote of which numbers are available
Now unplug your Arduino from your computer, look at the list ofports again, and see which COM port vanishes It might take amoment or two, and you may have to leave the Tools menu andopen it again to refresh the list of ports
Once you’ve figured out the COM port assignment, you canselect that port from the Tools→Serial Port menu in the ArduinoIDE
You’re almost done! The final thing you should check is thatArduino is configured for your type of board
From the Tools menu in the Arduino IDE, select Board and selectArduino Uno If you have a different board, you’ll need to select
22 Getting Started with Arduino
If you have trouble with any of these steps, see
“Problems Installing Drivers on Windows” on page
199 in Chapter 9
If you have trouble identifying the COM port used byyour Arduino Uno, see “Identifying the Arduino COMPort on Windows” on page 200 in Chapter 9
www.it-ebooks.info
Trang 39that board type (the name of the board is printed next to theArduino symbol).
Congratulations! Your Arduino software is installed, configured,and ready to use You’re ready to go on to Chapter 4
The Arduino Platform 23www.it-ebooks.info