A Tour of the Raspberry Pi GPIO

What each type of GPIO pin does

The Raspberry Pi can be a lot of things. That's part of its appeal. You can use the Pi like a regular PC, connecting peripheral devices through the board's USB and HDMI ports. If you want to dive into the technical aspects of the Pi, turn your attention to the set of pins along the side of the circuit board. Those pins hold the key to using the Raspberry Pi in IoT, robotics, and other projects. The pins aren't as intimidating as you may think, once they're broken down and explained.

Raspberry Pi GPIO

An Introduction to the Raspberry Pi's Pins

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.

The GPIO with the Raspberry Pi is the long block of pins in the upper-left corner of the board. Older models had 26 pins, and current models have 40 pins.

You can connect components and other hardware devices to these pins and use code to control what the components 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 Pi. 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 explains what each type of GPIO pin does and its limitations.

The GPIO

First, let's take a look at the GPIO as a whole. The pins might look the same, but all have different functions. The image below shows these functions in different colors.

Raspberry Pi GPIO pin sequence diagram

Each pin is numbered from 1 to 40, starting from the lower-left corner. These are the physical pin numbers. However, there are also numbering and ​labeling conventions such as BCM, which are used when writing code.

Power and Ground

Highlighted in red are power pins labeled 3 or 5 for 3.3V or 5V. These pins allow you to send power to a device without the need for any code. There's no way of turning these off either.

The Raspberry Pi GPIO Power and Ground Pins

There are two power rails: 3.3 volts and 5 volts. The 3.3V rail is limited to 50mA current draw. In contrast, the 5V rail provides whatever current capacity is left over from the power supply after the Pi has taken what it needs.

Highlighted in brown are the ground pins (GND). These ground pins 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.

Input/Output Pins

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

These pins can send power to an LED, buzzer, or other components, or they can be used as an input to read sensors, switches, or other input devices.

Raspberry Pi GPIO Input and Output pins

The output power of these pins is 3.3V. Each pin shouldn't exceed 16mA of current, either sinking or sourcing. 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.

I2C Pins

The I2C pins are in yellow. I2C is a communication protocol that allows devices to communicate with the Raspberry Pi. These pins can also be used as generic GPIO pins.

Raspberry Pi I2C GPIO pins

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

I2C GPIO pins are physical pin numbers 3 and 5.

UART (Serial) Pins

The UART pins are in grey. These pins are another communication protocol that offers serial connections and can be used as generic GPIO inputs/outputs.

Raspberry Pi GPIO UART pins

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

UART GPIO pins are physical pin numbers 8 and 10.

SPI Pins

The SPI pins are in pink. SPI is an interface bus that sends data between the Pi and other hardware and peripherals. It's commonly used for chaining devices such as an LED matrix or display.

Raspberry Pi SPI GPIO Pins

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.

DNC Pins

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

Raspberry Pi DNC GPIO pins

DNC GPIO pins are physical pin numbers 27 and 28.

GPIO Numbering Conventions

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

The first option is GPIO BCM. This is the Broadcom numbering convention. It's commonly used 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. You'll find it's used less in project examples.

Raspberry Pi Portsplus

The GPIO mode is set when importing the GPIO library:

import RPi.GPIO as GPIO

To import as BCM:

GPIO.setmode(GPIO.BCM)

To ​import as BOARD:

GPIO.setmode(GPIO.BOARD)

Both of these methods do the same job. It's a matter of numbering preference.

You can use GPIO label boards such as the RasPiO Portsplus (pictured) to check which pins you're connecting wires to. One side shows the BCM numbering convention. The other shows BOARD. So, you're covered for any project you find.