EXPLORING ARDUINO: TOOLS AND TECHNIQUES FOR ENGINEERING WIZARDRY
JOHN WILEY & SONS INC
JOHN WILEY & SONS INC
09 AUG 2013
Learn to easily build gadgets, gizmos, robots, and more using Arduino Written by Arduino expert Jeremy Blum, this unique book uses the popular Arduino microcontroller platform as an instrument to teach you about topics in electrical engineering, programming, and human-computer interaction. Whether you're a budding hobbyist or an engineer, you'll benefit from the perfectly paced lessons that walk you through useful, artistic, and educational exercises that gradually get more advanced. In addition to specific projects, the book shares best practices in programming and design that you can apply to your own projects. Code snippets and schematics will serve as a useful reference for future projects even after you've mastered all the topics in the book. * Includes a number of projects that utilize different capabilities of the Arduino, while interfacing with external hardware * Features chapters that build upon each other, tying in concepts from previous chapters to illustrate new ones * Includes aspects that are accompanied by video tutorials and other multimedia content * Covers electrical engineering and programming concepts, interfacing with the world through analog and digital sensors, communicating with a computer and other devices, and internet connectivity * Explains how to combine smaller topics into more complex projects * Shares downloadable materials and source code for everything covered in the book * Projects compatible with many official Arduino boards including Arduino Uno; Arduino Leonardo; Arduino Mega 2560; Arduino Due; Arduino Nano; Arduino Mega ADK; LilyPad Arduino and may work with Arduino-compatible boards such as Freeduino and new third party certified boards such as the Intel Galileo Exploring Arduino takes you on an adventure and provides you with exclusive access to materials not found anywhere else!
Introduction xix Part I Arduino Engineering Basics 1 Chapter 1 Getting Up and Blinking with the Arduino 3 Exploring the Arduino Ecosystem 4 Arduino Functionality 4 Atmel Microcontroller 6 Programming Interfaces 6 General I/O and ADCs 7 Power Supplies 7 Arduino Boards 8 Creating Your First Program 13 Downloading and Installing the Arduino IDE 13 Running the IDE and Connecting to the Arduino 14 Breaking Down Your First Program 16 Summary 18 Chapter 2 Digital Inputs, Outputs, and Pulse-Width Modulation 19 Digital Outputs 20 Wiring Up an LED and Using Breadboards 20 Working with Breadboards 21 Wiring LEDs 22 Programming Digital Outputs 24 Using For Loops 25 Pulse-Width Modulation with analogWrite() 27 Reading Digital Inputs 29 Reading Digital Inputs with Pulldown Resistors 29 Working with Bouncy Buttons 32 Building a Controllable RGB LED Nightlight 35 Summary 39 Chapter 3 Reading Analog Sensors 41 Understanding Analog and Digital Signals 42 Comparing Analog and Digital Signals 43 Converting an Analog Signal to a Digital One 44 Reading Analog Sensors with the Arduino: analogRead() 45 Reading a Potentiometer 45 Using Analog Sensors 50 Working with Analog Sensors to Sense Temperature 52 Using Variable Resistors to Make Your Own Analog Sensors 54 Using Resistive Voltage Dividers 55 Using Analog Inputs to Control Analog Outputs 56 Summary 59 Part II Controlling Your Environment 61 Chapter 4 Using Transistors and Driving Motors 63 Driving DC Motors 65 Handling High-Current Inductive Loads 65 Using Transistors as Switches 66 Using Protection Diodes 67 Using a Secondary Power Source 68 Wiring the Motor 68 Controlling Motor Speed with PWM 70 Using an H-Bridge to Control DC Motor Direction 72 Building an H-bridge Circuit 73 Operating an H-bridge Circuit 76 Driving Servo Motors 80 Understanding the Difference Between Continuous Rotation and Standard Servos 80 Understanding Servo Control 80 Controlling a Servo 85 Building a Sweeping Distance Sensor 86 Summary 90 Chapter 5 Making Sounds 91 Understanding How Speakers Work 92 The Properties of Sound 92 How a Speaker Produces Sound 94 Using tone() to Make Sounds 95 Including a Definition File 95 Wiring the Speaker 96 Making Sound Sequences 99 Using Arrays 99 Making Note and Duration Arrays 100 Completing the Program 101 Understanding the Limitations of the tone() Function 102 Building a Micro Piano 102 Summary 105 Chapter 6 USB and Serial Communication 107 Understanding the Arduino s Serial Communication Capabilities 108 Arduino Boards with an Internal or External FTDI USB-to-Serial Converter 110 Arduino Boards with a Secondary USB-Capable ATMega MCU Emulating a Serial Converter 112 Arduino Boards with a Single USB-Capable MCU 114 Arduino Boards with USB-Host Capabilities 114 Listening to the Arduino 115 Using print Statements 115 Using Special Characters 117 Changing Data Type Representations 119 Talking to the Arduino 119 Reading Information from a Computer or Other Serial Device 120 Telling the Arduino to Echo Incoming Data 120 Understanding the Differences Between Chars and Ints 121 Sending Single Characters to Control an LED 122 Sending Lists of Values to Control an RGB LED 125 Talking to a Desktop App 127 Talking to Processing 127 Installing Processing 128 Controlling a Processing Sketch from Your Arduino 129 Sending Data from Processing to Your Arduino 132 Learning Special Tricks with the Arduino Leonardo (and Other 32U4-Based Arduinos) 134 Emulating a Keyboard 135 Typing Data into the Computer 135 Commanding Your Computer to Do Your Bidding 139 Emulating a Mouse 140 Summary 144 Chapter 7 Shift Registers 145 Understanding Shift Registers 146 Sending Parallel and Serial Data 147 Working with the 74HC595 Shift Register 148 Understanding the Shift Register Pin Functions 148 Understanding How the Shift Register Works 149 Shifting Serial Data from the Arduino 151 Converting Between Binary and Decimal Formats 154 Controlling Light Animations with a Shift Register 154 Building a Light Rider 154 Responding to Inputs with an LED Bar Graph 157 Summary 160 Part III Communication Interfaces 161 Chapter 8 The I2C Bus 163 History of the I2C Bus 164 I2C Hardware Design 164 Communication Scheme and ID Numbers 165 Hardware Requirements and Pull-Up Resistors 167 Communicating with an I2C Temperature Probe 167 Setting Up the Hardware 168 Referencing the Datasheet 169 Writing the Software 171 Combining Shift Registers, Serial Communication, and I2C Communications 173 Building the Hardware for a Temperature Monitoring System 173 Modifying the Embedded Program 174 Writing the Processing Sketch 177 Summary 180 Chapter 9 The SPI Bus 181 Overview of the SPI Bus 182 SPI Hardware and Communication Design 183 Hardware Configuration 184 Communication Scheme 184 Comparing SPI to I2C 185 Communicating with an SPI Digital Potentiometer 185 Gathering Information from the Datasheet 186 Setting Up the Hardware 189 Writing the Software 190 Creating an Audiovisual Display Using SPI Digital Potentiometers 193 Setting Up the Hardware 194 Modifying the Software 195 Summary 197 Chapter 10 Interfacing with Liquid Crystal Displays 199 Setting Up the LCD 200 Using the LiquidCrystal Library to Write to the LCD 203 Adding Text to the Display 204 Creating Special Characters and Animations 206 Building a Personal Thermostat 209 Setting Up the Hardware 210 Displaying Data on the LCD 211 Adjusting the Set Point with a Button 213 Adding an Audible Warning and a Fan 214 Bringing It All Together: The Complete Program 215 Taking This Project to the Next Level 219 Summary 219 Chapter 11 Wireless Communication with XBee Radios 221 Understanding XBee Wireless Communication 222 XBee Radios 223 The XBee Radio Shield and Serial Connections 224 3.3V Regulator 226 Logic Level Shifting 226 Associate LED and RSSI LED 226 UART Selection Jumper or Switch 226 Hardware vs. Software Serial UART Connection Option 227 Configuring Your XBees 228 Configuring via a Shield or a USB Adapter 228 Programming Option 1: Using the Uno as a Programmer (Not Recommended) 229 Programming Option 2: Using the SparkFun USB Explorer (Recommended) 230 Choosing Your XBee Settings and Connecting Your XBee to Your Host Computer 230 Configuring Your XBee with X-CTU 231 Configuring Your XBee with a Serial Terminal 235 Talking with Your Computer Wirelessly 236 Powering Your Remote Arduino 236 USB with a Computer or a 5V Wall Adapter 237 Batteries 237 Wall Power Adapters 239 Revisiting the Serial Examples: Controlling Processing with a Potentiometer 239 Revisiting the Serial Examples: Controlling an RGB LED 243 Talking with Another Arduino: Building a Wireless Doorbell 246 System Design 246 Transmitter Hardware 247 Receiver Hardware 248 Transmitter Software 249 Receiver Software 250 Summary 252 Part IV Advanced Topics and Projects 255 Chapter 12 Hardware and Timer Interrupts 257 Using Hardware Interrupts 258 Knowing the Tradeoffs Between Polling and Interrupting 259 Ease of Implementation (Software) 260 Ease of Implementation (Hardware) 260 Multitasking 260 Acquisition Accuracy 261 Understanding the Arduino s Hardware Interrupt Capabilities 261 Building and Testing a Hardware-Debounced Button Interrupt Circuit 262 Creating a Hardware-Debouncing Circuit 262 Assembling the Complete Test Circuit 267 Writing the Software 267 Using Timer Interrupts 270 Understanding Timer Interrupts 270 Getting the Library 270 Executing Two Tasks Simultaneously(ish) 271 Building an Interrupt-Driven Sound Machine 272 Sound Machine Hardware 272 Sound Machine Software 273 Summary 275 Chapter 13 Data Logging with SD Cards 277 Getting Ready for Data Logging 278 Formatting Data with CSV Files 279 Preparing an SD Card for Data Logging 279 Interfacing the Arduino with an SD Card 284 SD Card Shields 284 SD Card SPI Interface 288 Writing to an SD Card 289 Reading from an SD Card 293 Using a Real-Time Clock 297 Understanding Real-Time Clocks 298 Using the DS1307 Real-Time Clock 298 Using the RTC Arduino Third-Party Library 299 Using the Real-Time Clock 300 Installing the RTC and SD Card Modules 300 Updating the Software 301 Building an Entrance Logger 305 Logger Hardware 306 Logger Software 307 Data Analysis 311 Summary 312 Chapter 14 Connecting Your Arduino to the Internet 313 The Web, the Arduino, and You 314 Networking Lingo 314 IP Address 314 Network Address Translation 315 MAC Address 316 HTML 316 HTTP 316 GET/POST 316 DHCP 316 DNS 317 Clients and Servers 317 Networking Your Arduino 317 Controlling Your Arduino from the Web 318 Setting Up the I/O Control Hardware 318 Designing a Simple Web Page 318 Writing an Arduino Server Sketch 320 Connecting to the Network and Retrieving an IP via DHCP 321 Replying to a Client Response 321 Putting It Together: Web Server Sketch 322 Controlling Your Arduino via the Network 326 Controlling Your Arduino over the Local Network 326 Using Port Forwarding to Control your Arduino from Anywhere 327 Sending Live Data to a Graphing Service 329 Building a Live Data Feed on Xively 330 Creating a Xively Account 330 Creating a Data Feed 330 Installing the Xively and HttpClient Libraries 331 Wiring Up Your Arduino 332 Configuring the Xively Sketch and Running the Code 332 Displaying Data on the Web 335 Adding Feed Components 336 Adding an Analog Temperature Sensor 336 Adding Additional Sensor Readings to the Datastream 336 Summary 339 Appendix Deciphering the ATMega Datasheet and Arduino Schematics 341 Reading Datasheets 341 Breaking Down a Datasheet 341 Understanding Component Pin-outs 344 Understanding the Arduino Schematic 345 Index 349