This Is Arduino - Programming Arduino Getting Started with Sketches (2012)

Programming Arduino Getting Started with Sketches (2012)

1
This Is Arduino

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.

Microcontrollers

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 voltage. That is, you can set the output of a pin to some particular voltage, 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 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.

Development Boards

We have established that the microcontroller is really just a chip. A chip 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 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.

A Tour of an Arduino Board

Figure 1-1 shows an Arduino board. Let’s take a quick tour of the various components on the board.

Image

Figure 1-1 An Arduino Uno board

Power Supply

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 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.

Power Connections

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 first 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.5V, 5V, GND, and 9V), 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.

Analog Inputs

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. 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.

Digital Connections

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.

Microcontroller

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 inline (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 is 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 nonvolatile. 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.

Image

Figure 1-2 ATmega328 block diagram

Other Components

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.

To the right of the crystal 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.

To the right of the Reset button 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.

The Origins of Arduino

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 “*dunino” names, such as Boarduino, Seeeduino, and Freeduino. However, the official boards manufactured in Italy still sell extremely well. 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 an Arduino Uno with an Ethernet shield attached.

Image

Figure 1-3 Arduino Uno with an Ethernet shield

The Arduino Family

It is useful to have a little background on the various Arduino boards. We will be using the Arduino Uno as our standard device. 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.

Uno, Duemilanove, and Diecimila

The Arduino Uno is the latest incarnation of the most popular series of Arduino boards. The series includes the Diecimila (Italian for 10,000) and the Duemilanove (Italian for 2011). Figure 1-4 shows an Arduino clone. By now you may have guessed that Arduino is an Italian invention.

These older boards look very similar to the Arduino Uno. They both have the same connectors and a USB socket and are generally compatible with each other.

The most significant difference between the Uno and the earlier boards is that the Uno uses a different USB chip. This does not affect how you use the board, but it does make installation of the Arduino software easier and allows higher speeds of communication with the computer.

The Uno can also supply more current on its 3.3V supply and always comes equipped with the ATmega328. The earlier boards will have either an ATmega328 or ATmega168. The ATmega328 has more memory, but unless you are creating a large sketch, this will make no difference.

Image

Figure 1-4 The Arduino Duemilanove

Mega

The Arduino Mega (Figure 1-5) 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 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

Nano

The Arduino Nano (Figure 1-6) is a very useful device for use with a solderless breadboard. If you fit pins to it, it can just plug into the breadboard as if it were a chip.

Image

Figure 1-5 An Arduino Mega board

Image

Figure 1-6 Arduino Nano

The downside of the Nano is that because it is so much smaller than an Uno, it cannot accept Uno-sized shields.

Bluetooth

The Arduino Bluetooth (Figure 1-7) is an interesting device as it includes Bluetooth hardware in place of the USB connector. This allows the device to even be programmed wirelessly.

The Arduino Bluetooth is not a cheap board, and it is often cheaper to attach a third-party Bluetooth module to a regular Arduino Uno.

Lilypad

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.

Image

Figure 1-7 Arduino Bluetooth

Image

Figure 1-8 Arduino Lilypad

Other “Official” Boards

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.

Arduino Clones and Variants

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:

• Roboduino

• Freeduino

• Seeeduino (yes, with three e’s)

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:

• Chipkit, a high-speed variant based on a PIC processor, but which is fairly compatible with Arduino

• Femtoduino, a very small Arduino

• Ruggeduino, with is an Arduino board with built-in I/O protection

• Teensy, a low-cost nano-type device

Conclusion

Now that you have explored the Arduino hardware a little, it’s time to set up your Arduino software.