Introduction
The GPIO (General Purpose Input/Output) pins are the heart of what makes the Raspberry Pi so powerful as a maker platform. Unlike a regular computer, the Pi gives you direct, programmable access to hardware — sensors, motors, LEDs, displays, relays, and virtually any electronic component you can imagine.
Whether you're a beginner blinking your first LED or an advanced maker building a home automation system, this guide is your go-to reference for every single pin on the 40-pin GPIO header found on the Raspberry Pi 2, 3, 4, 5, and Zero series.
The 40-Pin Header at a Glance
All modern Raspberry Pi models share the same 40-pin GPIO header layout, making your projects portable across generations.

Pin Numbering Systems
This is one of the most common sources of confusion. There are two ways to refer to GPIO pins:
| System | Also Called | Example |
|---|---|---|
| BCM | GPIO numbering | GPIO17 = the 17th Broadcom GPIO |
| BOARD | Physical numbering | Pin 11 = the 11th physical pin on the header |
In Python with the RPi.GPIO library:
import RPi.GPIO as GPIO
# Use BCM numbering (recommended)
GPIO.setmode(GPIO.BCM)
# OR use physical board pin numbers
GPIO.setmode(GPIO.BOARD)
| Physical Pin | BCM GPIO | Name | Function |
|---|---|---|---|
| 1 | — | 3.3V Power | 3.3V supply (max 50mA total) |
| 2 | — | 5V Power | 5V supply direct from USB |
| 3 | GPIO2 | SDA1 | I²C Data |
| 4 | — | 5V Power | 5V supply |
| 5 | GPIO3 | SCL1 | I²C Clock |
| 6 | — | GND | Ground |
| 7 | GPIO4 | GPCLK0 | General Purpose Clock |
| 8 | GPIO14 | TXD0 | UART Transmit |
| 9 | — | GND | Ground |
| 10 | GPIO15 | RXD0 | UART Receive |
| 11 | GPIO17 | — | General Purpose |
| 12 | GPIO18 | PCM_CLK | PWM0 / PCM Clock |
| 13 | GPIO27 | — | General Purpose |
| 14 | — | GND | Ground |
| 15 | GPIO22 | — | General Purpose |
| 16 | GPIO23 | — | General Purpose |
| 17 | — | 3.3V Power | 3.3V supply |
| 18 | GPIO24 | — | General Purpose |
| 19 | GPIO10 | SPI0_MOSI | SPI Data (Master Out) |
| 20 | — | GND | Ground |
| 21 | GPIO9 | SPI0_MISO | SPI Data (Master In) |
| 22 | GPIO25 | — | General Purpose |
| 23 | GPIO11 | SPI0_SCLK | SPI Clock |
| 24 | GPIO8 | SPI0_CE0_N | SPI Chip Enable 0 |
| 25 | — | GND | Ground |
| 26 | GPIO7 | SPI0_CE1_N | SPI Chip Enable 1 |
| 27 | GPIO0 | ID_SD | EEPROM I²C Data (HAT ID) |
| 28 | GPIO1 | ID_SC | EEPROM I²C Clock (HAT ID) |
| 29 | GPIO5 | — | General Purpose |
| 30 | — | GND | Ground |
| 31 | GPIO6 | — | General Purpose |
| 32 | GPIO12 | PWM0 | PWM Channel 0 |
| 33 | GPIO13 | PWM1 | PWM Channel 1 |
| 34 | — | GND | Ground |
| 35 | GPIO19 | SPI1_MISO | SPI1 / PCM FS |
| 36 | GPIO16 | SPI1_CE0_N | SPI1 Chip Enable |
| 37 | GPIO26 | — | General Purpose |
| 38 | GPIO20 | SPI1_MOSI | SPI1 Data In |
| 39 | — | GND | Ground |
| 40 | GPIO21 | SPI1_SCLK | SPI1 Clock |
Pin Categories Explained
🔴 Power Pins
The Pi provides two voltage rails on the header:
- 3.3V (Pins 1, 17) — All GPIO pins operate at 3.3V logic. Total current limit is ~50mA across all 3.3V pins combined.
- 5V (Pins 2, 4) — Directly connected to your USB power supply. Useful for powering 5V peripherals, but bypasses the Pi's protection circuitry.
- GND (Pins 6, 9, 14, 20, 25, 30, 34, 39) — Multiple ground pins distributed across the header for convenience.
🚨 Danger: GPIO pins are 3.3V logic only. Applying 5V to a GPIO pin will damage your Pi permanently. Always use a logic level shifter when interfacing with 5V devices.
🟡 General Purpose Pins
Most GPIO pins can be configured as either input or output in software:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Configure GPIO17 as output (e.g. an LED)
GPIO.setup(17, GPIO.OUT)
# Configure GPIO27 as input with internal pull-up resistor
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Blink an LED
try:
while True:
GPIO.output(17, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(17, GPIO.LOW)
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
💡 Always call
GPIO.cleanup()at the end of your script to reset all pins to their default state. Skipping this can cause issues with your next script run.
🔵 I²C — Pins 3 (SDA) and 5 (SCL)
I²C (Inter-Integrated Circuit) is a two-wire serial protocol that lets you chain multiple devices on the same two wires using unique addresses.
Enable I²C on the Pi:
sudo raspi-config
# Navigate to: Interface Options → I2C → Enable
Scan for connected I²C devices:
sudo i2cdetect -y 1
Common I²C devices:
| Device | Default I²C Address | Use |
|---|---|---|
| MPU-6050 | 0x68 | Gyroscope + Accelerometer |
| BMP280 | 0x76 or 0x77 | Temperature + Pressure |
| SSD1306 OLED | 0x3C | 128×64 Display |
| PCF8574 | 0x20–0x27 | GPIO Expander |
| DS3231 RTC | 0x68 | Real-Time Clock |
🟢 SPI — Pins 19, 21, 23, 24, 26
SPI (Serial Peripheral Interface) is faster than I²C but uses more wires. It's ideal for high-speed peripherals like displays and ADCs.
| Pin | BCM | SPI Role |
|---|---|---|
| 19 | GPIO10 | MOSI — Master sends data |
| 21 | GPIO9 | MISO — Pi receives data |
| 23 | GPIO11 | SCLK — Clock signal |
| 24 | GPIO8 | CE0 — Chip select device 0 |
| 26 | GPIO7 | CE1 — Chip select device 1 |
Enable SPI:
sudo raspi-config
# Navigate to: Interface Options → SPI → Enable
🟠 UART — Pins 8 (TX) and 10 (RX)
UART is used for serial communication — connecting GPS modules, Bluetooth adapters, or another microcontroller like an Arduino.
⚠️ Warning: On Raspberry Pi 3 and 4, the primary UART is used by Bluetooth by default. You may need to disable Bluetooth or use the mini UART (
ttyS0) instead of the full UART (ttyAMA0).
Disable Bluetooth to free up the full UART (Pi 3/4 only):
# Add to /boot/config.txt
dtoverlay=disable-bt
sudo systemctl disable hciuart
sudo reboot
🟣 PWM — Pins 12 (GPIO18) and 33 (GPIO13)
PWM (Pulse Width Modulation) lets you simulate analog output — perfect for dimming LEDs, controlling servo motors, or generating audio tones.
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
# Create PWM instance: pin 18, frequency 1000Hz
pwm = GPIO.PWM(18, 1000)
pwm.start(0) # Start with 0% duty cycle
# Fade an LED from 0% to 100% brightness
for duty in range(0, 101, 5):
pwm.ChangeDutyCycle(duty)
time.sleep(0.05)
pwm.stop()
GPIO.cleanup()
Electrical Limits — Don't Fry Your Pi
These are hard limits. Exceeding them risks permanent damage:
| Spec | Limit |
|---|---|
| GPIO voltage (input/output) | 3.3V max |
| Max current per GPIO pin | 16mA |
| Max total GPIO current | 50mA |
| 5V pin current | Limited by your PSU |
| 3.3V rail total current | ~800mA (Pi 4) |
Always use a current-limiting resistor with LEDs:
R = (Vsupply - Vled) / Iled
R = (3.3V - 2.0V) / 0.01A = 130Ω → use 150Ω or 220Ω
Python Quick-Start Cheat Sheet
import RPi.GPIO as GPIO
import time
# ── Setup ──────────────────────────────────────────
GPIO.setmode(GPIO.BCM) # Use BCM pin numbering
GPIO.setwarnings(False)
# ── Output ─────────────────────────────────────────
GPIO.setup(17, GPIO.OUT) # Set GPIO17 as output
GPIO.output(17, GPIO.HIGH) # Turn on
GPIO.output(17, GPIO.LOW) # Turn off
# ── Input ──────────────────────────────────────────
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
state = GPIO.input(27) # Read: 1 = HIGH, 0 = LOW
# ── Interrupt / Event ──────────────────────────────
def button_pressed(channel):
print(f"Button on GPIO{channel} pressed!")
GPIO.add_event_detect(27, GPIO.FALLING,
callback=button_pressed,
bouncetime=200)
# ── PWM ────────────────────────────────────────────
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50) # 50Hz (good for servos)
pwm.start(7.5) # Neutral position
# ── Cleanup ────────────────────────────────────────
GPIO.cleanup()
Useful Tools & Commands
# Show GPIO status (requires raspi-gpio package)
raspi-gpio get
# Read a single pin's state
raspi-gpio get 17
# Set a pin high/low from the terminal
raspi-gpio set 17 op dh # output, drive high
raspi-gpio set 17 op dl # output, drive low
# Interactive pinout reference in the terminal
pinout
Recommended Libraries
| Library | Language | Install | Best For |
|---|---|---|---|
RPi.GPIO | Python | pip install RPi.GPIO | General GPIO, beginners |
gpiozero | Python | pip install gpiozero | High-level, readable API |
pigpio | Python/C | sudo apt install pigpio | Precise PWM, remote GPIO |
wiringPi | C/C++ | sudo apt install wiringpi | C projects |
lgpio | Python | pip install lgpio | Pi 5 recommended |
💡 Pi 5 users: The Pi 5 uses a new GPIO chip (
gpiochip4). Uselgpioorgpiozero—RPi.GPIOmay not be fully compatible yet.
Common Wiring Mistakes to Avoid
- Connecting 5V directly to a GPIO pin — always use a logic level converter
- Forgetting a resistor on an LED — will draw too much current and damage the pin
- Not calling
GPIO.cleanup()— leaves pins in undefined states - Confusing BCM and BOARD numbering — double-check before wiring
- Powering the Pi from the 5V GPIO pin without protection — bypasses the polyfuse




