Arduino Cookbook 1st Edition Michael Margolis pdf download
Arduino Cookbook 1st Edition Michael Margolis pdf download
download
https://ebookmeta.com/product/arduino-cookbook-1st-edition-
michael-margolis-3/
https://ebookmeta.com/product/arduino-cookbook-1st-edition-
michael-margolis/
https://ebookmeta.com/product/arduino-cookbook-1st-edition-
michael-margolis-3/
https://ebookmeta.com/product/arduino-cookbook-1st-edition-
michael-margolis-2/
https://ebookmeta.com/product/rossi-cosa-nostra-heirs-4-1st-
edition-atlas-rose-raven-scott/
The Currency Cold War Cash and Cryptography Hash Rates
and Hegemony 1st Edition Birch David
https://ebookmeta.com/product/the-currency-cold-war-cash-and-
cryptography-hash-rates-and-hegemony-1st-edition-birch-david/
https://ebookmeta.com/product/the-mountain-mans-muse-deep-woods-
alphas-mountain-man-stories-book-3-1st-edition-scarlett-winters-
winters/
https://ebookmeta.com/product/journeys-with-emperors-tracking-
the-worlds-most-extreme-penguin-1st-edition-gerald-l-kooyman/
https://ebookmeta.com/product/orthopedic-physical-assessment-
david-j-magee/
https://ebookmeta.com/product/strange-world-meet-the-clades-
disney/
Systematically Analysing Indirect Translations: Putting
the Concatenation Effect Hypothesis to the Test James
Luke Hadley
https://ebookmeta.com/product/systematically-analysing-indirect-
translations-putting-the-concatenation-effect-hypothesis-to-the-
test-james-luke-hadley/
www.it-ebooks.info
www.it-ebooks.info
Arduino Cookbook
by Michael Margolis
Copyright © 2011 Michael Margolis and Nicholas Weldin. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editors: Simon St. Laurent and Brian Jepson Indexer: Lucie Haskins
Production Editor: Teresa Elsey Cover Designer: Karen Montgomery
Copyeditor: Audrey Doyle Interior Designer: David Futato
Proofreader: Teresa Elsey Illustrator: Robert Romano
Printing History:
March 2011: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Arduino Cookbook, the image of a toy rabbit, and related trade dress are trademarks
of O’Reilly 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 O’Reilly 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 resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-80247-9
[LSI]
1299267108
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Installing the Integrated Development Environment (IDE) 4
1.2 Setting Up the Arduino Board 6
1.3 Using the Integrated Development Environment (IDE) to Prepare
an Arduino Sketch 8
1.4 Uploading and Running the Blink Sketch 11
1.5 Creating and Saving a Sketch 13
1.6 Using Arduino 15
www.it-ebooks.info
2.20 Performing Bitwise Operations 56
2.21 Combining Operations and Assignment 58
4. Serial Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.1 Sending Debug Information from Arduino to Your Computer 86
4.2 Sending Formatted Text and Numeric Data from Arduino 89
4.3 Receiving Serial Data in Arduino 92
4.4 Sending Multiple Text Fields from Arduino in a Single Message 95
4.5 Receiving Multiple Text Fields in a Single Message in Arduino 98
4.6 Sending Binary Data from Arduino 101
4.7 Receiving Binary Data from Arduino on a Computer 105
4.8 Sending Binary Values from Processing to Arduino 107
4.9 Sending the Value of Multiple Arduino Pins 109
4.10 How to Move the Mouse Cursor on a PC or Mac 112
4.11 Controlling Google Earth Using Arduino 115
4.12 Logging Arduino Data to a File on Your Computer 121
4.13 Sending Data to Two Serial Devices at the Same Time 124
4.14 Receiving Serial Data from Two Devices at the Same Time 128
4.15 Setting Up Processing on Your Computer to Send
and Receive Serial Data 131
vi | Table of Contents
www.it-ebooks.info
5.5 Reading a Keypad 149
5.6 Reading Analog Values 152
5.7 Changing the Range of Values 154
5.8 Reading More Than Six Analog Inputs 155
5.9 Displaying Voltages Up to 5V 158
5.10 Responding to Changes in Voltage 161
5.11 Measuring Voltages More Than 5V (Voltage Dividers) 162
www.it-ebooks.info
7.13 Controlling an Array of LEDs by Using MAX72xx Shift Registers 253
7.14 Increasing the Number of Analog Outputs Using PWM Extender
Chips (TLC5940) 255
7.15 Using an Analog Panel Meter As a Display 259
www.it-ebooks.info
11.2 Formatting Text 337
11.3 Turning the Cursor and Display On or Off 340
11.4 Scrolling Text 342
11.5 Displaying Special Symbols 345
11.6 Creating Custom Characters 347
11.7 Displaying Symbols Larger Than a Single Character 349
11.8 Displaying Pixels Smaller Than a Single Character 352
11.9 Connecting and Using a Graphical LCD Display 355
11.10 Creating Bitmaps for Use with a Graphical Display 359
11.11 Displaying Text on a TV 361
Table of Contents | ix
www.it-ebooks.info
15.6 Setting Up an Arduino to Be a Web Server 469
15.7 Handling Incoming Web Requests 471
15.8 Handling Incoming Requests for Specific Pages 474
15.9 Using HTML to Format Web Server Responses 479
15.10 Serving Web Pages Using Forms (POST) 483
15.11 Serving Web Pages Containing Large Amounts of Data 486
15.12 Sending Twitter Messages 493
15.13 Sending and Receiving Simple Messages (UDP) 496
15.14 Getting the Time from an Internet Time Server 502
15.15 Monitoring Pachube Feeds 507
15.16 Sending Information to Pachube 510
x | Table of Contents
www.it-ebooks.info
C. Building and Connecting the Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Table of Contents | xi
www.it-ebooks.info
Preface
This book was written by Michael Margolis with Nick Weldin to help you explore the
amazing things you can do with Arduino.
Arduino is a family of microcontrollers (tiny computers) and a software creation envi-
ronment that makes it easy for you to create programs (called sketches) that can interact
with the physical world. Things you make with Arduino can sense and respond to
touch, sound, position, heat, and light. This type of technology, often referred to as
physical computing, is used in all kinds of things, from the iPhone to automobile elec-
tronics systems. Arduino makes it possible for anyone—even people with no program-
ming or electronics experience—to use this rich and complex technology.
xiii
www.it-ebooks.info
How This Book Is Organized
The book contains information that covers the broad range of the Arduino’s capabili-
ties, from basic concepts and common tasks to advanced technology. Each technique
is explained in a recipe that shows you how to implement a specific capability. You do
not need to read the content in sequence.
Chapter 1, Getting Started, introduces the Arduino environment and provides help on
getting the Arduino development environment and hardware installed and working.
The next couple of chapters introduce Arduino software development. Chapter 2,
Making the Sketch Do Your Bidding, covers essential software concepts and tasks, and
Chapter 3, Using Mathematical Operators, shows how to make use of the most common
mathematical functions.
Chapter 4, Serial Communications, describes how to get Arduino to connect and com-
municate with your computer and other devices. Serial is the most common method
for Arduino input and output, and this capability is used in many of the recipes
throughout the book.
Chapter 5, Simple Digital and Analog Input, introduces a range of basic techniques for
reading digital and analog signals. Chapter 6, Getting Input from Sensors, builds on this
with recipes that explain how to use devices that enable Arduino to sense touch, sound,
position, heat, and light.
Chapter 7, Visual Output, covers controlling light. Recipes cover switching on one or
many LEDs and controlling brightness and color. This chapter explains how you can
drive bar graphs and numeric LED displays, as well as create patterns and animations
with LED arrays. In addition, the chapter provides a general introduction to digital and
analog output for those who are new to this.
Chapter 8, Physical Output, explains how you can make things move by controlling
motors with Arduino. A wide range of motor types are covered: solenoids, servo motors,
DC motors, and stepper motors.
Chapter 9, Audio Output, shows how to generate sound with Arduino through an out-
put device such as a speaker. It covers playing simple tones and melodies and playing
WAV files and MIDI.
Chapter 10, Remotely Controlling External Devices, describes techniques that can be
used to interact with almost any device that uses some form of remote controller, in-
cluding TV, audio equipment, cameras, garage doors, appliances, and toys. It builds
on techniques used in previous chapters for connecting Arduino to devices and
modules.
Chapter 11, Using Displays, covers interfacing text and graphical LCD displays. The
chapter shows how you can connect these devices to display text, scroll or highlight
words, and create special symbols and characters.
xiv | Preface
www.it-ebooks.info
Chapter 12, Using Time and Dates, covers built-in Arduino time-related functions and
introduces many additional techniques for handling time delays, time measurement,
and real-world times and dates.
Chapter 13, Communicating Using I2C and SPI, covers the Inter-Integrated Circuit
(I2C) and Serial Peripheral Interface (SPI) standards. These standards provide simple
ways for digital information to be transferred between sensors and Arduino. This chap-
ter shows how to use I2C and SPI to connect to common devices. It also shows how to
connect two or more Arduino boards, using I2C for multiboard applications.
Chapter 14, Wireless Communication, covers wireless communication with XBee. This
chapter provides examples ranging from simple wireless serial port replacements to
mesh networks connecting multiple boards to multiple sensors.
Chapter 15, Ethernet and Networking, describes the many ways you can use Arduino
with the Internet. It has examples that demonstrate how to build and use web clients
and servers and shows how to use the most common Internet communication protocols
with Arduino.
Arduino software libraries are a standard way of adding functionality to the Arduino
environment. Chapter 16, Using, Modifying, and Creating Libraries, explains how to
use and modify software libraries. It also provides guidance on how to create your own
libraries.
Chapter 17, Advanced Coding and Memory Handling, covers advanced programming
techniques, and the topics here are more technical than the other recipes in this book
because they cover things that are usually concealed by the friendly Arduino wrapper.
The techniques in this chapter can be used to make a sketch more efficient—they can
help improve performance and reduce the code size of your sketches.
Chapter 18, Using the Controller Chip Hardware, shows how to access and use hard-
ware functions that are not fully exposed through the documented Arduino language.
It covers low-level usage of the hardware input/output registers, timers, and interrupts.
Appendix A, Electronic Components, provides an overview of the components used
throughout the book.
Appendix B, Using Schematic Diagrams and Data Sheets, explains how to use schematic
diagrams and data sheets.
Appendix C, Building and Connecting the Circuit, provides a brief introduction to using
a breadboard, connecting and using external power supplies and batteries, and using
capacitors for decoupling.
Appendix D, Tips on Troubleshooting Software Problems, provides tips on fixing com-
pile and runtime problems.
Appendix E, Tips on Troubleshooting Hardware Problems, covers problems with elec-
tronic circuits.
Preface | xv
www.it-ebooks.info
Appendix F, Digital and Analog Pins, provides tables indicating functionality provided
by the pins on standard Arduino boards.
Appendix G, ASCII and Extended Character Sets, provides tables showing ASCII
characters.
Rather than the following, commonly used by experienced programmers, that does the
same thing:
result++; // increment using the post increment operator
xvi | Preface
www.it-ebooks.info
Feel free to substitute your preferred style. Beginners should be reassured that there is
no benefit in performance or code size in using the terse form.
Some programming expressions are so common that they are used in their terse form.
For example, the loop expressions are written as follows:
for(int i=0; i < 4; i++)
See Chapter 2 for more details on these and other expressions used throughout the
book.
Good programming practice involves ensuring that values used are valid (garbage in
equals garbage out) by checking them before using them in calculations. However, to
keep the code focused on the recipe topic, very little error-checking code has been
included.
Preface | xvii
www.it-ebooks.info
Exploring the Variety of Random
Documents with Different Content
TWO NEW BALLETS
The Examiner.
February 9, 1817.
There have been two new ballets this week, one at each Theatre. That at Drury-
Lane, Patrick’s Return, is one of the prettiest things we have seen a long time. The
dancing and pantomime are very delightfully adapted to a number of old Irish
melodies, which we are never tired of hearing.—Zephyr and Flora, at Covent-
Garden, is too fine by half for our rude tastes. There are lusty lovers flying in the
air, nests of winged Cupids, that start out of bulrushes, trees that lift up their
branches like arms:—we suppose they will speak next like Virgil’s wood. But in the
midst of all these wonders, we have a more amiable wonder, the three Miss
Dennetts, as nymphs,
They might represent Love, Hope, and Joy. There is one part in which they seem
to dance on the strings of the harp which plays to them; the liquid sounds and the
motion are the same. These young ladies put us in mind of Florizel’s praise of
Perdita:—
We noticed more than one of these double meanings, which however passed off
without any marks of reprobation, for unless they are made pretty broad, the
audience, from being accustomed to the cautious purity of the modern drama, are
not very expert in decyphering the equivocal allusion.—All the characters in the
Double Gallant are very well kept up, and they were most of them well supported
in the representation. At-All and Lady Dainty are the two most prominent
characters in the original comedy, and those into which Cibber has put most of his
own nature and genius. They are the essence of active impertinence and sickly
affectation. At-All has three intrigues upon his hands at once, and manages them
all with the dexterity with which an adept shuffles a pack of cards. His cool
impudence is equal to his wonderful vivacity. He jumps, by mere volubility of
tongue and limbs, under three several names into three several assignations with
three several incognitas, whom he meets at the same house, as they happen to be
mutual friends. He would succeed with them all, but that he is detected by them all
round, and then he can hardly be said to fail, for he carries off the best of them at
last (Mrs. Mardyn), who not being able to seduce him from her rivals by any other
means, resorts to a disguise, and vanquishes him in love by disarming him in a
duel. The scene in which At-All, who had made love to Clorinda as Colonel
Standfast, is introduced to her by her cousin (who is also in love with him) as Mr.
Freeman, and while he is disowning his personal identity, is surprised by the
arrival of Lady Sadlife, to whom he had been making the same irresistible
overtures, is one of the best coup d’œils of the theatre we have seen for a long time.
Harley acts this character laughably, but not very judiciously. He bustles through it
with the liveliness of a footman, not with the manners of a gentleman. He never
changes his character with his dress, but still he is a pleasant fellow in himself, and
is so happy in the applause he receives, that we are sorry to find any fault with him.
Mrs. Alsop’s Lady Dainty was a much better, but a much less agreeable piece of
acting. The affected sensibility, the pretended disorders, the ridiculous admiration
of novelty, and the languid caprices of this character, were given by the actress
with an overpowering truth of effect. The mixture of folly, affectation, pride,
insensibility, and spleen which constitute the character of the fine lady, as it
existed in the days of Cibber, and is delineated in this comedy, is hardly to be
tolerated in itself, with every advantage of grace, youth, beauty, dress, and fashion.
But Mrs. Alsop gave only the inherent vice and ridiculous folly of the character,
without any external accomplishments to conceal or adorn it. She has always the
same painful ‘frontlet’ on: the same uneasy expression of face and person. Her
affected distortions seemed to arise from real pain; nor was her delight in mischief
and absurdity counteracted by any palliating circumstances of elegance or beauty.
A character of this description ought only to appeal to the understanding, and not
to offend the senses. We do not know how to soften this censure; but we will add,
that Mrs. Alsop, in all her characters, shews sense, humour, and spirit.
Dowton and Miss Kelly, as Sir Solomon Sadlife and Wishwell, are two for a pair.
We do not wish to see a better actor or actress. The effect which both these
performers produce, is the best and strongest that can be, because they never try to
produce an effect. Their style of acting is the reverse of grimace or caricature. They
never overcharge or force any thing, and their humour is so much the more
irresistible in its appeal, as it seems to come from them in spite of themselves.
Instead of wanting to shew their talents to the audience, they seem hardly
conscious of them themselves. All their excellence is natural, unaffected,
involuntary. When the sense of absurdity is so strong that it cannot be contained
any longer, it bursts out; and the expression of their feelings commands our
sympathy, because they do not appear to court it. Their nature is downright sturdy,
sterling, good old English nature, that is, the sort of nature that we like best. In the
present play, it is hard to determine which is the best—Miss Kelly’s sulky
suppressed abigail airs as Wishwell, her adroit irony and contemptuous expression
of pity for Sir Solomon’s credulity, or Dowton’s deliberate manner of digesting his
disgraces, chewing the cud of his misfortunes, and pocketing up his branching
horns, in the latter character. Wishwell’s tingling fingers, uplifted eyes, pouting
mouth, bridling chin, and Sir Solomon’s bronzed face, curling lips, blank looks,
nods, winks, and shrugs, told their own story and kept their own secret (to
themselves), as well as heart could wish. We have a stronger relish for this kind of
dry pungent humour, than we have for the taste of olives.
She was less successful in the execution of the song to Massetto just after, ‘Batte,
batte, Massetto:’ for she seemed to sing it as if she had hardly learned it by heart.
To this, however, she gave a characteristic simplicity of expression; she appeared
in the first part as if she would willingly stand like a lamb, come agnellina, to be
beaten by her provoked lover, and afterwards, when she is reconciled to him, as if
she was glad she had escaped a beating. Her song, Vedrai carino, promising him a
remedy, when Massetto himself gets beaten, by offering him her heart, was
charming, both from the execution of the air, and from the action with which she
accompanied it.
Of the other performers we cannot speak so favourably. Signor Ambrogetti gave
considerable life and spirit to the part of Don Giovanni; but we neither saw the
dignified manners of the Spanish nobleman, nor the insinuating address of the
voluptuary. He makes too free and violent a use of his legs and arms. He sung the
air, Finche dal vino, in which he anticipates an addition to his list of mistresses
from the success of his entertainment, with a sort of jovial turbulent vivacity, but
without the least ‘sense of amorous delight.’ His only object seemed to be, to sing
the words as loud and as fast as possible. Nor do we think he gave to Don Juan’s
serenade, Deh vieni alla finestra, any thing like the spirit of fluttering
apprehension and tenderness which characterises the original music. Signor
Ambrogetti’s manner of acting in this scene was that of the successful and
significant intriguer, but not of an intriguer—in love. Sensibility should be the
ground-work of the expression: the cunning and address are only accessories.
Naldi’s Laporello was much admired, and it was not without its merits, though
we cannot say that it gave us much pleasure. His humour is coarse and boisterous,
and is more that of a buffoon than a comic actor. He treats the audience with the
same easy cavalier airs that an impudent waiter at a French table-d’hôte does the
guests as they arrive. The gross familiarity of his behaviour to Donna Elvira, in the
song where he makes out the list of his master’s mistresses, was certainly not in
character; nor is there any thing in the words or the music to justify it. The tone
and air which he should assume are those of pretended sympathy, mixed with
involuntary laughter, not of wanton undisguised insult.
Signor Crivelli and Madame Camporese did not add any particular prominence
to the serious parts of Don Octavio, and Donna Anna. Signora Hughes’s Donna
Elvira was successful beyond what we could have supposed. This lady at the Italian
Opera is respectable: on the English stage she was formidable. Signor Angrisani
doubles the part of Massetto and the Ghost. In the former, he displayed much
drollery and naiveté; and in the latter, he was as solemn, terrific, and mysterious
as a ghost should be. A new translation accompanies the Opera House edition of
Don Giovanni. It is very well executed. But as it is not in verse, it might have been
more literal, without being less elegant.
THE CONQUEST OF TARANTO
The Examiner.
He went through the different exploits of wickedness assigned him with evident
marks of reluctance and contrition; and it seemed the height of injustice that so
well meaning a young man, forced into acts of villainy against his will, should at
last be seized upon as their lawful prize by fiends come hot from hell with flaming
torches, and that he should sink into a lake of burning brimstone on a splendid car
brought to receive him by the devil, in the likeness of a great dragon, writhing
round and round upon a wheel of fire—an exquisite device of the Managers,
superadded to the original story, and in striking harmony with Mozart’s music! Mr.
Liston’s Leporello was not quite what we wished it. He played it in a mixed style
between a burlesque imitation of the Italian Opera, and his own inimitable
manner. We like him best when he is his own great original, and copies only
himself—
He did not sing the song of Madamira half so well, nor with half the impudence of
Naldi. Indeed, all the performers seemed, instead of going their lengths on the
occasion, to be upon their good behaviour, and instead of entering into their parts,
to be thinking of the comparison between themselves and the performers at the
Opera. We cannot say it was in their favour.