Table of Contents
Introduction xxv
 Part I Arduino Engineering Basics 1
 1 Getting Started and Understanding the Arduino Landscape 3
 Exploring the Arduino Ecosystem 4
 Arduino Functionality 5
 The Microcontroller 7
 Programming Interfaces 8
 Input/Output: GPIO, ADCs, and Communication Busses 9
 Power 9
 Arduino Boards 11
 Creating Your First Program 15
 Downloading and Installing the Arduino IDE 16
 Running the IDE and Connecting to the Arduino 17
 Breaking Down Your First Program 18
 Summary 21
 2 Digital Inputs, Outputs, and Pulse-Width Modulation 23
 Digital Outputs 24
 Wiring Up an LED and Using Breadboards 24
 Working with Breadboards 24
 Wiring LEDs 25
 Programming Digital Outputs 29
 Using For Loops 30
 Pulse-Width Modulation with analogWrite() 31
 Reading Digital Inputs 35
 Reading Digital Inputs with Pull-Down Resistors 35
 Working with “Bouncy” Buttons 38
 Building a Controllable RGB LED Nightlight 42
 Summary 46
 3 Interfacing with Analog Sensors 47
 Understanding Analog and Digital Signals 48
 Comparing Analog and Digital Signals 48
 Converting an Analog Signal to Digital 49
 Reading Analog Sensors with the Arduino: analogRead() 51
 Reading a Potentiometer 51
 Using Analog Sensors 56
 Using Variable Resistors to Make Your Own Analog Sensors 60
 Using Resistive Voltage Dividers 61
 Using Analog Inputs to Control Analog Outputs 64
 Summary 66
 Part II Interfacing with Your Environment 67
 4 Using Transistors and Driving DC Motors 69
 Driving DC Motors 70
 Handling High-Current Inductive Loads 71
 Using Transistors as Switches 72
 Using Protection Diodes73
 Using a Secondary Power Source 74
 Wiring the Motor 74
 Controlling Motor Speed with PWM 76
 Using an H-Bridge to Control DC Motor Direction 78
 Building an H-Bridge Circuit 80
 Operating an H-Bridge Circuit 82
 Building a Roving Robot 86
 Choosing the Robot Parts 87
 Selecting a Motor and Gearbox 87
 Powering Your Robot 87
 Constructing the Robot 89
 Writing the Robot Software 92
 Bringing It Together 96
 Summary 97
 5 Driving Stepper and Servo Motors 99
 Driving Servo Motors 100
 Understanding the Difference between Continuous Rotation and Standard Servos 100
 Understanding Servo Control 101
 Controlling a Servo 104
 Building a Sweeping Distance Sensor 105
 Understanding and Driving Stepper Motors 109
 How Bipolar Stepper Motors Work 111
 Making Your Stepper Move 113
 Building a “One-Minute Chronograph” 117
 Wiring and Building the Chronograph 117
 Programming the Chronograph 119
 Summary 124
 6 Making Sounds and Music 125
 Understanding How Speakers Work 126
 The Properties of Sound 126
 How a Speaker Produces Sound 128
 Using tone() to Make Sounds 129
 Including a Definition File 129
 Wiring the Speaker 130
 Making Sound Sequences 133
 Using Arrays 133
 Making Note and Duration Arrays 134
 Completing the Program 134
 Understanding the Limitations of the tone() Function 136
 Building a Micro Piano 136
 Summary 139
 7 USB Serial Communication 141
 Understanding the Arduino’s Serial Communication Capabilities 142
 Arduino Boards with an Internal or External FTDI or Silicon Labs USB-to-Serial Converter 143
 Arduino Boards with a Secondary USB-Capable ATmega MCU Emulating a Serial Converter 146
 Arduino Boards with a Single USB-Capable MCU 147
 Arduino Boards with USB-Host Capabilities 147
 Listening to the Arduino 148
 Using print Statements 148
 Using Special Characters 150
 Changing Data Type Representations 152
 Talking to the Arduino 152
 Configuring the Arduino IDE’s Serial Monitor to Send Command Strings 152
 Reading Incoming Data from a Computer or Other Serial Device 153
 Telling the Arduino to Echo Incoming Data 153
 Understanding the Differences between Chars and Ints 154
 Sending Single Characters to Control an LED 156
 Sending Lists of Values to Control an RGB LED 158
 Talking to a Desktop App 161
 Installing Processing 162
 Controlling a Processing Sketch from Your Arduino 163
 Sending Data from Processing to Your Arduino 166
 Summary 169
 8 Emulating USB Devices 171
 Emulating a Keyboard 173
 Typing Data into the Computer 173
 Commanding Your Computer to Do Your Bidding 177
 Emulating a Mouse 178
 Summary 182
 9 Shift Registers 183
 Understanding Shift Registers 184
 Sending Parallel and Serial Data 185
 Working with the 74HC595 Shift Register 186
 Understanding the Shift Register pin Functions 186
 Understanding How the Shift Register Works 187
 Shifting Serial Data from the Arduino 189
 Converting Between Binary and Decimal Formats 192
 Controlling Light Animations with a Shift Register 192
 Building a “Light Rider” 192
 Responding to Inputs with an LED Bar Graph 194
 Summary 197
 Part III Communication Interfaces 199
 10 The I2C Bus 201
 History of the I2C Bus 202
 I2C Hardware Design 203
 Communication Scheme and ID Numbers 203
 Hardware Requirements and Pull-Up Resistors 206
 Communicating with an I2C Temperature Probe 208
 Setting Up the Hardware208
 Referencing the Datasheet 210
 Writing the Software 212
 Combining Shift Registers, Serial Communication, and I2C Communications 214
 Building the Hardware for a Temperature Monitoring System 214
 Modifying the Embedded Program 215
 Writing the Processing Sketch 218
 Summary 221
 11 The SPI Bus and Third-Party Libraries 223
 Overview of the SPI Bus 224
 SPI Hardware and Communication Design 225
 Hardware Configuration 225
 Communication Scheme 227
 Comparing SPI to I2C and UART 227
 Communicating with an SPI Accelerometer 228
 What is an Accelerometer? 229
 Gathering Information from the Datasheet 231
 Setting Up the Hardware233
 Writing the Software 235
 Installing the Adafruit Sensor Libraries 236
 Leveraging the Library 237
 Creating an Audiovisual Instrument Using a 3-Axis Accelerometer 241
 Setting Up the Hardware242
 Modifying the Software 242
 Summary 246
 12 Interfacing with Liquid Crystal Displays 247
 Setting Up the LCD 248
 Using the LiquidCrystal Library to Write to the LCD 251
 Adding Text to the Display 252
 Creating Special Characters and Animations 254
 Building a Personal Thermostat 258
 Setting Up the Hardware 258
 Displaying Data on the LCD 261
 Adjusting the Set Point with a Button 264
 Adding an Audible Warning and a Fan 265
 Bringing It All Together: The Complete Program 266
 Taking This Project to the Next Level 270
 Summary 271
 Part IV Digging Deeper and Combining Functions 273
 13 Interrupts and Other Special Functions 275
 Using Hardware Interrupts 276
 Knowing the Tradeoffs Between Polling and Interrupting 277
 Ease of Implementation (Software) 277
 Ease of Implementation (Hardware) 277
 Multitasking 278
 Acquisition Accuracy 278
 Understanding the Arduino Hardware Interrupt Capabilities 278
 Building and Testing a Hardware-Debounced Button Interrupt Circuit 279
 Creating a Hardware-Debouncing Circuit 280
 Assembling the Complete Test Circuit 284
 Writing the Software 285
 Using Timer Interrupts 288
 Understanding Timer Interrupts 288
 Getting the Library 289
 Executing Two Tasks Simultaneously(ish) 289
 Building an Interrupt-Driven Sound Machine 290
 Sound Machine Hardware 291
 Sound Machine Software 291
 Summary 294
 14 Data Logging with SD Cards 295
 Getting Ready for Data Logging 296
 Formatting Data with CSV Files 297
 Preparing an SD Card for Data Logging 297
 Formatting Your SD Card Using a Windows PC 298
 Formatting Your SD Card Using Mac OS 300
 Formatting Your SD Card Using Linux 302
 Interfacing the Arduino with an SD Card 304
 SD Card Shields 304
 SD Card SPI Interface 307
 Writing to an SD Card 307
 Reading from an SD Card 312
 Real-Time Clocks 317
 Understanding Real-Time Clocks 317
 Communicating with a Real-Time Clock 317
 Using the RTC Arduino Third-Party Library 318
 Using a Real-Time Clock 319
 Installing the RTC and SD Card Modules 319
 Updating the Software 320
 Building an Entrance Logger 327
 Logger Hardware 328
 Logger Software 329
 Data Analysis 334
 Summary 335
 Part V Going Wireless 337
 15 Wireless RF Communications 339
 The Electromagnetic Spectrum 340
 The Spectrum 342
 How Your RF Link Will Send and Receive Data 343
 Receiving Key Presses with the RF Link 346
 Connecting Your Receiver 346
 Programming Your Receiver 347
 Making a Wireless Doorbell 351
 Wiring the Receiver 351
 Programming the Receiver 351
 The Start of Your Smart Home—Controlling a Lamp 354
 Your Home’s AC Power 356
 How a Relay Works 356
 Programming the Relay Control 358
 Hooking up Your Lamp and Relay to the Arduino 360
 Summary 361
 16 Bluetooth Connectivity 363
 Demystifying Bluetooth 364
 Bluetooth Standards and Versions 364
 Bluetooth Profiles and BTLE GATT Services 365
 Communication between Your Arduino and Your Phone 366
 Reading a Sensor over BTLE 366
 Adding Support for Third-Party Boards to the Arduino IDE 367
 Installing the BTLE Module Library 369
 Programming the Feather Board 369
 Connecting Your Smartphone to Your BTLE Transmitter 377
 Sending Commands from Your Phone over BTLE 379
 Parsing Command Strings 380
 Commanding Your BTLE Device with Natural Language 384
 Controlling an AC Lamp with Bluetooth 389
 How Your Phone “Pairs” to BTLE Devices 389
 Writing the Proximity Control Software 390
 Pairing Your Phone 394
 Pairing an Android Phone 394
 Pairing an iPhone 395
 Make Your Lamp React to Your Presence 396
 Summary 397
 17 Wi-Fi and the Cloud 399
 The Web, the Arduino, and You 400
 Networking Lingo 401
 The Internet vs. the World Wide Web vs. the Cloud 401
 IP Address 401
 Network Address Translation 402
 MAC Address 402
 HTML 402
 HTTP and HTTPS 402
 GET/POST 403
 DHCP 403
 DNS 403
 Clients and Servers 403
 Your Wi-Fi–Enabled Arduino 404
 Controlling Your Arduino from the Web 404
 Setting Up the I/O Control Hardware 404
 Preparing the Arduino IDE for Use with the Feather Board.406
 Ensuring the Wi-Fi Library is Matched to the Wi-Fi Module’s Firmware 407
 Checking the WINC1500’s Firmware Version 408
 Updating the WINC1500’s Firmware 408
 Writing an Arduino Server Sketch 408
 Connecting to the Network and Retrieving an IP Address via DHCP 409
 Writing the Code for a Bare-Minimum Web Server 412
 Controlling Your Arduino from Inside and Outside Your Local Network 423
 Controlling Your Arduino over the Local Network 423
 Using Port Forwarding to Control Your Arduino from Anywhere 425
 Interfacing with Web APIs 427
 Using a Weather API428
 Creating an Account with the API Service Provider 429
 Understanding How APIs are Structured 430
 JSON-Formatted Data and Your Arduino 430
 Fetching and Parsing Weather Data 431
 Getting the Local Temperature from the Web on Your Arduino 433
 Completing the Live Temperature Display 440
 Wiring up the LED Readout Display 440
 Driving the Display with Temperature Data 443
 Summary 449
 Appendix A: Deciphering Datasheets and Schematics 451
 Index 461