Skip to content

Guide

Raspberry Pi 4 GPIO Pinout: All 40 Pins, Power and Interfaces

Raspberry Pi 4 40-pin GPIO header pinout with physical and BCM numbering, power and ground pins, I2C, SPI, UART, PWM, 3.3 V limits and safe wiring notes. Includes Pi 5 and Zero differences.

  • 7 min read
  • Updated November 7, 2026
Raspberry Pi 4 GPIO Pinout: All 40 Pins, Power and Interfaces

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.

Raspberry Pi 40-pin GPIO header pinout: physical pin numbers, BCM GPIO names, power and ground pins

Pin Numbering Systems

This is one of the most common sources of confusion. There are two ways to refer to GPIO pins:

SystemAlso CalledExample
BCMGPIO numberingGPIO17 = the 17th Broadcom GPIO
BOARDPhysical numberingPin 11 = the 11th physical pin on the header

In Python with the RPi.GPIO library:

main.py
import RPi.GPIO as GPIO

# Use BCM numbering (recommended)
GPIO.setmode(GPIO.BCM)

# OR use physical board pin numbers
GPIO.setmode(GPIO.BOARD)
Physical PinBCM GPIONameFunction
1—3.3V Power3.3V supply (keep external loads small)
2—5V Power5V supply direct from USB
3GPIO2SDA1I²C Data
4—5V Power5V supply
5GPIO3SCL1I²C Clock
6—GNDGround
7GPIO4GPCLK0General Purpose Clock
8GPIO14TXD0UART Transmit
9—GNDGround
10GPIO15RXD0UART Receive
11GPIO17—General Purpose
12GPIO18PCM_CLKPWM0 / PCM Clock
13GPIO27—General Purpose
14—GNDGround
15GPIO22—General Purpose
16GPIO23—General Purpose
17—3.3V Power3.3V supply
18GPIO24—General Purpose
19GPIO10SPI0_MOSISPI Data (Master Out)
20—GNDGround
21GPIO9SPI0_MISOSPI Data (Master In)
22GPIO25—General Purpose
23GPIO11SPI0_SCLKSPI Clock
24GPIO8SPI0_CE0_NSPI Chip Enable 0
25—GNDGround
26GPIO7SPI0_CE1_NSPI Chip Enable 1
27GPIO0ID_SDEEPROM I²C Data (HAT ID)
28GPIO1ID_SCEEPROM I²C Clock (HAT ID)
29GPIO5—General Purpose
30—GNDGround
31GPIO6—General Purpose
32GPIO12PWM0PWM Channel 0
33GPIO13PWM1PWM Channel 1
34—GNDGround
35GPIO19SPI1_MISOSPI1 Data In / PWM1 / PCM FS
36GPIO16SPI1_CE2_NSPI1 Chip Enable 2
37GPIO26—General Purpose
38GPIO20SPI1_MOSISPI1 Data Out
39—GNDGround
40GPIO21SPI1_SCLKSPI1 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:

main.py
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.

Diagram: BCM GPIO0 to GPIO27 with their physical pin numbers, coloured by default pull at power-up, fixed I²C pull-ups, HAT EEPROM and UART pins

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:

terminal
sudo raspi-config
# Navigate to: Interface Options → I2C → Enable

Scan for connected I²C devices:

terminal
sudo i2cdetect -y 1

Common I²C devices:

DeviceDefault I²C AddressUse
MPU-60500x68Gyroscope + Accelerometer
BMP2800x76 or 0x77Temperature + Pressure
SSD1306 OLED0x3C128×64 Display
PCF85740x20–0x27GPIO Expander
DS3231 RTC0x68Real-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.

PinBCMSPI Role
19GPIO10MOSI — Master sends data
21GPIO9MISO — Pi receives data
23GPIO11SCLK — Clock signal
24GPIO8CE0 — Chip select device 0
26GPIO7CE1 — Chip select device 1

Enable SPI:

terminal
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).

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.

main.py
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:

SpecLimit
GPIO voltage (input/output)3.3V max
Max current per GPIO pin16mA
Max total GPIO current~50mA (conservative design figure)
Default drive strength8mA (configurable 2–16mA)
5V pin currentLimited by your PSU
3.3V rail current budgetBoard-dependent; keep external loads to a few sensors

Always use a current-limiting resistor with LEDs:

Output
R = (Vsupply - Vled) / Iled
R = (3.3V - 2.0V) / 0.01A = 130Ω → use 150Ω or 220Ω

Python Quick-Start Cheat Sheet

main.py
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:

terminal
# 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)
pinout

On older Raspberry Pi OS releases the same commands work as raspi-gpio get and raspi-gpio set.


Recommended Libraries

LibraryLanguageInstall on Raspberry Pi OSBest forPi 5
gpiozeroPythonPreinstalled (python3-gpiozero)Readable, high-level code; the default choiceYes
RPi.GPIOPythonsudo apt install python3-rpi.gpioOlder tutorials and scriptsNo; rpi-lgpio is a drop-in replacement
lgpioPython/Csudo apt install python3-lgpioLow-level access on any modelYes
pigpioPython/Csudo apt install pigpioHardware-timed PWM and servos on Pi 3/4No
WiringPiC/C++Build from the community GitHub repo (no longer in apt)Legacy C projectsVersion 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 gpiozero with a supported pin factory or libgpiod-based tools. Do not hard-code a GPIO-chip index or assume a legacy RPi.GPIO script works unchanged.

Chart: gpiozero, RPi.GPIO, lgpio, pigpio, WiringPi, pinctrl and raspi-gpio support on Raspberry Pi 3, 4 and 5

Common Wiring Mistakes to Avoid

  1. Connecting 5V directly to a GPIO pin — always use a logic level converter
  2. Forgetting a resistor on an LED — will draw too much current and damage the pin
  3. Not calling GPIO.cleanup() — leaves pins in undefined states
  4. Confusing BCM and BOARD numbering — double-check before wiring
  5. Powering the Pi from the 5V GPIO pin without protection — bypasses the polyfuse
  6. Putting an active-LOW relay module on a pin that's pulled LOW at boot — the relay switches on before your script runs
  7. Expecting an RPi.GPIO script to run unchanged on a Pi 5 — use gpiozero, or install rpi-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

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.