One of the key features of a Raspberry Pi computer that sets it apart from most other home/office computers is that it has the ability to directly interface with other hardware. The general-purpose input/output (GPIO) pins on the Raspberry Pi can control a wide range of low-level electronics, from light-emitting diodes (LEDs) to switches, sensors, motors, servos, and even extra displays.
This chapter will focus on connecting the Raspberry Pi with some simple circuits and getting to grips with using Python to control and respond to the connected components.
The Raspberry Pi hardware interface consists of 40 pins located along one side of the board.
The Raspberry Pi 3, Raspberry Pi 2, and Raspberry Pi B+ all have the same 40-pin layout.
The older Raspberry Pi 1 models (nonplus types) have a 26-pin header, which is the same as the 1-26 pins of the newer models.
The layout of the connector is shown in the preceding diagram; the pin numbers are shown as seen from pin 1 of the GPIO header.
Pin 1 is at the end that is nearest to the SD card, as shown in the following photo:
Care should be taken when using the GPIO header, since it also includes power pins (3V3 and 5 V), as well as ground (GND) pins. All of the GPIO pins can be used as standard GPIO, but several also have special functions; these are labeled and highlighted with different colors.
The TX and RX pins are used for serial communications, and with the aid of a voltage-level converter, information can be transferred via a serial cable to another computer or device.
We also have the SDA and SCL pins, which are able to support a two-wire bus communication protocol called I2C (there are two I2C channels on Raspberry Pi 3 and Model Plus boards: channel 1 ARM, which is for general use, and channel 0 VC, which is typically used for identifying hardware attached on top (HAT) modules). There are also the SPI MOSI, SPI MISO, SPI SCLK, SPI CE0, and SPI CE1 pins, which support another type of bus protocol called SPI for high-speed data. Finally, we have the PWM0/1 pin, which allows a pulse-width modulation signal to be generated, which is useful for servos and generating analog signals.
However, we will focus on using just the standard GPIO functions in this chapter. The GPIO pin layout is shown in the following diagram:
The Raspberry Pi Rev 2 (pre-July 2014) has the following differences compared to the Raspberry Pi 2 GPIO layout:
- 26-GPIO-pin header (matching the first 26 pins).
- An additional secondary set of eight holes (P5) located next to the pin header. The details are as follows:
- The original Raspberry Pi Rev 1 (pre-October 2012) has only 26 GPIO pins in total, (matching the first 26 pins of the current Raspberry Pi, except for the following details:
The RPi.GPIO library can reference the pins on the Raspberry Pi using one of two systems. The numbers shown in the center refer to the physical position of the pins, and are also the numbers referenced by the RPi.GPIO library when in GPIO.BOARD mode. The numbers on the outside (GPIO.BCM) are the actual reference numbers of the physical ports of the processor that indicate which of the pins are wired (which is why they are not in any specific order). They are used when the mode is set to GPIO.BCM, and they allow control of the GPIO header pins as well as any peripherals connected to other GPIO lines. This includes the LED on the add-on camera on BCM GPIO 4 and the status LED on the board. However, this can also include the GPIO lines used for reading/writing to the SD card, which would cause serious errors if interfered with.
If you use other programming languages to access the GPIO pins, the numbering scheme may be different, so it will be helpful if you are aware of the BCM GPIO references, which refer to the physical GPIO ports of the processor.