Introduction
The 40 GPIO (General Purpose Input/Output) pins are what turn a Raspberry Pi from a small computer into a controller for real hardware. 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.
This guide covers every pin on the 40-pin header shared by the Raspberry Pi 2, 3, 4, 5 and Zero boards, using the Pi 4 as the reference and calling out the Pi 5 differences where they matter.
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 (keep external loads small) |
| 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 Data In / PWM1 / PCM FS |
| 36 | GPIO16 | SPI1_CE2_N | SPI1 Chip Enable 2 |
| 37 | GPIO26 | — | General Purpose |
| 38 | GPIO20 | SPI1_MOSI | SPI1 Data Out |
| 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. The often-quoted 50mA limit for these pins dates from the first Raspberry Pi models. Newer boards supply more, but the same rail feeds the board's own I/O, so keep it to a few sensors and use a separate regulator for anything heavier.
- 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.
Pull resistors and pin states at power-up
Every GPIO has an internal pull-up and pull-down of roughly 50kΩ that you can switch on in software (pull_up_down=GPIO.PUD_UP above). Before your script runs, though, the chip applies its own defaults: GPIO0–8 start with the pull-up on, and GPIO9–27 start with the pull-down on.
That matters for relays and MOSFET drivers, which can switch on while the Pi boots, long before your code takes over. Match the load to the default: an active-HIGH relay or MOSFET belongs on GPIO9–27 (held LOW at boot), and an active-LOW relay module on GPIO4–8 (held HIGH). The internal pulls are weak, so add an external resistor if even a brief click at boot is a problem.
GPIO2 and GPIO3 (pins 3 and 5) are different again: they have fixed 1.8kΩ pull-up resistors on the board for I²C, so they always read HIGH when nothing drives them, and turning on a pull-down has no useful effect.
Each pin's drive strength defaults to 8mA and can be set anywhere from 2 to 16mA, which is where the 16mA-per-pin figure below comes from.
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 → EnableScan for connected I²C devices:
sudo i2cdetect -y 1Common 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 |
Two of those share an address: the MPU-6050 and the DS3231 both answer at 0x68. To use them together, tie the MPU-6050's AD0 pin HIGH to move it to 0x69.
GPIO3 (pin 5) has one more job: shorting it to ground briefly starts a Raspberry Pi that has been shut down, so a push button between pins 5 and 6 makes a simple power-on button.
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 → EnableUART — 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).
If you need the full UART on Pi 3/4: Bluetooth may share the primary UART on some configurations. Check the documentation for your model and Raspberry Pi OS release before changing boot overlays. On recent Raspberry Pi OS releases, the firmware configuration file is commonly /boot/firmware/config.txt. Disabling Bluetooth changes other functionality and is not necessary for every UART use case.
Enable the serial port: run sudo raspi-config, open Interface Options → Serial Port, answer No to a login shell over serial and Yes to the serial port hardware, then reboot. In your code, open /dev/serial0: it's an alias that always points at whichever UART is on pins 8 and 10, so you don't have to choose between ttyS0 and ttyAMA0 yourself.
PWM — Hardware on GPIO12, 13, 18 and 19
PWM (Pulse Width Modulation) lets you simulate an analog output for dimming LEDs, setting motor speed or driving servos. The Pi has two hardware PWM channels: PWM0 on GPIO12 (pin 32) or GPIO18 (pin 12), and PWM1 on GPIO13 (pin 33) or GPIO19 (pin 35). Two pins on the same channel output the same signal.
The RPi.GPIO example below is software PWM: it works on any GPIO, but Linux does the timing, so the signal jitters. That's fine for an LED; a servo will twitch. For servos, use hardware PWM through gpiozero with the pigpio pin factory (Pi 3/4), the pwm-2chan device-tree overlay, or a PCA9685 PWM board on I²C.
import RPi.GPIO as GPIO
import time
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 (conservative design figure) |
| Default drive strength | 8mA (configurable 2–16mA) |
| 5V pin current | Limited by your PSU |
| 3.3V rail current budget | Board-dependent; keep external loads to a few sensors |
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
On Raspberry Pi OS Bookworm, pinctrl replaces the older raspi-gpio tool and works on every model, including the Pi 5:
# Show every pin's mode and level
pinctrl get
# Read a single pin
pinctrl get 17
# Set a pin from the terminal
pinctrl set 17 op dh # output, drive high
pinctrl set 17 op dl # output, drive low
# Print a pinout diagram for the board you're on (from gpiozero)
pinoutOn older Raspberry Pi OS releases the same commands work as raspi-gpio get and raspi-gpio set.
Recommended Libraries
| Library | Language | Install on Raspberry Pi OS | Best for | Pi 5 |
|---|---|---|---|---|
gpiozero | Python | Preinstalled (python3-gpiozero) | Readable, high-level code; the default choice | Yes |
RPi.GPIO | Python | sudo apt install python3-rpi.gpio | Older tutorials and scripts | No; rpi-lgpio is a drop-in replacement |
lgpio | Python/C | sudo apt install python3-lgpio | Low-level access on any model | Yes |
pigpio | Python/C | sudo apt install pigpio | Hardware-timed PWM and servos on Pi 3/4 | No |
| WiringPi | C/C++ | Build from the community GitHub repo (no longer in apt) | Legacy C projects | Version 3 and later |
On Bookworm, pip install into the system Python is blocked by default. Use the apt packages above, or a virtual environment created with python3 -m venv --system-site-packages so it can still see them.
Pi 5 users: Pi 5 routes GPIO through the RP1 I/O controller, and low-level GPIO backends differ from older boards. Use GPIO tools and libraries supported by your current Raspberry Pi OS release, such as
gpiozerowith a supported pin factory or libgpiod-based tools. Do not hard-code a GPIO-chip index or assume a legacyRPi.GPIOscript works unchanged.
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
- Putting an active-LOW relay module on a pin that's pulled LOW at boot — the relay switches on before your script runs
- Expecting an
RPi.GPIOscript to run unchanged on a Pi 5 — usegpiozero, or installrpi-lgpio
Before Wiring a Sensor or Relay
Check three things before applying power: the sensor's output voltage, the GPIO voltage limit, and the current the load needs.
- Pushbuttons can go straight to a GPIO with the internal pull-up or pull-down enabled.
- Relay coils, motors and solenoids need a driver (a transistor, MOSFET or relay module) and their own power path. Never draw load current from a GPIO.
- 5V sensor outputs need a level shifter or a voltage divider. An HC-SR04 powered at 5V, for example, returns a 5V ECHO pulse; a 1kΩ/2kΩ divider brings it down to about 3.3V for the Pi.
For model-specific details, check the official Raspberry Pi GPIO documentation.
Related Guides
- Raspberry Pi 5 pinout guide: what changes with the RP1 I/O chip.
- Raspberry Pi Zero pinout guide: the same header on the smallest board.
- I2C vs SPI: which bus to pick for a new sensor.
- Raspberry Pi Flask server: control these pins from a web page.




