Skip to content

Raspberry Pi - GPIO Connector

One of the defining features of the Raspberry Pi (excluding the Pico microcontroller initially) is the General Purpose Input/Output (GPIO) header. This row of pins allows the Pi to interact with the physical world by connecting to electronic components like LEDs, sensors, buttons, motors (via driver boards), and other integrated circuits.

All modern Raspberry Pi models (since the Pi 1 Model B+) feature a standard 40-pin header. Understanding the function of each pin is crucial for electronics projects.

Key Pin Types:

  • Power Pins: Provide power to external components. There are pins for +5V, +3.3V.
  • Ground Pins (GND): Provide the common 0V reference for circuits.
  • General Purpose I/O Pins (GPIO): These are the programmable pins. Each can be configured as either an input (to read signals from sensors/buttons) or an output (to send signals to control LEDs/motors). They operate at a 3.3V logic level.
  • Special Function Pins: Some GPIO pins have alternative functions for standard communication protocols like I2C, SPI, and UART.

There are two common ways to refer to the pins:

  • Board Pin Numbering: Counts the physical pins on the header from 1 to 40, starting top-left and moving down, then top-right and moving down (with USB/Ethernet ports facing you).
  • BCM (Broadcom) Pin Numbering: Refers to the GPIO channel number used by the Broadcom processor (e.g., GPIO17, GPIO22). This is the numbering scheme most commonly used in software libraries like gpiozero and RPi.GPIO.

It’s essential to know which numbering scheme your code or diagram is using. You can view an interactive pinout diagram directly on your Pi by opening a terminal and running the command:

pinout

This command shows both numbering schemes, current functions, and voltage levels.

Failing to observe these rules can permanently damage your Raspberry Pi:

  • Never connect more than 3.3V to any GPIO pin. Connecting 5V directly to a GPIO input pin will destroy it.
  • Do not draw excessive current from GPIO pins. Each pin configured as an output can safely supply only a small amount of current (typically up to 16mA). The total current drawn from all GPIO pins should not exceed around 50mA.
  • Use current-limiting resistors for LEDs. Connect LEDs in series with an appropriate resistor (e.g., 220-330 Ohms) to prevent them from drawing too much current from the GPIO pin.
  • Do not draw excessive current from the 3.3V power pins. The total current drawn from the 3.3V pins should generally be kept below 50-100mA (check specific model specs). Power-hungry components should use the 5V pins or an external power supply.
  • Do not short pins together, especially power and ground pins. Double-check your wiring before applying power.
  • Avoid connecting/disconnecting wires while the Pi is powered on (antistatic precautions are also wise).

Python is the most common language for controlling GPIO pins on the Pi. Several libraries are available:

gpiozero provides a simple, object-oriented interface for common components like LEDs, buttons, sensors, and motors. It handles much of the low-level setup automatically and uses BCM pin numbering by default. It’s usually pre-installed on Raspberry Pi OS Desktop.

Installation (if needed):

sudo apt update
sudo apt install python3-gpiozero

Example: Blink an LED connected (with a resistor) between GPIO17 and GND:

from gpiozero import LED
from time import sleep
led = LED(17) # Use BCM pin number
while True:
led.on() # Turn LED on
sleep(1) # Wait 1 second
led.off() # Turn LED off
sleep(1) # Wait 1 second

This library offers more direct control over GPIO functions (setting pin mode, pull-up/down resistors, detecting edges). It allows using either BCM or Board pin numbering. Requires more explicit setup and cleanup.

Installation (if needed):

sudo apt update
sudo apt install python3-rpi.gpio

Example: Blink an LED using RPi.GPIO (BCM numbering):

import RPi.GPIO as GPIO
import time
LED_PIN = 17
GPIO.setmode(GPIO.BCM) # Use BCM pin numbering
GPIO.setup(LED_PIN, GPIO.OUT) # Set pin as output
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off
time.sleep(1)
except KeyboardInterrupt: # Allows clean exit with Ctrl+C
pass
finally:
GPIO.cleanup() # Reset GPIO pins

Special Function Interfaces (I2C, SPI, UART)

Section titled “Special Function Interfaces (I2C, SPI, UART)”

Many sensors and peripheral devices use standard communication protocols like I2C, SPI, or UART. These interfaces need to be enabled before use.

The easiest way to enable these interfaces is using the Raspberry Pi Configuration tool:

  1. From the Desktop: Application Menu -> Preferences -> Raspberry Pi Configuration -> Interfaces Tab.
  2. From the Command Line: Run sudo raspi-config, navigate to ‘Interface Options’, select the interface (I2C, SPI, Serial Port), and choose ‘Enable’.
  3. Reboot your Pi after enabling interfaces for the changes to take full effect.

A two-wire protocol (SDA - Data, SCL - Clock) commonly used for sensors (temperature, pressure, accelerometers), RTC modules, and OLED displays. Multiple devices can share the same bus, each having a unique address.

  • Pins: Typically GPIO2 (SDA) and GPIO3 (SCL). Check pinout.
  • Tools: Install i2c-tools (sudo apt install i2c-tools). Use sudo i2cdetect -y 1 to scan the bus and find connected device addresses.
  • Python: Use the smbus2 library (sudo apt install python3-smbus).

A four-wire protocol (MOSI, MISO, SCLK, CE) often used for faster communication with devices like ADC converters, SD card readers, and some displays.

  • Pins: Typically GPIO10 (MOSI), GPIO9 (MISO), GPIO11 (SCLK), GPIO8 (CE0), GPIO7 (CE1). Check pinout.
  • Python: Use the spidev library (sudo apt install python3-spidev or pip install spidev).

A two-wire protocol (TX - Transmit, RX - Receive) for point-to-point serial communication, often used for connecting to GPS modules, other microcontrollers, or for accessing the Pi’s console.

  • Pins: Typically GPIO14 (TXD) and GPIO15 (RXD). Check pinout.
  • Configuration: When enabling via raspi-config, choose whether you want a login shell accessible over serial (disables it for general use) or just enable the hardware serial port for your own programs.
  • Device: The primary hardware UART usually appears as /dev/serial0 (which often links to /dev/ttyS0 or /dev/ttyAMA0 depending on model and config).
  • Python: Use the pyserial library (sudo apt install python3-serial).
  • Testing: Use minicom (sudo apt install minicom) to send/receive data, e.g., minicom -b 9600 -o -D /dev/serial0 (replace 9600 with the correct baud rate).