Table Of ContentFor your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author ...................................................................................................... xv
About the Technical Reviewers ............................................................................... xvi
Acknowledgments .................................................................................................. xvii
Preface .................................................................................................................. xviii
■ Chapter 1: Hardware ............................................................................................... 1
■ Chapter 2: Software ............................................................................................... 25
■ Chapter 3: Atmel AVR ............................................................................................ 39
■ Chapter 4: Supporting Hardware ........................................................................... 71
■ Chapter 5: Arduino Software ................................................................................. 89
■ Chapter 6: Optimizations ....................................................................................... 99
■ Chapter 7: Hardware Plus Software .................................................................... 133
■ Chapter 8: Example Projects ............................................................................... 165
■ Chapter 9: Project Management .......................................................................... 213
■ Chapter 10: Hardware Design .............................................................................. 231
■ Chapter 11: Software Design ............................................................................... 255
■ Chapter 12: Networking ....................................................................................... 281
■ Chapter 13: More Example Projects .................................................................... 305
Index ....................................................................................................................... 359
iv
C H A P T E R 1
Hardware
The hardware of the Arduino has evolved slowly since its introduction in 2005. Because Arduino as a
concept is very much a combination of hardware and software, it’s important to have a good
understanding of what’s involved in both areas, as well as the areas where they overlap. Let’s undertake
a broad outline of the hardware part of the Arduino in this chapter, going into some detail in a few areas,
as well as its history and how you’ll play a part in its future.
What Is an Arduino?
Because of Arduino’s history and evolution, there are many variations on what can be called an Arduino.
The list grows longer every day. The official offering from the Arduino Team consists of the Arduino Uno
and the larger Arduino Mega 2560.
When most people think of Arduino, they imagine the small, rectangular (and probably blue)
printed circuit board (PCB). This is properly called the I/O Board. See Figure 1-1.
Figure 1-1. The Arduino I/O Board. This is what most people think of when you say “Arduino,” even
though it’s only one piece of a larger system.
1
CHAPTER 1 ■ HARDWARE
The I/O Board is the physically tangible part of the Arduino system. Technically speaking, the term
Arduino covers the hardware, software, development team, design philosophy, and esprit de corps of the
user community. Yet you’ll often hear people say things like, “Please hand me that Arduino,” or “Careful
with that Arduino, Eugene.”
Arduino was originally developed in Ivrea, Italy. Arduin of Ivrea was the king of Italy about a
thousand years ago and is celebrated in local history. The Piazza Gioberti hosts a pub named after this
famous king, which some say is only named after the road it’s on, the Via Arduino.
The name Arduino is a masculine Italian name meaning “strong friend.” Being a proper name,
Arduino is always capitalized. The model name Uno is stylized in all capitals only in the logo on the PCB.
For more on the history and heritage of Arduino, as well as mountains of other fascinating information,
please see the Arduino web site, http://arduino.cc.
The Arduino I/O Board has traditionally been based on the Atmel AVR ATmega8 and later
derivatives. The I/O Board also contains a serial port, power supply circuitry, expansion connectors, and
miscellaneous support components. The official Arduino FAQ states, “It’s just an AVR development
board” (www.arduino.cc/en/Main/FAQ). This assumes that you know what an AVR is. If you read Chapter
3, you will. (Hint: an AVR is a programmable microcontroller chip.) See the simplified block diagram in
Figure 1-2.
Figure 1-2. The Arduino I/O Board block diagram
The Arduino Uno
The Arduino Uno was announced on September 25, 2011 at the New York Maker Faire. The model name
Uno is Italian for the number one and is intended to correspond with the Uno Punto Zero, or 1.0 release
of the Arduino software. Previous releases, numbered 0001 through 0022 have been considered alpha, or
preliminary releases.
The Arduino Uno maintains a remarkable resemblance to its forebears. The physical form factor has
remained the same. Over the years, the processor has been upgraded twice from the original ATmega8
with 8KB bytes of program memory, first to the ATmega168 with 16KB of program memory and then to
the ATmega328 with 32KB bytes of program memory, while remaining pin compatible. The nine-pin RS-
232 serial connector and interface circuitry has been replaced with a virtual serial port using various USB
interface chips. The power-supply circuitry has seen some refinement with extra over-current protection
and intelligent power-source selection.
2
CHAPTER 1 ■ HARDWARE
Due to a temporary worldwide shortage of the beloved 28-pin dual inline package (DIP) version of
the ATmega328 processor (whose Atmel part number, to help differentiate it from the other packaging
options, is ATMEGA328P-PU, the first P being for picoPower technology and the second P meaning
plastic DIP), a surface-mount version of the Arduino Uno was released, dubbed the Arduino Uno SMD.
It’s functionally identical in operation to the Uno. The only drawback is that the surface-mount
processor can’t easily be removed from the PCB, as was the case with the socketed DIP versions. See
Figure 1-3.
Figure 1-3. The Arduino Uno (left) and the Arduino Uno SMD (right)
If you’re starting to get interested in the details of the Atmel AVR, you’re in luck! All will be revealed
in Chapter 3, including packaging options, how all the pins work and what they do, as well as a good
introduction to the inner workings of this very capable device. For now, let’s focus on how the Atmel
AVR fits into the big picture, Arduino-wise.
Processor
The main brain of the Arduino Uno is the Atmel AVR ATmega328, the black, rectangular plastic block
with two rows of pins protruding from its sides. On the SMD version, the processor is one of the two
miniscule black squares soldered directly to the PCB.
This device is essentially a computer on a chip, containing a central processing unit (CPU), memory
arrays, clocks, and peripherals in a single package. See Figure 1-4.
3
CHAPTER 1 ■ HARDWARE
Figure 1-4. Simplified block diagram of the ATmega328
The ATmega328 chip is derived from the original Arduino processor, the ATmega8. It contains more
memory and more peripheral capability than its predecessor while using less power. The ATmega328
processor can operate from a wide range of power-supply voltages, from 1.8V to 5.5V. This makes it well-
suited for battery-powered applications. At the lowest voltages, the processor has a maximum clock rate
of 4MHz (millions of cycles per second). Increase the supply voltage to at least 2.7V, and you can
increase the clock rate to 10MHz. To run at the rated maximum clock rate of 20MHz, the chip needs at
least 4.5V. The Arduino I/O Board provides 5.0V for the ATmega328 chip, so it can run at any speed, up
to the maximum of 20MHz.
The current crop of ATmega328 chips from Atmel feature the company’s picoPower technology,
which dramatically reduces power consumption in the device. These parts are designated with a P suffix:
for example, ATmega328P. The previous versions available were able to run either at lower voltages
(such as the ATmega328V) but not full speed, or at full speed but not at voltages below 2.7V. The
picoPower technology eliminates this limitation, allowing both full speed (at appropriate supply
voltages) and low power operation at reduced speeds. The picoPower parts don’t even have a speed
rating as a component of their part number, as the previous generation did (for example, ATmega328P-
PU vs. ATmega328-20PU).
■ Note Some specialized I/O Board models are designed to be run at 3.3V. This limits the maximum clock rate to
10MHz.
Although the new ATmega328 chip can run up to 20MHz, the original ATmega8 topped out at
16MHz. The 16MHz clock rate has been maintained in all subsequent Arduino models to preserve
compatibility.
See Chapter 3 for more detailed information about the Atmel AVR family of processors.
4
CHAPTER 1 ■ HARDWARE
Serial Port
The function of the serial port remains unchanged from the earliest days of Arduino. The connectors
have changed, but everyone pretends that everything is the same. From a functional perspective, this is
certainly true.
The serial port is used to communicate. In the development stage of your Arduino project, the
communication is between the Arduino and your PC, where you’re writing, compiling, and uploading
your sketch to the I/O Board. In the application (or deployment) phase of your project, when your
Arduino is performing its intended purpose, the serial port may continue to communicate with your PC,
if that is part of the plan, or it may communicate with another serial device. The use of the serial port is
optional at the application stage, so it may be communicating with nothing at all. If this is the case, the
receive (RX) and transmit (TX) pins can be used as general-purpose input/output (I/O) lines.
There are several types of serial communication protocols. The Arduino’s serial port (internally
referred to as the USART peripheral, or Universal Asynchronous/Synchronous Transmitter/Receiver) is
used in an asynchronous mode, meaning it doesn’t provide or require an independent clock signal. This
mode of operation is identical to the serial ports of most PCs, also known as RS-232 ports. The built-in
serial port hardware on the ATmega328 chip is capable of other modes of operation, including
synchronous mode, where a separate, dedicated signal carries the clock information. The asynchronous
method uses one signal to transmit data and another to receive data. Depending on your application
requirements, you may need to transmit, receive, do both, or do neither.
■ Caution Don’t connect RS-232 signals directly to your Arduino. The typically higher-voltage RS-232 signals
can damage the circuitry on the board, including the processor. Always use an RS-232–to-TTL adapter when
interfacing an Arduino to an RS-232 port.
Power Supply
The power supply circuit doesn’t actually supply any power to the Arduino. It only routes, regulates, and
filters power supplied from an external source. The present circuit has evolved over the years to make it
a convenient and almost foolproof process. The circuit selects the highest available voltage and uses that
source to supply the remainder of the circuit. There is even a resettable fuse installed on the board to
help prevent damage in the event of a short, thus lessening the likelihood of an unauthorized thermal
event. This is a great example of how the Arduino Team has listened to the user community and added
incremental improvements to the product over the years.
There are several ways to get power to your Arduino. The simplest, at least initially, is to use the
power supplied with the USB cable, which comes from your PC. The USB standard allows for the supply
of up to 100mA (milliamps, or 0.1 amps) of current at 5.0V for an unenumerated USB device (that is, a
device plugged into the USB bus but not properly identifying itself to the host, such as a USB power tap)
and as much as 500mA (0.5 amps) for a properly enumerated USB device. This is more than enough
electrical power to light up several LEDs and a few low-power sensors. It isn’t sufficient for larger
electrical loads, such as relays, heaters, fans, motors, or solenoids.
When the Arduino isn’t connected to a PC via the USB cable, regulated 5V power can be supplied to
it through the power expansion connector pins labeled 5V and GND.
5
CHAPTER 1 ■ HARDWARE
■ Caution A regulated 5V supply is required when supplying power via the 5V and GND pins. An unregulated
supply’s voltage fluctuates with line voltage and load, with the distinct possibility of exceeding the narrow voltage
range and very likely causing permanent damage to one or more components, including the processor. The
standard Arduino I/O Board provides a voltage regulator. Use it.
For unregulated supply voltage, a modular barrel connector is provided, with input voltages from 7V
to 12V. It’s directly connected to a 5V regulator circuit. In theory, the input voltage could be as high as
20V, but the likelihood of the voltage regulator chip overheating increases, which can permanently
damage the PCB.
The latest design revisions of the Arduino PCBs have greatly improved the ground planes where the
voltage regulators are mounted, increasing their ability to dissipate waste heat. However, even with this
improved cooling capability, a conservative estimate of the thermal resistance of the device is over
100°C/W, meaning that the temperature of the device will rise over 100°C from the ambient air
temperature if 1W is dissipated via the device. That’s hot! Don’t push it too far!
The barrel connector has a 2.1mm diameter pin. This center pin of the barrel connector is the
positive terminal. The outer sleeve connector is ground. The positive connection is also wired to the Vin
pin on the expansion connector. The Vin pin can be used to either supply power to the shield(s) or route
external power from shields back to the main I/O Board.
One very nice design feature of modern Arduino I/O Boards is the ability to have multiple, different
power supplies connected at once. The intelligent power-switching circuitry selects the highest available
voltage and routes that to the voltage regulator.
If you bypass this circuitry and provide regulated 5V power directly to your Arduino (which you
most certainly can do), be careful that it truly is regulated 5V that you’re pumping in. You just bypassed
all the safety devices that were put there for your protection. Again, if you know what you’re doing,
that’s fine.
The Arduino Uno also has a dedicated 3.3V regulator installed. Previous I/O Board designs relied on
the small 3.3V regulator built into the FTDI USB interface chip. This smaller regulator, although
effectively free of additional cost to the system, is only capable of supplying a maximum of 50mA (0.05
amps) of current at 3.3V to the system. The Arduino Uno sports its own 3.3V regulator (the LP2985 from
National Semiconductor) that can supply a maximum of 150mA (0.15 amps) of current, but the Arduino
web site still only admits to being able to supply 50mA.
Expansion Connectors
To make it easier to connect your Arduino to additional circuitry, four sets of expansion connectors are
provided. The two connectors across the top edge of the PCB contain the digital pins, along with the
analog reference input and an additional ground connection. The USART TX and RX pins are among
these pins, as well.
Along the bottom edge of the PCB are the power and analog connectors. The power connector
provides connections to the main supply voltages (Vin, 5V, 3V3, and ground) along with a connection to
the microcontroller’s -RESET pin. The analog connector brings out the six analog inputs, which can also
be used as digital I/O lines if need be.
A very handy feature of the Arduino PCB artwork is that every pin is clearly labeled. This
considerably reduces or eliminates tedious cross-referencing between data sheets and code listings. See
Figure 1-5.
6
CHAPTER 1 ■ HARDWARE
Figure 1-5. The I/O Board’s expansion connectors allow additional circuitry to be easily connected.
One of the buried technical details of the Arduino is the naming and grouping of the I/O pins within
the expansion connectors. In Arduino-speak, the pins are simply numbered: D0–D13 for the 14 digital
pins and A0–A5 for the 6 analog pins. The digital pins run along the top edge of the board, and the analog
pins are on the bottom edge.
This naming convention, although widely adopted and referenced extensively in the Arduino
documentation and software, is both misleading and inaccurate. Some of the digital pins provide the
analog outputs (see the analogWrite() function) but are in reality pulse-width-modulation (PWM) or
purely digital outputs. The analog inputs can just as easily be used in exactly the same manner as any of
the other digital pins, either as digital inputs or as digital outputs, but never as analog outputs.
■ Note You can use the analog pins A0–A5 just like any of the other digital pins by referring to them as D14–
D19. See Table 1-1.
In AVR-parlance, the ATmega8 family, of which the Arduino Uno’s ATmega328 is a derivative, has
three general-purpose I/O ports. On the ATmega8, these ports are named Port B, Port C, and Port D.
Each port can have a maximum of eight I/O pins associated with it. There’s much more information
about the details of the AVR I/O ports in Chapter 3.
7