CONTENTS

Preface

Acknowledgments

Introduction

1    Programming Arduino

What Is Arduino?

Installation and the IDE

Installing the IDE

Blink

A Tour of Arduino

Power Supply

Power Connections

Analog Inputs

Digital Connections

Arduino Boards

Uno and Similar

Big Arduino Boards

Small Arduino Boards

Unofficial Arduinos

Programming Language

Modifying the Blink Sketch

Variables

If

Loops

Functions

Digital Inputs

Digital Outputs

The Serial Monitor

Arrays and Strings

Analog Inputs

Analog Outputs

Using Libraries

Arduino Data Types

Arduino Commands

Summary

2    Under the Hood

A Brief History of Arduino

Anatomy of an Arduino

AVR Processors

ATmega328

ATmega32u4

ATmega2560

AT91SAM3X8E

Arduino and Wiring

From Sketch to Arduino

AVR Studio

Installing a Bootloader

Burning a Bootloader with AVR Studio and a Programmer

Burning a Bootloader with the Arduino IDE and a Second Arduino

Summary

3    When Is an Arduino Not an Arduino?

The Arduino IDE’s Extensible Architecture

Adafruit Circuit Playground Express

NodeMCU

ESP32

ATtiny Microcontrollers

ATtiny44

Using an Arduino as a Programmer

Installing ATtinyCore into the IDE

Clocks, Crystals, and Fuses

Minimal Arduino

Summary

4    Interrupts and Timers

Hardware Interrupts

Interrupt Pins

Interrupt Modes

Enabling Internal Pull-Up

Interrupt Service Routines

Volatile Variables

ISR Summary

Enabling and Disabling Interrupts

Timer Interrupts

Summary

5    Making Arduino Faster

How Fast Is an Arduino?

Comparing Arduino Boards

Speeding Up Arithmetic

Do You Really Need to Use a Float?

Lookup vs. Calculate

Fast I/O

Basic Code Optimization

Bytes and Bits

ATmega328 Ports

Very Fast Digital Output

Fast Digital Input

Speeding Up Analog Inputs

Summary

6    Low-Power Arduino

Power Consumption of Arduino Boards

Current and Batteries

Reducing the Clock Speed

Turning Things Off

Sleeping ATmega-Based Arduinos

Narcoleptic

Waking ATmega-Based Arduinos on External Interrupts

ESP8266 Sleeping

ESP32 Sleeping

Use Digital Outputs to Control Power

Summary

7    Memory

Arduino Memory

Minimizing RAM Usage

Use the Right Data Structures

Store String Constants in Flash Memory

Common Misconceptions

Measure Free Memory

Minimizing Flash Usage

Use Constants

Remove Unwanted Trace

Bypass the Bootloader

Static vs. Dynamic Memory Allocation

Strings

C char Arrays

The Arduino String Object Library

Using EEPROM

EEPROM Example

Using the avr/eeprom.h Library

EEPROM Limitations

Using Flash

Using SD Card Storage

Summary

8    Interfacing with Arduino

Binary

Arduino Types and Binary

Hexadecimal

Masking Bits

Shifting Bits

Serial Data

Summary

9    Using I2C

I2C Hardware

The I2C Protocol

The Wire Library

Initializing I2C

Master Sending Data

Master Receiving Data

I2C Examples

TEA5767 FM Radio

Arduino-to-Arduino Communication

LED Backpack Boards

DS1307 Real-Time Clock

Summary

10    Interfacing with 1-Wire Devices

1-Wire Hardware

The 1-Wire Protocol

The OneWire Library

Initializing 1-Wire

Scanning the Bus

Using the DS18B20

Summary

11    Interfacing with SPI Devices

Bit Manipulation

SPI Hardware

The SPI Protocol

The SPI Library

SPI Example

Summary

12    Serial UART Programming

Serial Hardware

Serial Protocol

The Serial Commands

The SoftwareSerial Library

Serial Examples

Computer to Arduino over USB

Arduino to Arduino

GPS Module

Summary

13    USB Programming

Keyboard and Mouse Emulation

Keyboard Emulation

Keyboard Emulation Example

Mouse Emulation

Mouse Emulation Example

USB Host on the Arduino Due

Summary

14    Network and Internet of Things Programming

Networking Hardware

Ethernet Shield

Arduino Ethernet/EtherTen

The Ethernet Library

Making a Connection

Setting Up a Web Server

Making Requests

Wired Ethernet Examples

Physical Web Server

Using a JSON Web Service

The Official Arduino WiFi Library

Making a Connection

WiFi-Specific Functions

Arduino WiFi Example

ESP8266/ESP32 WiFi Example

Internet of Things

dweet.io

Programming the NodeMCU or Wemos D1 Mini

Attaching the TMP36

A Web Page to Display the Temperature

Summary

15    Digital Signal Processing

Introducing Digital Signal Processing

Averaging Readings

An Introduction to Filtering

Creating a Simple Low-Pass Filter

Arduino Uno DSP

Arduino Due DSP

Filter Code Generation

The Fourier Transform

Spectrum Analyzer Example

Frequency Measurement Example

Summary

16    Managing with One Process

Making the Transition from Big Programming

Why You Don’t Need Threads

Setup and Loop

Sense Then Act

Pause Without Blocking

The Timer Library

State Diagrams

State Machines in Arduino

Summary

17    Writing Libraries

When to Make a Library

Using Classes and Methods

Library Example (TEA5767 Radio)

Define the Library’s Interface

Write the Header File

Write the Implementation File

Write the Keywords File

Make the Examples Folder

Testing the Library

Releasing the Library

Publishing Your Library on GitHub

GitHub

Creating a Repository

Summary

A    Parts

Arduino Boards

Components and Modules

Suppliers

Arduino Starter Kits

Index