Chapter 4. Accessing the GPIO Pins

Now that you are familiar with the Arduino IDE and how to create, edit, and upload a program, this chapter will now turn your focus to the HW. You'll get a chance to learn how to connect to and access the capabilities of the general purpose input/output (GPIO) pins from the SW. In this chapter, I'll start by explaining the GPIO pins, what they can and can't do, and then show you how to make Arduino access the outside world with the help of some very basic circuits and very simple programming examples.

The GPIO capability of Arduino

Arduino was built to access the outside world. Much of that access is through the GPIO pins. Each Arduino board has a different set of GPIO pins, so in this section, I'll provide details on the GPIO pins available on the most common variant of Arduino: Arduino Uno. Then, I'll also document the additional capability of the Arduino Mega. Finally, I'll show the GPIO capability of a more limited Arduino: Arduino FLORA.

First, let's focus on the Arduino Uno. As described in Chapter 1, Powering on Arduino, the Arduino Uno comes with a set of 14 digital and six analog I/O pins, along with some additional pins to provide power and serial I/O.

Fortunately, the pins are actually well labeled on the board itself, as shown in the following image:

The GPIO capability of Arduino

The following table shows a list of pins that are available and a brief description of what each pin can do, starting at the upper-right side of the board and going clockwise. A more in-depth description of these pins will come later as you actually use them in some example projects:

Arduino Pin

Description

AREF

This pin provides a reference voltage for the analog inputs. The values on the analog pins will be reported in reference to this voltage. You'll also use this in some applications to provide a reference voltage for some sensing devices. You can also provide an external reference value to this pin, which means that the numerical values of the inputs will be scaled according to the value supplied on this pin.

GND

This pin provides a ground reference for the AREF pin.

Digital ((PWM~) 13/2

These 11 pins can be used to either read or write digital values. If defined as an input, the value will be read as either 0 or 1 based on the voltage level at the input. If defined as an output, the value will be set to either a 0 or 1 logic voltage level. (The actual voltage will depend on the voltage logic level of your Arduino. Some are 5 V logic level, while others are 3.3 V logic level.)

Digital TX->1

This pin and the RX pin next to it provide a serial interface that can be used to communicate with other devices.

Digital RX->0

This pin and the TX pin next to it provide a serial interface that can be used to communicate with other devices.

Analog IN A5/A0

These pins do double duty. Normally, they would be used as A/D inputs to Arduino to read continuous voltage values and turn them into integer values. However, they can also be used as digital I/O, very similar to the digital I/O pins.

Power Vin

You can power your Arduino from this pin. This can be especially useful after you have uploaded your program; you can then disconnect the USB port, and when you apply voltage to this pin, your Arduino will boot and run the uploaded program. You can use a voltage value from 7 to 12 volts, so a wide variety of DC power adapters or battery configurations can be used.

Power GND

This pin is the ground connection associated with the Power Vin connection.

Power GND

This is a ground connection normally associated with the Power 5 V and Power 3.3 V outputs.

Power 5 V

This is a voltage output set to 5 V.

Power 3.3 V

This is a voltage output set to 3.3 V.

RESET

This pin will reset the processor, which will cause the program to be run from the beginning.

IOREF

This provides either a 3.3 V or 5 V reference, indicating the logic level of the board.

The Mega provides a bit more from an I/O pin perspective. The following image shows the Arduino board:

The GPIO capability of Arduino

You'll first notice that the pins on the left side of the Arduino Mega are labeled the same as with the Arduino Uno. And they do have the same functionality. However, you'll also notice some additional pins on the right side of the board. The following table provides a brief explanation of the most useful pins, starting at the upper-right side of the board and going clockwise:

Arduino Mega pin

Description

Communication TX3/RX3/TX2/RX2/TX1/RX1

These pins add three more serial I/O ports. Mega supports four serial I/O ports.

SDA/SDL

These pins support the I2C I/O port. This is a special purpose communication that supports addressing, so you can talk to more than one device.

Digital I/O pins 44-46

Additional digital I/O pins that can be used to either read or write digital values. If input, the value will be read as either a 0 or 1 based on the voltage level at the input. If output, the value will be set to either a 0 or 1 logic voltage level. (The actual voltage will depend on the voltage logic level of your Arduino. Some are 5 V logic level, while others are 3.3 V logic level.)

Digital 50-53

These pins provide an SPI interface, particularly useful for video.

Analog In 6-15

These are additional analog DSP inputs that operate the same as the A0 to A6 pins.

The FLORA, being a much smaller package, provides a different set of pins. The following image shows the FLORA:

The GPIO capability of Arduino

The following table gives a description of the pins available, starting at the upper-left side of the board just above the USB connector:

Arduino FLORA pin

Description

3.3 V

This is a voltage output set to 3.3 volts. There are two of these pins available on the Arduino FLORA.

Digital I/O pins D10, D9, D6, D12

These pins can be used to either read or write digital values. If defined as an input, the value will be read as either a 0 or 1 based on the voltage level at the input. If defined as an output, the value will be set to either a 0 or 1 logic voltage level. The FLORA uses 3.3 V for 1 and 0 V for 0.

GND

There are three GND pins on the FLORA. The one closest to the white battery connector (the connector opposite the USB port) is normally used as the ground connection from the battery. The other two GND pins can be used for the digital I/O pins or the 3.3 V outputs.

VBATT

You can power the Arduino FLORA from this pin. This can be especially useful after you have uploaded your program; you can then disconnect the USB port, and when you apply voltage to this pin, your Arduino FLORA will boot and run the uploaded program. You can use a voltage value from 7 to 16 volts, so a wide variety of DC power adapters or battery configurations can be used.

Digital TX->1

This pin, and the RX pin next to it, provide a serial interface that can be used to communicate with other devices.

Digital RX->0

This pin, and the TX pin next to it, provide a serial interface that can be used to communicate with other devices.

SDA/SDL

These pins support the I2C I/O port. This is a special purpose communication that supports addressing, so you can talk to more than one device.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.218