Raspberry Pi GPIO Programming

March 8, 2018

Interfacing to the Outside World

One of the strengths of the Raspberry Pi board is its GPIO (General Purpose Input/Output). This is a double row of pins that allow electrical connection and interface to devices external to the RPi. The original RPi had 26 pins. Later RPi have 40 pins. Some of these pins are used for power and ground, but the majority can be used for interface purposes.

The "general purpose" part of GPIO means that many of the pins can be programmed and used for different purposes. This really makes the interface more powerful and flexible. Configuring the pins is done through programming languages like Python or C (as well as other languages).

Examples of interfaces possible with the RPi:

  • Blinking LED
  • Proximity sensor
  • Motion sensor
  • Alarm system
  • Robotic control
  • Weather station
  • Touch sensor input
  • Game console devices
  • LCD display output
  • Relays (for power control)
  • . . . virtually unlimited other possibilities!

This article will demonstrate a couple of simple interface examples using Python as the programming language. First, let's understand what it means to be "general purpose" and how pins can change function.

Each pin has a "default" function. Many have one or more "alternate" functions that can be programmed for different purposes. GPIO pins can be programmed to accept "input" or to provide "output".

An example of an input might be a push button or switch. A Python program could monitor this switch connected to an RPi pin and react or perform some function when the switch changes state - on or off. This function might be to display status on the RPi monitor screen, or through another GPIO pin configured as output to sound a buzzer, turn on a light or some similar output function.

The Pi's pins are all digital - that means that at any point, their pin level is either on (high) or off (low). There is no analog capability to permit variable level input/output. However, there are ways to handle analog levels through methods called analog to digital conversion or vice versa.

Additionally, input / outputs can process complex signals such as sending data streams to control and display information on an LCD display, or to receive data streams from external communications devices.

GPIO Pinout

Raspberry Pi GPIO Pinout image

Raspberry Pi GPIO Pinout - RPi 2 and 3

Note that each of the 40 pins has a "physical" pin number: 1, 2, 3, 4 . . . as well as a "BCM" designator like: GPI02, GPI027, ID_SD . . . BCM refers to the Broadcom processor chip. As you will see, you can use the pins by their physical board number or the BCM number. There are also a few specific pins reserved exclusively for power: +5v, +3.3v and Ground (and not programmable).

Board vs BCM Numbering

The programmable pins may be referenced by either the physical pin number on the board or by the BCM number (or simply GPIO number) at the end of the designator name (i.e. GPIO27). This choice is made in the initial configuration during program setup.

Some examples of board vs BCM/GPIO numbering
Board Pin Number BCM/GPIO Name BCM Pin Number
3 GPIO2 2
5 GPIO3 3
7 GPIO4 4
8 GPIO14 14

It is important to understand that board numbering and BCM numbering use the same physical set of pins, but they are numbered differently. For example, board pin number 3 is the same physical pin as BCM pin number 2 (GPIO2) - see the GPIO Pinout image above. The only difference is in the setup and usage described below.

Setup and Configure GPIO

We must first "import" code to help support the interface. This is provided by the module "RPi.GPIO" and is done as follows:

import RPi.GPIO as GPIO

Then we decide if we wish to reference the pins by their physical board number or by their BCM number (GPIO number). Use the first line below to configure to use board numbering or the second line to use BCM numbering.

    - or -

Once the GPIO is configured for board numbering or BCM numbering, the next step is to select the pins and configure them for use as input or output. Note that the Python input and output statements are the same for both board numbering and BCM numbering. Refer to the GPIO.setmode to know which pin numbering mode is in use.


# Configured as input
GPIO.setup(3, GPIO.IN)

# Configured as output
GPIO.setup(3, GPIO.OUT)

Read a Pin as Input

A Python program can "read" a pin configured as input with the following statement:

# Assign state of pin 3 to variable "input_pin"
input_pin = GPIO.input(3)

Turn an Output Pin On or Off

GPIO outputs can be changed from on or off, also referred to as high or low. The voltage on the pin is either +3.3v for high (on) and 0v for low (off).

Let's say we have an LED connected to number 3. The values "True" and "False" are used to turn the LED on / off:

# Turn LED on
GPIO.output(3, True) 
# Turn LED off
GPIO.output(3, False)

Alternate Functions

Some of the GPIO's have alternate capabilities. Selected pins can be configured to support communications protocols, digital coding and interrupts.

Alternate RPi GPIO functions
Name Description Function
I2C Inter-Integrated Circuit Half duplex, serial data transmission used for short-distance between boards, modules and peripherals. Uses 2 pins.
SPI Serial Peripheral Interface bus Full-duplex, serial data transmission used for short-distance between devices. Uses 4 pins.
UART Universal Asynchronous Receiver-Transmitter Asynchronous, serial data transmission between devices. Uses 2 pins.
PWM Pulse Width Modulation A square-wave signal that is varied in 'Duty Cycle' used primarily to control electro-mechanical devices including servos and motors. Uses 1 pin plus a ground.
PCM Pulse Coded Modulation Audio in the form of a digital signal to simulate analog input/output. Uses up to 4 pins.
Interrupt Interrupt A pin programmed for use as an interrupt is used to monitor a signal from an external source that alerts the RPi of a "high priority" event that needs some attention within the RPi program. Most RPi GPIO pins can be configured as an interrupt.
Raspberry Pi 3 GPIO Pinout image

Raspberry Pi 3 GPIO Pinout

This is just the beginning. This article serves as background for the powerful GPIO capabilities of the Raspberry Pi. Actual interfaces with the outside world vary immensely and require further in-depth discussion in more detailed articles. Something that we, at MBTechWorks, will do regularly.

Article closing text image