Table Of ContentArduino
®
2nd Edition
by John Nussey
Arduino® For Dummies®, 2nd Edition
Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com
Copyright © 2018 by John Wiley & Sons, Inc., Hoboken, New Jersey
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to
the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River
Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written
permission. Arduino is a registered trademark of Arduino AG Corporation. All other trademarks are the property of
their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND
SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR
A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL
MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY
SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN
RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS
REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE
PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN
ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION
THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS
SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED
BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services, please contact our Customer Care Department within
the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit
https://hub.wiley.com/community/support/dummies.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to
media such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2018951004
ISBN 978-1-119-48954-2 (pbk); ISBN 978-1-119-48955-9 (ebk); ISBN 978-1-119-48957-3 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Part 1: Getting to Know Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
CHAPTER 1: Discovering Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
CHAPTER 2: Finding Your Board and Your Way Around It . . . . . . . . . . . . . . . . . . . . . . . 17
CHAPTER 3: Blinking an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Part 2: Getting Physical with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . 55
CHAPTER 4: Tools of the Trade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
CHAPTER 5: A Primer on Electricity and Circuitry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
CHAPTER 6: Basic Sketches: Inputs, Outputs, and Communication . . . . . . . . . . . . . . . 85
CHAPTER 7: More Basic Sketches: Motion and Sound . . . . . . . . . . . . . . . . . . . . . . . . . 119
Part 3: Building on the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
CHAPTER 8: Learning by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
CHAPTER 9: Soldering On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
CHAPTER 10: Getting Clever with Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
CHAPTER 11: Common Sense with Common Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CHAPTER 12: Becoming a Specialist with Shields and Libraries . . . . . . . . . . . . . . . . . . 285
Part 4: Sussing Out Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
CHAPTER 13: Getting to Know Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
CHAPTER 14: Processing the Physical World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Part 5: The Part of Tens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
CHAPTER 15: Ten Places to Learn More about Arduino . . . . . . . . . . . . . . . . . . . . . . . . . 359
CHAPTER 16: Ten Great Shops to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Table of Contents v
Table of Contents
FOREWORD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
PART 1: GETTING TO KNOW ARDUINO . . . . . . . . . . . . . . . . . . . . . . . 5
CHAPTER 1: Discovering Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Where Did Arduino Come From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Learning by Doing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Patching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Circuit bending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Open Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
CHAPTER 2: Finding Your Board and Your Way Around It . . . . . . . 17
Getting to Know the Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
The Brains: ATmega328P microcontroller chip . . . . . . . . . . . . . . . . .19
Header sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Digital pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Analog in pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
What about analog out? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Power pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
USB socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Power socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Reset button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
Discovering Other Arduino Boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Official Arduino boards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Contributed (approved) Arduinos . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Shopping for Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Arduino Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Adafruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
SparkFun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
vi
Arduino For Dummies
Seeed Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Watterott Electronic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Electronics distributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Amazon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
eBay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Kitted Out: Starting with a Beginner’s Kit . . . . . . . . . . . . . . . . . . . . . . . . .29
Preparing a Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
Installing Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Surveying the Arduino Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Using Arduino Web Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
CHAPTER 3: Blinking an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Working with Your First Arduino Sketch . . . . . . . . . . . . . . . . . . . . . . . . .37
Finding the Blink Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Identifying your board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Configuring the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Uploading the sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Congratulate yourself! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
What just happened? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Looking Closer at the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50
Blinking Brighter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Tweaking the Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
PART 2: GETTING PHYSICAL WITH ARDUINO . . . . . . . . . . . . . . . 55
CHAPTER 4: Tools of the Trade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Finding the Right Tools for the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Breadboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
Jump wires . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Needle-nose pliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Multimeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62
Using a Multimeter to Measure Voltage, Current, and Resistance . . . .63
Measuring voltage in a circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Measuring current in a circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
Measuring resistance of a resistor . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Measuring resistance of a variable resistor . . . . . . . . . . . . . . . . . . . .66
Checking the continuity (in bleeps) of your circuit . . . . . . . . . . . . . .67
Table of Contents vii
CHAPTER 5: A Primer on Electricity and Circuitry . . . . . . . . . . . . . . . . . 69
Understanding Electricity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Using Equations to Build Your Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Ohm’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Calculating power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Joule’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
Working with Circuit Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
A simple circuit diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Using a circuit diagram with an Arduino . . . . . . . . . . . . . . . . . . . . . .78
Color-Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Datasheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80
Resistor Color Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
CHAPTER 6: Basic Sketches: Inputs, Outputs, and
Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Uploading a Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Using Pulse-Width Modulation (PWM) . . . . . . . . . . . . . . . . . . . . . . . . . . .87
The LED Fade Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Understanding the Fade sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Tweaking the Fade sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94
The Button Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Understanding the Button sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .100
Tweaking the Button sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
The AnalogInput Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
Understanding the AnalogInput sketch . . . . . . . . . . . . . . . . . . . . . .106
Tweaking the AnalogInput sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .108
Talking Serial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
The DigitalReadSerial Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Understanding the DigitalReadSerial sketch . . . . . . . . . . . . . . . . . .112
The AnalogInOutSerial Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
Understanding the AnalogInOutSerial sketch . . . . . . . . . . . . . . . . .117
CHAPTER 7: More Basic Sketches: Motion and Sound . . . . . . . . . . . 119
Working with Electric Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119
Discovering Diodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
Spinning a DC Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
The Motor sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Understanding the Motor sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Changing the Speed of Your Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
The MotorSpeed sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
Understanding the MotorSpeed sketch . . . . . . . . . . . . . . . . . . . . . .127
viii
Arduino For Dummies
Controlling the Speed of Your Motor . . . . . . . . . . . . . . . . . . . . . . . . . . .128
The MotorControl sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Understanding the MotorControl Sketch . . . . . . . . . . . . . . . . . . . . .131
Tweaking the MotorControl sketch . . . . . . . . . . . . . . . . . . . . . . . . . .131
Getting to Know Servo motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Creating Sweeping Movements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
The Sweep sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Understanding the Sweep sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .136
Controlling Your Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
The Knob sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Understanding the Knob sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Making Noises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Piezo buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
The toneMelody sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143
Understanding the sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
Making an Instrument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
The PitchFollower sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153
Understanding the sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156
PART 3: BUILDING ON THE BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . 159
CHAPTER 8: Learning by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Skube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
Chorus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
Push Snowboarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
Baker Tweet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
The National Maritime Museum’s Compass Lounge
and Compass Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
The Good Night Lamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .171
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Little Printer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
Table of Contents ix
Flap to Freedom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
How it works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176
CHAPTER 9: Soldering On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Understanding Soldering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177
Gathering What You Need for Soldering . . . . . . . . . . . . . . . . . . . . . . . .178
Creating a workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
Choosing a soldering iron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
Solder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
Third hand (helping hand) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184
Adhesive putty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184
Wire cutters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Wire strippers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186
Needle-nosed pliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Multimeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Solder sucker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Solder wick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Equipment wire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Staying Safe while Soldering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Handling your soldering iron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
Keeping your eyes protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
Working in a ventilated environment . . . . . . . . . . . . . . . . . . . . . . . .190
Cleaning your iron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
Don’t eat the solder! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
Assembling a Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
Laying out all the pieces of the circuit . . . . . . . . . . . . . . . . . . . . . . .192
Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
Header pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194
Acquiring Your Soldering Technique . . . . . . . . . . . . . . . . . . . . . . . . . . .195
Building Your Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
Knowing your circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Laying out your circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Preparing your wire . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
Soldering your circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
Cleaning up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Testing your shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Packaging Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
Enclosures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
Wiring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
Securing the board and other elements . . . . . . . . . . . . . . . . . . . . .204
x
Arduino For Dummies
CHAPTER 10: Getting Clever with Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Blinking Better . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Setting up the BlinkWithoutDelay sketch . . . . . . . . . . . . . . . . . . . . .207
Understanding the BlinkWithoutDelay sketch . . . . . . . . . . . . . . . .210
Taking the Bounce Out of Your Button . . . . . . . . . . . . . . . . . . . . . . . . .212
Setting up the Debounce sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . .212
Understanding the Debounce sketch . . . . . . . . . . . . . . . . . . . . . . . .216
Making a Better Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218
Setting up the StateChangeDetection sketch . . . . . . . . . . . . . . . . .218
Understanding the StateChangeDetection sketch . . . . . . . . . . . . .222
Smoothing Your Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225
Setting up the Smoothing sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .225
Understanding the Smoothing sketch . . . . . . . . . . . . . . . . . . . . . . .228
Calibrating Your Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231
Setting up the Calibration sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .231
Understanding the Calibration sketch . . . . . . . . . . . . . . . . . . . . . . .234
CHAPTER 11: Common Sense with Common Sensors . . . . . . . . . . . . . 237
Making Buttons Easier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
Implementing the DigitalInputPullup sketch . . . . . . . . . . . . . . . . . .239
Understanding the DigitalInputPullup sketch . . . . . . . . . . . . . . . . .242
Exploring Piezo Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243
Implementing the Knock sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . .244
Understanding the Knock sketch . . . . . . . . . . . . . . . . . . . . . . . . . . .247
Utilizing Pressure, Force, and Load Sensors . . . . . . . . . . . . . . . . . . . . .249
Implementing the toneKeyboard sketch . . . . . . . . . . . . . . . . . . . . .251
Understanding the toneKeyboard sketch . . . . . . . . . . . . . . . . . . . .254
Sensing with Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
Implementing the CapPinSketch sketch . . . . . . . . . . . . . . . . . . . . . .258
Understanding the CapPinSketch sketch . . . . . . . . . . . . . . . . . . . . .261
Tripping Along with Lasers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
Implementing the AnalogInOutSerial sketch . . . . . . . . . . . . . . . . . .265
Understanding the AnalogInOutSerial sketch . . . . . . . . . . . . . . . . .268
Detecting Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
Implementing the DigitalReadSerial sketch . . . . . . . . . . . . . . . . . . .270
Understanding the DigitalReadSerial sketch . . . . . . . . . . . . . . . . . .273
Measuring Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274
Implementing the MaxSonar sketch . . . . . . . . . . . . . . . . . . . . . . . . .275
Understanding the MaxSonar sketch . . . . . . . . . . . . . . . . . . . . . . . .278
Testing, Testing . . . Can Anybody Hear This? . . . . . . . . . . . . . . . . . . . .280
Implementing the AnalogInOutSerial sketch . . . . . . . . . . . . . . . . . .281
Understanding the AnalogInOutSerial sketch . . . . . . . . . . . . . . . . .284
Table of Contents xi
CHAPTER 12: Becoming a Specialist with Shields
and Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Looking at Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286
Considering combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286
Reviewing the field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287
Staying current . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297
Browsing the Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298
Reviewing the standard libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
Installing additional libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300
Obtaining contributed libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302
PART 4: SUSSING OUT SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
CHAPTER 13: Getting to Know Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Looking Under the Hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306
Installing Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307
Taking a look at Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
Trying Your First Processing Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Drawing shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314
Changing color and opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317
Playing with interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
CHAPTER 14: Processing the Physical World . . . . . . . . . . . . . . . . . . . . . . . 323
Making a Virtual Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
Setting up the Arduino code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
Setting up the Processing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .326
Understanding the Processing PhysicalPixel sketch . . . . . . . . . . .329
Understanding the Arduino PhysicalPixel sketch . . . . . . . . . . . . . .332
Drawing a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .334
Setting up the Arduino code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
Setting up the Processing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Understanding the Arduino Graph sketch . . . . . . . . . . . . . . . . . . . .340
Understanding the Processing Graph sketch . . . . . . . . . . . . . . . . .340
Sending Multiple Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344
Setting up the Arduino code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
Setting up the Processing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .348
Understanding the Arduino SerialCallResponse sketch . . . . . . . .350
Understanding the Processing SerialCallResponse sketch . . . . . .352
xii
Arduino For Dummies
PART 5: THE PART OF TENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
CHAPTER 15: Ten Places to Learn More about Arduino . . . . . . . . . . 359
Arduino Blog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Hack a Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
SparkFun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
MAKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
Adafruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
YouTube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Hackerspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Friends, Colleagues, and Workshops . . . . . . . . . . . . . . . . . . . . . . . . . . .362
CHAPTER 16: Ten Great Shops to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Adafruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363
Arduino Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
Seeed Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
SparkFun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
Allied Electroncs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
Newark Electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
Mouser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
Digi-Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365
eBay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
Dumpster Diving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Foreword xiii
Foreword
T
he moment a For Dummies book comes out, it’s definitely a milestone in the
history of a product.
Programming embedded computers used to be a very difficult task, reserved only
to experienced engineers willing to master the obscure assembly language. In
recent years, however, many platforms have tried to make this task simpler and
more accessible to everyday people. Arduino is one of the latest attempts at mak-
ing technology less scary and more creative.
With John, this book’s author, we watched this creative tool being adopted by
designers and artists in London, making its way into many memorable projects.
Now Arduino has escaped the lab of Arts & Design and spread like a virus, becom-
ing the tool of choice for all kinds of people who have great ideas they want to
realize.
I’m really glad that John decided to write this book, because he’s an early user of
the Arduino platform from back in the days when it was still quite experimental.
Having taught Arduino classes for many years, he has the ability to introduce the
subject to all audiences.
Any newcomer to Arduino will, with the right tools and teaching — such as those
found in this book — show true genius in no time.
— Massimo Banzi