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 powerPin 2: 5V powerPin 6: GroundPin 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
| Pin | Function | BCM GPIO Number | Notes |
|---|---|---|---|
| 1 | 3.3V Power | - | Power supply pin |
| 2 | 5V Power | - | Power supply pin |
| 6 | Ground | - | Common ground |
| 7 | GPIO 4 | 4 | General purpose I/O |
| 11 | GPIO 17 | 17 | General purpose I/O |
| 13 | GPIO 27 | 27 | General purpose I/O |
| 19 | GPIO 10 (MOSI) | 10 | SPI interface |
| 21 | GPIO 9 (MISO) | 9 | SPI interface |
| 23 | GPIO 11 (SCLK) | 11 | SPI interface |
| 29 | GPIO 5 | 5 | General purpose I/O |
| 31 | GPIO 6 | 6 | General purpose I/O |
| 33 | GPIO 13 | 13 | General purpose I/O |
| 35 | GPIO 19 | 19 | General purpose I/O |
| 37 | GPIO 26 | 26 | General purpose I/O |
| 40 | GPIO 21 (PCM_DOUT) | 21 | Audio 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.