Preface

Since its introduction in 2005 the Arduino has become one of the most successful (some might argue the most successful) open source hardware projects in the world. Boards based on the open designs released by the Arduino team have been fabricated in countries around the world, including Italy, Brazil, China, the Netherlands, India, and the United States. One can purchase a fully functional Arduino-compatible board for around $15, and the Arduino development environment is readily available for download and is completely free. Originally based on the 8-bit AVR family of microcontrollers (the AVR is itself an interesting device with an equally interesting history), the Arduino has moved into the realm of 32-bit processing with the addition of the Due model with an ARM processor, the Yún with an on-board module running the OpenWrt version of Linux, and the upcoming Zero model. Arduinos have been used for everything from interactive art to robotics, and from environmental sensors to the smarts in small “CubeSat” satellites built by small teams and launched for a fraction of what a full-size satellite would cost.

I bought my first Arduino (a Duemilanove) many years ago, more out of curiosity than anything else. I had worked with microprocessor and microcontroller development systems since the early 1980s, starting with the 6502, 6800, and 8051, and then moving on to the 8086, the Z80, the 80186, and the 68000 family. Early on I usually programmed these devices in assembly language or PL/M, since these were really the only rational choices at the time for embedded systems. Later it became feasible to use C or Ada, as the capabilities of the microprocessors improved and the software tools matured. In each case, however, I came to expect having loads of reference material available: datasheets, hefty manuals, handbooks, and reference design documentation that would arrive along with the development circuit board and its accessories. It usually showed up in a large, heavy box.

When my new Arduino arrived and I opened the very small box I found that there was only a circuit board, a plug-in power pack, a few LEDs and resistors, some jumper wires, and a solderless breadboard block. No manuals, no handbooks, and no datasheets. Not even a CD with documents and software on it. Nothing more than a few sheets of paper with a manifest of what was in the box and a URL to a web page where I could read some “how to get started” material and find links to the software I needed. I was, to say the least, surprised.

I was also ignorant. When I bought the Arduino I didn’t know its full backstory, nor was I aware of its intended audience. It turns out that it was originally meant primarily for people with little or no hardcore technical background who just wanted to experiment (in a playful sense) with something cool and make things go. In other words, artists and tinkerers, not engineers who have a penchant for technical details and an addiction to project plans, specifications, and, of course, manuals.

Once I understood this, it all made a lot more sense. Reading Massimo Banzi’s book Getting Started with Arduino (O’Reilly) gave me a better understanding of the Arduino philosophy, and it was a good starting place in my quest for additional details. Also, unlike semiconductor manufacturers with their development kits, the folks on the Arduino team aren’t in the business of selling chips—they’re working to inspire creativity. The AVR microcontroller was chosen because it was inexpensive and it could be easily integrated into their vision for a device that could be readily applied to a creative endeavor. The AVR has enough computational “horsepower” and sufficient built-in memory to do complex and interesting things, unlike earlier generations of microcontrollers that usually needed expensive development tools and provided only scant amounts of internal on-chip memory.

Simplicity and low cost aside, the real secret to the success of the Arduino is the firmware bootloader on the AVR chip, coupled with a simple and easy-to-use integrated development environment (IDE) and the code libraries supplied with it—all provided free under open source and Creative Commons licensing. The Arduino philosophy is to make it as easy as possible to use an Arduino. By clearing away the bulk of the technical details and simplifying the development process, the Arduino invites the user to experiment, try new things, and yes, play. For the first time in a long time I found myself actually having a lot of fun just connecting things in different combinations to see what I could make it do. I wish that the Arduino had been around when I was teaching introductory embedded systems design—it would have helped reduce a lot of frustration for the people in the class trying to wade through assembly language listings, memory maps, and flowcharts.

Since my first Arduino arrived I’ve found many sources for useful and interesting add-on components for the Arduino family, some of them quite amazing in terms of both price and capabilities.1 In fact, I became something of an Arduino pack rat, buying inexpensive shields and modules and building up a sizable collection of various bits. But, sadly, I have to say that many times I’ve opened a package with a nifty new gizmo in it, only to discover that there is no documentation of any kind. Not even a simple wiring diagram.

As an engineer, it is particularly frustrating to me to purchase something interesting, only to have it show up with no documentation. I then have to embark on a quest to determine if any documentation actually does exist, and in a form that I can read (I can’t read Chinese). Sometimes that search is fruitless, and I’m forced to resort to component datasheets and reverse engineering the circuit board to figure out the wiring. Other times the information I’m seeking does exist, but it is scattered across multiple websites in various bits and pieces. This situation is slowly improving, but it can still be a real pain. After years of collecting stacks of notes, web page links, and datasheets, I finally decided to get organized and assemble it in one place.

So, what does this book have that can’t be found somewhere on the Internet? Not that much, to be perfectly honest, but hopefully what it will do is reduce a lot of the potential frustration and wasted time—and of course there are all the bits that I’ve discovered on my own. The official technical data comes from manufacturers such as Atmel, the Arduino team, and numerous other sources, both well known and obscure. Some overseas vendors do have at least rudimentary websites, whereas others have very nice web pages with links to other sites for the documentation. This book contains as much of the essential data as I could find or reverse engineer, all in one convenient place, with as many of the holes plugged as I could manage. I wanted to save others the frustration I’ve experienced trying to run down some mundane little technical detail about the USB interface, or figure out why a shield wasn’t working correctly, or why that really neat sensor I bought from someone through eBay didn’t seem to work at all.

The result of my frustrations is this book, the one I’ve been wanting for working with the Arduino boards and shields. I really wanted something physical that I could keep near at hand on my workbench. It isn’t always practical to have to refer to a web page to look something up, and to make things even more interesting, sometimes access to the Internet just isn’t available (like when you’re trying to debug a remote data logging device on a mountaintop with just a little netbook for company and no wireless service for 60 miles in any direction). I wanted something that I could use to quickly look up the answer to a question when working with the Arduino and associated add-on components, no matter where I was. As far as I know, such a thing didn’t exist until now. I hope you find it as useful as I have as I assembled my notes for this book.

Intended Audience

This book is intended for those people who need or want to know the technical details. Perhaps you’ve gone as far as you can with the introductory material and the “99 amazing projects” type of books, and you now need to know how to do something novel and unique. Or, you might be a working engineer or researcher who would like to incorporate an Arduino into your experimental setup in the lab. You might even be someone who wants to install an Arduino into an RC airplane, use one as part of a DIY weather station,2 or maybe do something even more ambitious (a CubeSat, perhaps?).

Ideally you should have a basic knowledge of C or C++, some idea of how electrons move around in a circuit, and some experience building electronic devices. If I may be so bold, I would suggest that you have a copy of my book Practical Electronics: Components and Techniques (also from O’Reilly) on hand, along with some reference texts on programming and electronics (see Appendix D for some suggestions).

What This Book Is

This book is a reference and a handbook. I have attempted to organize it such that you can quickly and easily find what you are looking for. I have included the sources of my information whenever possible, and those insights that are the result of my own investigations are noted as such.

What This Book Is Not

This book is not a tutorial. That is not its primary purpose. I don’t cover basic electronics, nor is there any discussion of the dialect of the C++ language that is used to create the so-called “sketches” for programming an Arduino. There are some excellent tutorial texts available that cover general electronics theory and programming, and I would refer the reader to those as a place to get started with those topics.

This book is also not an Arduino-sanctioned guide to the products produced by the Arduino team. It is based on information gleaned from various sources, some more obscure than others, along with my own notes and comments based on my experiences with the Arduino. As such, I am solely responsible for any errors or omissions.

About Terminology

The distinctions between processors, microprocessors, and microcontrollers arose sometime in the early 1980s as manufacturers were trying to distinguish their products based on size and amount of external circuitry required for the devices to do something useful. Full-size mainframe processors and the smaller microprocessors like those found in desktop PCs both typically require some external components (sometimes a lot of them) in order to be useful. A microcontroller, on the other hand, has everything it needs to do its job already built in. Also, a microprocessor will typically support external memory, whereas a microcontroller may have only limited support (if any at all) for adding additional memory to what is already on the chip itself.

Throughout this book I will use the terms “microcontroller” and “processor” interchangeably. Although “microcontroller” might be considered to be technically more correct, in my mind it is still a processor of data, just a smaller version of the huge machines I once worked with in the distant past. They all do essentially the same thing, just at different scales and processing speeds.

What’s in This Book

Chapter 1 presents a brief history of the Arduino in its various forms. It also introduces the AVR microcontrollers used in the Arduino boards, and discusses the differences between software-compatible and hardware-compatible products based on the Arduino.

The Atmel AVR microcontroller is the subject of Chapter 2. This is intended as an overview of what is actually a very sophisticated device, and so this chapter provides a quick tour of the highlights. This includes the timer logic, the analog comparator, the analog input, the SPI interface, and other primary subsystems on the chip.

Chapter 3 takes a closer look at the AVR microcontrollers used in Arduino boards, namely the ATmega168/328, the ATmega1280/2560, and the ATmega32U4 devices. It builds on the overview presented in Chapter 2, and provides additional low-level details such as internal architecture, electrical characteristics, and chip pinouts.

Chapter 4 covers the physical characteristics and interface functions of various Arduino boards. This includes the USB interface types, printed circuit board (PCB) dimensions, and board pinout diagrams.

What really makes the Arduino unique is its programming environment, and that is the subject of Chapter 5. This chapter defines the Arduino sketch concept and how it utilizes the C and C++ languages to create sketches. It introduces the Arduino bootloader and the Arduino main() function. This chapter also describes how you can download the Arduino source code and see for yourself what it looks like under the hood.

Chapter 6 describes the AVR-GCC toolchain and presents techniques for programming an Arduino board without using the Arduino IDE. It also covers makefiles and includes a brief overview of assembly language programming. The chapter wraps up with a look at the tools available to upload code into an AVR.

The focus of Chapter 7 is on the standard libraries supplied with the Arduino IDE. The Arduino IDE is supplied with numerous libraries, and more are being added all the time. If you want to know if a library module exists for a particular sensor or for a specific operation, then this is a good starting point.

Chapter 8 presents the various types of shields available for the Arduino. It covers many of the commonly available types, such as flash memory, prototyping, input/output, Ethernet, Bluetooth, ZigBee, servo control, stepper motor control, LED displays, and LCD displays. It also covers using multiple shields, and presents some hints and tips for getting the most from a shield.

Chapter 9 describes some of the various add-on components available that can be used with an Arduino. These include sensors, relay modules, keypads, and other items that aren’t specific to the Arduino, but work with it quite nicely. Electrical pinout information and schematics are provided for many of the items discussed.

Sometimes there just isn’t a readily available shield to do what needs to be done. Chapter 10 describes the steps involved in creating a custom shield. It also describes how to use an AVR microcontroller without an Arduino-type circuit board but still take advantage of the Arduino IDE.

Chapters 11, 12, and 13 cover some projects that illustrate the capabilities of the AVR microcontrollers and the Arduino boards. They are intended to demonstrate how an Arduino can be applied in various situations, not as how-to guides for building a board or device. You can, however, build any of the items described yourself, if you feel so inclined, and they might serve as jumping-off points for your own projects. Each example project description includes theory of operation, schematics, a detailed parts list, PCB layouts (if required), and an overview of the software necessary to make it go.

Because the main emphasis of this book is on the Arduino hardware and related modules, sensors, and components, the software shown is intended only to highlight key points; my aim was not to present complete, ready-to-run examples. The full software listings for the examples and projects can be found on GitHub.

Chapter 11 describes a basic programmable signal generator, a handy thing to have around when working with electronic circuits. With this project you can generate pulses at various duty cycles, output a sequence of pulses in response to a trigger input, generate sine waves, and also create programmable patterns of pulses.

Chapter 12 covers the design and construction of a “smart” thermostat suitable for use with a home HVAC (heating, ventilation, and air conditioning) system. Instead of paying for something that is already built and sealed into a plastic case, you can build it yourself and program it to behave exactly the way you want it to. I’ll show you how to incorporate more than just a temperature sensor: features include multiple temperature and humidity sensors, and the use of your HVAC system’s fan to provide a comfortable environment without the cost of running the compressor or lighting up the heater.

In Chapter 13 we will look at how to build an automatic model rocket launcher with a programmable sequencer and automatic system checks. Even if you don’t happen to have a model rocket handy, this project describes techniques that can be applied to many types of sequentially controlled processes, be it on a production line or a robotic material handling device in a laboratory.

Appendix A is an overview of the basic tools and accessories you may need if you want to go beyond prefabricated circuit boards and modules.

Appendix B is a compilation of the control registers for the ATmega168/328, the ATmega1280/2560, and the ATmega32U4 microcontrollers.

Appendix C is a summary listing of the Arduino and compatible products distributors and manufacturers listed in this book. It is by no means exhaustive, but hopefully it will be enough to get you started on your quest to find what you need.

Appendix D lists some recommended books that cover not just the Arduino, but also C and C++ programming and general electronics.

Finally, Appendix E is a summary of some of the readily available Arduino and AVR software development tools that are currently out there.

Endorsements

Other than references to the Arduino team and the folks at Arduino.cc, there aren’t any specific endorsements in this book—at least, not intentionally. I’ve made reference to many different component manufacturers, suppliers, and other authors, but I’ve tried to be evenhanded about it, and I don’t specifically prefer any one over another. My only criteria in selecting those I do mention are that I own one or more of their products and that I’ve successfully managed to use a shield, module, sensor, or Arduino main PCB (or clone PCB in some cases) from the supplier in something, even if just in a demonstration of some type. Any trademarks mentioned are the property of their respective owners; they appear here solely for reference. As for the photography, I tried to use my own components, tools, circuit boards, modules, and other items as much as possible, and although an image may show a particular brand or model, that doesn’t mean it’s the only type available—it likely just happens to be the one that I own and use in my own shop. In some cases I’ve used images with permission from the vendor or creator, works in the public domain, or images with a liberal Creative Commons (CC) license, and these are noted and credited as appropriate. I created all the diagrams, schematics, and other nonphotographic artwork, and I am solely responsible for any errors or omissions in these figures.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/arduino-a-technical-reference.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

This book would not have been possible without the enduring patience and support of my family. Writing appears to be habit-forming, but they’ve been very encouraging and supportive, and will even bring me things to eat and occasionally check to make sure I’m still alive and kicking in my office. It doesn’t get much better than that. I would particularly like to thank my daughter Seren for her photographic assistance and help in keeping my collection of various bits and pieces cataloged and organized.

I would also like to thank the editorial staff at O’Reilly for the opportunity to work with them once again. As always, they have been helpful, patient, and willing to put up with me. Special thanks goes to Brian Sawyer and Dawn Schanafelt for their excellent editorial support and guidance, and to Mike Westerfield for his insightful technical review of the material.

1 Entering “Arduino” into the search field on eBay will return a multitude of things like ultrasonic range sensors, temperature and humidity sensors, various Arduino clones, Bluetooth and ZigBee shields, and much more. But, unfortunately, some of the items come with little or no documentation, and even if there is some it may not be particularly up-to-date or accurate. That doesn’t mean you shouldn’t consider these sellers (the prices are usually excellent and the build quality is generally very good), but as always when buying things online, caveat emptor.

2 The books Environmental Monitoring with Arduino and Atmospheric Monitoring with Arduino (O’Reilly), both by Emily Gertz and Patrick Di Justo, offer some good ideas for doing just this with cheap and readily available sensors and an Arduino.

Get Arduino: A Technical Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.