A Tour of the Raspberry Pi GPIO

01
of 09

An Introduction to the Raspberry Pi's Pins

Raspberry Pi GPIO
The Raspberry Pi GPIO. Richard Saville

The term 'GPIO' (General Purpose Input Output) isn't exclusive to the Raspberry Pi. Input and output pins can be found on most microcontrollers such as the Arduino, Beaglebone and more.

When we talk about GPIO with the Raspberry Pi, we're referring to the long block of pins at the top-left corner of the board. Older models had 26 pins, however most of us will be using a current model with 40.

You can connect components and other hardware devices to these pins, and use code to control what they do. It's an important part of the Raspberry Pi and an excellent way to learn about electronics.

After a few software projects, you'll likely find yourself experimenting with these pins, eager to mix your code with hardware to make things happen in 'real life'.

This process can be intimidating if you're new to the scene, and considering that one false move can damage your Raspberry Pi, it's understandable that it's a nervous area for beginners to explore.

This article will explain what each type of GPIO pin does and their limitations.

02
of 09

The GPIO

Raspberry Pi GPIO
The GPIO pins are numbered 1 to 40, and can be grouped under different functions. Richard Saville

First, let's take a look at the GPIO as a whole. The pins might look the same but they all have different functions. The image above shows these functions in different colors which we will explain in the following steps.

Each pin is numbered from 1 to 40 starting at the bottom left. These are the physical pin numbers, however, there are also numbering/​labelling conventions such as 'BCM' which are used when writing code.

03
of 09

Power & Ground

GPIO Power and Ground Pins
The Raspberry Pi offers multiple power and ground pins. Richard Saville

Highlighted red, are power pins labeled '3' or '5' for 3.3V or 5V.

These pins allow you to directly send power to a device without the need for any code. There's no way of turning these off either.

There are 2 power rails - 3.3 volts and 5 volts. According to this article, the 3.3V rail is limited to 50mA current draw, whereas the 5V rail can provide whatever current capacity is left over from your power supply after the Pi has taken what it needs.

Highlighted brown are the ground pins (GND). These pins are exactly what they say - ground pins - which are a vital part of any electronics project.

(5V GPIO pins are physical numbers 2 and 4. 3.3V GPIO pins are physical numbers 1 and 17. Ground GPIO pins are physical numbers 6, 9, 14, 20, 25, 30, 34 and 39)

04
of 09

Input/Output Pins

Raspberry Pi GPIO Input and Output pins
The Input and Output pins allow you to connect hardware such as sensors and switches. Richard Saville

The green pins are what I call 'generic' input/output pins. These can be easily used as inputs or outputs without any worries about clashing with other functions such as I2C, SPI or UART.

These are the pins that can send power to an LED, buzzer, or other components, or be used as an input to read sensors, switches or other input device.

The output power of these pins is 3.3V. Each pin shouldn't exceed 16mA of current, either sinking or sourcing, and the entire set of GPIO pins shouldn't exceed more than 50mA at any one time. This can be restrictive, so you may have to get creative in certain projects.

(Generic GPIO pins are physical numbers 7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 33, 35, 36, 37, 38 and 40)

05
of 09

I2C Pins

Raspberry Pi I2C GPIO Pins
I2C allows you to connect other devices to your Pi with just a couple of pins. Richard Saville

In yellow, we have the I2C pins. I2C is a communication protocol that in simple terms allows devices to communicate with the Raspberry Pi. These pins can also be used as 'generic' GPIO pins.

A good example of using I2C is the very popular MCP23017 port expander chip, that can give you more input/output pins through this I2C protocol.

(I2C GPIO pins are physical pin numbers 3 and 5)

06
of 09

UART (Serial) Pins

Raspberry Pi GPIO UART Pins
Connect to your Pi over a serial connection with the UART pins. Richard Saville

In grey,  are the UART pins. These pins are another communication protocol which offers serial connections, and can also be used as 'generic' GPIO inputs/outputs as well.

My favorite use for UART is to enable a serial connection from my Pi to my laptop over USB. This can be achieved using add-on boards or simple cables and removes the need for a screen or internet connection to access your Pi.

(UART GPIO pins are physical pin numbers 8 and 10)

07
of 09

SPI Pins

Raspberry Pi SPI GPIO Pins
The SPI Pins - another useful communication protocol. Richard Saville

In pink, we have the SPI pins. SPI is an interface bus that sends data between the Pi and other hardware/peripherals. It's commonly used for chaining of devices such as an LED matrix or display.

Like others, these pins can also be used as 'generic' GPIO inputs/outputs as well.

(SPI GPIO pins are physical pin numbers 19, 21, 23, 24 and 26)

08
of 09

DNC Pins

Raspberry Pi DNC GPIO Pins
Nothing to see here - the DNC pins serve no function. Richard Saville

Lastly are two pins in blue that, currently, are labeled as DNC which stands for 'Do Not Connect'. This may change in the future if the Raspberry Pi Foundation alter the boards/software.

(DNC GPIO pins are physical pin numbers 27 and 28)

09
of 09

GPIO Numbering Conventions

Raspberry Pi Portsplus
The Portsplus is a handy tool for checking GPIO pin numbers. Richard Saville

When coding with the GPIO, you have the choice to import the GPIO library in one of two ways - BCM or BOARD.

The option I prefer is GPIO BCM. This is the Broadcom numbering convention and I find that it's used more commonly across projects and hardware add-ons.

The second option is GPIO BOARD. This method uses the physical pin numbers instead, which is handy when counting pins, but you'll find it's used less in project examples.

The GPIO mode is set when importing the GPIO library:

To import as BCM:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

To ​import as BOARD:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

Both of these methods do exactly the same job, it's just a matter of numbering preference.

I regularly make use of handy GPIO label boards such as the RasPiO Portsplus (pictured) to check which pins I'm connecting wires too. One side shows the BCM numbering convention, the other shows BOARD - so you're covered for any project you find.