Raspberry Pi - GPIO Connector
Raspberry Pi - GPIO Connector
Section titled “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.
The 40-Pin Header
Section titled “The 40-Pin Header”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.
Pin Numbering
Section titled “Pin Numbering”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
gpiozeroandRPi.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:
pinoutThis command shows both numbering schemes, current functions, and voltage levels.
GPIO Safety Rules - IMPORTANT!
Section titled “GPIO Safety Rules - IMPORTANT!”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).
Using GPIO with Python
Section titled “Using GPIO with Python”Python is the most common language for controlling GPIO pins on the Pi. Several libraries are available:
gpiozero (Recommended for Beginners)
Section titled “gpiozero (Recommended for Beginners)”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 updatesudo apt install python3-gpiozeroExample: Blink an LED connected (with a resistor) between GPIO17 and GND:
from gpiozero import LEDfrom 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 secondRPi.GPIO (Lower-Level Control)
Section titled “RPi.GPIO (Lower-Level Control)”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 updatesudo apt install python3-rpi.gpioExample: Blink an LED using RPi.GPIO (BCM numbering):
import RPi.GPIO as GPIOimport time
LED_PIN = 17
GPIO.setmode(GPIO.BCM) # Use BCM pin numberingGPIO.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 passfinally: GPIO.cleanup() # Reset GPIO pinsSpecial 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.
Enabling Interfaces
Section titled “Enabling Interfaces”The easiest way to enable these interfaces is using the Raspberry Pi Configuration tool:
- From the Desktop: Application Menu -> Preferences -> Raspberry Pi Configuration -> Interfaces Tab.
- From the Command Line: Run
sudo raspi-config, navigate to ‘Interface Options’, select the interface (I2C, SPI, Serial Port), and choose ‘Enable’. - Reboot your Pi after enabling interfaces for the changes to take full effect.
I2C (Inter-Integrated Circuit)
Section titled “I2C (Inter-Integrated Circuit)”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). Usesudo i2cdetect -y 1to scan the bus and find connected device addresses. - Python: Use the
smbus2library (sudo apt install python3-smbus).
SPI (Serial Peripheral Interface)
Section titled “SPI (Serial Peripheral Interface)”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
spidevlibrary (sudo apt install python3-spidevorpip install spidev).
UART (Serial Port)
Section titled “UART (Serial Port)”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/ttyS0or/dev/ttyAMA0depending on model and config). - Python: Use the
pyseriallibrary (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).