0
0
Iot-protocolsHow-ToBeginner · 4 min read

Raspberry Pi GPIO Pinout Diagram Explained with Examples

The Raspberry Pi GPIO pinout diagram shows the layout of pins on the GPIO header, including power, ground, and data pins. It helps you connect sensors and devices correctly by identifying each pin's function and number.
📐

Syntax

The Raspberry Pi GPIO header has 40 pins arranged in two rows. Each pin has a specific function such as 3.3V power, 5V power, Ground, or GPIO (General Purpose Input/Output). Pins are numbered in two ways: physical pin number (1 to 40) and BCM GPIO number which is used in programming.

  • Pin 1: 3.3V power
  • Pin 2: 5V power
  • Pin 6: Ground
  • Pin 7: GPIO 4 (BCM numbering)

Use the physical pin number to connect hardware and BCM numbering in code.

plaintext
Physical Pin Numbering (1 to 40):
1  2
3  4
5  6
... up to 40 pins

BCM GPIO Numbering example:
Pin 7 = GPIO 4
Pin 11 = GPIO 17
Pin 13 = GPIO 27
💻

Example

This example shows how to blink an LED connected to GPIO 17 (physical pin 11) using Python and the RPi.GPIO library.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # Use BCM numbering
GPIO.setup(17, GPIO.OUT)  # Set GPIO 17 as output

try:
    while True:
        GPIO.output(17, GPIO.HIGH)  # LED on
        time.sleep(1)  # Wait 1 second
        GPIO.output(17, GPIO.LOW)   # LED off
        time.sleep(1)  # Wait 1 second
except KeyboardInterrupt:
    GPIO.cleanup()  # Clean up GPIO on CTRL+C exit
Output
The LED connected to GPIO 17 blinks on and off every second until stopped.
⚠️

Common Pitfalls

Common mistakes when using the Raspberry Pi GPIO pins include:

  • Confusing physical pin numbers with BCM GPIO numbers, leading to wrong connections.
  • Applying 5V signals to 3.3V GPIO pins, which can damage the Pi.
  • Not setting the GPIO mode (GPIO.setmode()) before using pins.
  • Forgetting to clean up GPIO settings after running code, causing warnings on next run.
python
Wrong way (using physical pin number without setting mode):
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.OUT)  # This uses BCM by default, but 11 is physical pin

Right way:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)  # Use physical pin numbering
GPIO.setup(11, GPIO.OUT)  # Now pin 11 is physical pin 11
📊

Quick Reference

PinFunctionBCM GPIO NumberNotes
13.3V Power-Power supply pin
25V Power-Power supply pin
6Ground-Common ground
7GPIO 44General purpose I/O
11GPIO 1717General purpose I/O
13GPIO 2727General purpose I/O
19GPIO 10 (MOSI)10SPI interface
21GPIO 9 (MISO)9SPI interface
23GPIO 11 (SCLK)11SPI interface
29GPIO 55General purpose I/O
31GPIO 66General purpose I/O
33GPIO 1313General purpose I/O
35GPIO 1919General purpose I/O
37GPIO 2626General purpose I/O
40GPIO 21 (PCM_DOUT)21Audio interface

Key Takeaways

The Raspberry Pi GPIO header has 40 pins with both power, ground, and GPIO functions.
Use physical pin numbers for hardware wiring and BCM numbers in your code.
Always set the GPIO mode before using pins to avoid confusion.
Never apply 5V signals to 3.3V GPIO pins to prevent damage.
Clean up GPIO settings in your code to avoid warnings and conflicts.