Arduino is a microcontroller platform that has captured the imagination of electronics enthusiasts. Its ease of use and open source nature make it a great choice for anyone wanting to build electronic projects.
Ultimately, it allows you to connect electronics through its pins so that it can control things—for instance, turn lights or motors on and off or sense things such as light and temperature. This is why Arduino is sometimes given the description physical computing. Because Arduinos can be connected to your computer by a universal serial bus (USB) lead, this also means that you can use the Arduino as an interface board to control those same electronics from your computer.
This chapter is an introduction to the Arduino, including the history and background of the Arduino, as well as an overview of the hardware.
The heart of your Arduino is a microcontroller. Pretty much everything else on the board is concerned with providing the board with power and allowing it to communicate with your desktop computer.
A microcontroller really is a little computer on a chip. It has everything and more than the first home computers had. It has a processor, a kilobyte or two of random access memory (RAM) for holding data, a few kilobytes of erasable programmable read-only memory (EPROM) or flash memory for holding your programs and it has input and output pins. These input/output (I/O) pins link the microcontroller to the rest of your electronics.
Inputs can read both digital (is the switch on or off?) and analog (what is the voltage at a pin?). This opens up the opportunity of connecting many different types of sensor for light, temperature, sound, and more.
Outputs can also be analog or digital. So, you can set a pin to be on or off (0 volts or 5 volts) and this can turn light-emitting diodes (LEDs) on and off directly, or you can use the output to control higher power devices such as motors. They can also provide an analog output. That is, you can control the power output of a pin, allowing you to control the speed of a motor or the brightness of a light, rather than simply turning it on or off.
The microcontroller on an Arduino Uno board is the 28-pin chip fitted into a socket at the center of the board. This single chip contains the memory, processor, and all the electronics for the input/output pins. It is manufactured by the company Atmel, which is one of the major microcontroller manufacturers. Each of the microcontroller manufacturers actually produces dozens of different microcontrollers grouped into different families. The microcontrollers are not all created for the benefit of electronics hobbyists like us. We are a small part of this vast market. These devices are really intended for embedding into consumer products, including cars, washing machines, DVD players, children’s toys, and even air fresheners.
The great thing about the Arduino is that it reduces this bewildering array of choices by standardizing on one microcontroller and sticking with it. (Well, as we see later, this statement is not quite true, but it’s close enough.)
This means that when you are embarking on a new project, you do not first need to weigh all the pros and cons of the various flavors of microcontroller.
We have established that the microcontroller is really just a chip. A chip microcontroller will not just work on its own without some supporting electronics to provide it with a regulated and accurate supply of electricity (microcontrollers are fussy about this) as well as a means of communicating with the computer that is going to program the microcontroller.
This is where development boards come in. An Arduino Uno board is really a microcontroller development board that happens to be an independent open source hardware design. This means that the design files for the printed circuit board (PCB) and the schematic diagrams are all publicly available, and everyone is free to use the designs to make and sell his or her own Arduino boards.
All the microcontroller manufacturers—including Atmel, which makes the ATmega328 microcontroller used in an Arduino board—also provide their own development boards and programming software. Although they are usually fairly inexpensive, these tend to be aimed at professional electronics engineers rather than hobbyists. This means that such boards and software are arguably harder to use and require a greater learning investment before you can get anything useful out of them.
Figure 1-1 shows an Arduino Uno board. Let’s take a quick tour of the various components on the board.
Referring to Figure 1-1 , directly below the USB connector is the 5-volt (5V) voltage regulator. This regulates whatever voltage (between 7V and 12V) is supplied from the DC power socket into a constant 5V.
The 5V voltage regulator chip is actually quite big for a surface mount component. This is so that it can dissipate the heat required to regulate the voltage at a reasonably high current. This is useful when driving external electronics.
Although powering the Arduino through the DC power socket is useful when running the Arduino from batteries or a DC power jack, the Arduino Uno can also be powered through the USB port, which is also used to program the Arduino.
Next let us look at the connectors at the bottom of Figure 1-1 . You can read the connection names next to the connectors. The connector of interest is Reset. This does the same thing as the Reset button on the Arduino. Rather like rebooting a PC, using the Reset connector resets the microcontroller so that it begins its program from the start. To reset the microcontroller with the Reset connector, you momentarily set this pin low (connecting it to 0V).
The rest of the pins in this section just provide different voltages (3.3V, 5V, GND, and Vin), as they are labeled. GND, or ground, just means zero volts. It is the reference voltage to which all other voltages on the board are relative.
The six pins labeled as Analog In A0 to A5 can be used to measure the voltage connected to them so that the value can be used in a sketch (Arduino Program). Note that they measure a voltage and not a current. Only a tiny current will ever flow into them and down to ground because they have a very large internal resistance. That is, the pin having a large internal resistance only allows a tiny current to flow into the pin.
Although these inputs are labeled as analog, and are analog inputs by default, these connections can also be used as digital inputs or outputs.
We now switch to the top connector and start on the right-hand side in Figure 1-1 . Here we find pins labeled Digital 0 to 13. These can be used as either inputs or outputs. When used as outputs, they behave rather like the power supply voltages discussed earlier in this section, except that these are all 5V and can be turned on or off from your sketch. So, if you turn them on from your sketch they will be at 5V, and if you turn them off they will be at 0V. As with the power supply connectors, you must be careful not to exceed their maximum current capabilities. The first two of these connections (0 and 1) are also labeled RX and TX, for receive and transmit. These connections are reserved for use in communication and are indirectly the receive and transmit connections for your USB link to your computer.
These digital connections can supply 40 mA (milliamps) at 5V. That is more than enough to light a standard LED, but not enough to drive an electric motor directly.
Continuing our tour of the Arduino board, the microcontroller chip itself is the black rectangular device with 28 pins. This is fitted into a dual in-line (DIL) socket so that it can be easily replaced. The 28-pin microcontroller chip used on the Arduino Uno board is the ATmega328. Figure 1-2 presents a block diagram showing the main features of this device.
The heart—or, perhaps more appropriately, the brain—of the device is the central processing unit (CPU). It controls everything that goes on within the device. It fetches program instructions stored in the flash memory and executes them. This might involve fetching data from working memory (RAM), changing it, and then putting it back. Or, it may mean changing one of the digital outputs from 0V to 5V.
The EEPROM memory is a little like the flash memory in that it is non-volatile. That is, you can turn the device off and on and it will not have forgotten what is in the EEPROM. Whereas the flash memory is intended for storing program instructions (from sketches), the EEPROM is used to store data that you do not want to lose in the event of a reset or the power being turned off.
Above the microcontroller is a small, silver, rectangular component. This is a quartz crystal oscillator. It ticks 16 million times a second, and on each of those ticks, the microcontroller can perform one operation—addition, subtraction, or another mathematical operation.
In the top-left corner is the Reset switch. Clicking on this switch sends a logic pulse to the Reset pin of the microcontroller, causing the microcontroller to start its program afresh and clear its memory. Note that any program stored on the device will be retained, because this is kept in non-volatile flash memory—that is, memory that remembers even when the device is not powered.
On the right-hand edge of the board is the Serial Programming Connector. It offers another means of programming the Arduino without using the USB port. Because we do have a USB connection and software that makes it convenient to use, we will not avail ourselves of this feature.
In the top-left corner of the board next to the USB socket is the USB interface chip. This chip converts the signal levels used by the USB standard to levels that can be used directly by the Arduino board.
Arduino was originally developed as an aid for teaching students. It was subsequently (in 2005) developed commercially by Massimo Banzi and David Cuartielles. It has since gone on to become enormously successful with makers, students, and artists for its ease of use and durability.
Another key factor in its success is that all the designs for Arduino are freely available under a Creative Commons license. This has allowed many lower-cost alternatives to the boards to appear. Only the name Arduino is protected, so such clones often have “*duino” names, such as Boarduino, Seeeduino, and Freeduino. In 2014 there was a falling out between the original Arduino team and their principal manufacturer. One result of this split is that you will now find that outside of the USA, the Arduino Uno is known as the Genuino Uno. Many big retailers sell only the official boards, which are nicely packaged and of high quality.
Yet another reason for the success of Arduino is that it is not limited to microcontroller boards. There are a huge number of Arduino-compatible shield boards that plug directly into the top of an Arduino board. Because shields are available for almost every conceivable application, you often can avoid using a soldering iron and instead plug together shields that can be stacked one upon another. The following are just a few of the most popular shields:
♦ Ethernet, which gives an Arduino Web-serving capabilities
♦ Motor, which drives electric motors
♦ USB Host, which allows control of USB devices
♦ Relays, which switches relays from your Arduino
Figure 1-3 shows a motor shield (left) and relay shield (right).
It is useful to have a little background on the various Arduino boards. We will be using the Arduino Uno as our standard device—more specifically, the Arduino Uno R3 (Revision 3). Indeed, this is by far the most used of the Arduino boards, but the boards are all programmed using the same language and largely have the same connections to the outside world, so you can easily use a different board.
The Arduino Uno is just one incarnation of a long series of Arduino boards. The series includes the Diecimila (Italian for 10,000) and the Duemilanove (Italian for 2009). Figure 1-4 shows an Arduino Leonardo. By now you may have guessed that Arduino is an Italian invention.
The Arduino Leonardo (Figure 1-4 ) is another popular choice of Arduino board and can be used in place of the Arduino Uno in most situations. It is slightly cheaper than the Uno and has the same connections as the Uno. Its processor chip is soldered onto the board and so cannot be removed (as you can an Uno’s processor.) Its lower cost is in part due to the use of a processor that includes its own USB interface rather than having to use a separate chip like the Uno.
The Arduino Mega is the muscle car of Arduino boards. It boasts a huge collection of input/output ports, but cleverly adds these as extra connectors at one end of the board, allowing it to remain pin-compatible with the Arduino Uno and Leonardo and all the shields available for Arduino.
It uses a processor with more input/output pins, the ATmega1280, which is a surface mount chip that is fixed permanently to the board. So, unlike with the Uno and similar boards, you cannot replace the processor if you accidentally damage it.
The extra connectors are arranged at the end of the board. Extra features provided by the Mega include the following:
♦ 54 input/output pins
♦ 128KB of flash memory for storing sketches and fixed data (compared to the Uno’s 32KB)
♦ 8KB of RAM and 4KB of EEPROM
The Arduino Due (Figure 1-5 ) has the same board size and connections as the Mega, but uses a 32-bit ARM processor running at 84MHz. It also operates at 3.3V rather than the 5V of most Arduino boards so some Arduino shields will not operate correctly.
For those situations where an Uno is too big, there are a range of smaller Arduino and Arduino-compatible boards. Figure 1-6 shows a selection of these boards.
The Arduino Micro uses the same microcontroller as the Leonardo but is shrunk down to a more compact board. Third-party boards such as the LeoStick and Adafruit Trinket offer alternatives to the Micro.
The downside of small boards like the Micro is that because they are so much smaller than an Uno, they cannot accept Uno-sized shields.
The Arduino Yun (Figure 1-7 ) is essentially an Arduino Leonardo combined with a miniature WiFi module running Linux. The device is intended for Arduino applications that require a connection to the Internet. The Arduino and Linux halves of the Yun are linked using software called a bridge. The Yun is programmed using the Arduino IDE as normal, but can also be programmed wirelessly from the Arduino IDE once the Yun has been joined to your local network.
The Lilypad (Figure 1-8 ) is a tiny, thin Arduino board that can be stitched into clothing for applications that have become known as wearable computing.
The Lilypad does not have a USB connection, and you must use a separate adaptor to program it. It has an exceptionally beautiful design.
Adafruit also sell a board called the Flora, which is similar in concept to the Lilypad.
The previously described Arduino boards are the most useful and popular ones. However, the range of Arduino boards constantly changes, so for a complete and up-to-date picture of the Arduino family, see the official Arduino website list at www.arduino.cc/en/Main/Hardware .
Unofficial boards fall into two categories. Some just take the standard open source hardware designs of Arduino and build a cheaper one. Some names you can search for boards of this nature include the following:
♦ The Sparkfun RedBoard
♦ The Adafruit Metro
♦ The Olimexino
More interestingly, some Arduino-compatible designs are intended to extend or improve the Arduino in some way. New variants are appearing all the time, and far too many exist to mention them all. However, the following are some of the more interesting and popular variants:
♦ The Node MCU board, which is based around the ESP8266 WiFi System on a chip. This provides a very low-cost solution for projects where you need a WiFi connection for your Arduino. See Ch. 10 for more information.
♦ Adafruit Trinket, a very small Arduino.
♦ Freetronics EtherTen, an Arduino with built-in Ethernet.
♦ Particle Photon, a low-cost board with WiFi. It is programmed over the Internet using Arduino C but with a Web-based IDE rather than the Arduino IDE.
Now that you have explored the Arduino hardware a little, it’s time to set up your Arduino software.