How to Use Raspberry Pi GPIO Pins
In this tutorial, you'll learn about How to Use Raspberry Pi GPIO Pins. We cover key concepts, practical examples, and best practices.
The Problem
You connect an LED or sensor to the Raspberry Pi GPIO pins, but your Python script returns RuntimeError: No access to /dev/gpiomem, the pin does not change state, or you smell burning plastic. Incorrect GPIO usage can damage the Pi or connected components.
Quick Fix
Fix 1: Permission Denied for GPIO
WRONG — running the script as a regular user without proper permissions:
python3 blink.py
# Traceback (most recent call last):
# File "blink.py", line 5, in <module>
# GPIO.setmode(GPIO.BCM)
# RuntimeError: No access to /dev/mem. Try running as root!
RIGHT — add the user to the gpio group:
sudo usermod -a -G gpio,i2c,spi $USER
# Log out and log back in
python3 blink.py
# (LED blinks without errors)
Fix 2: Wrong Pin Numbering
WRONG — confusing physical pin number with GPIO number:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) # uses physical pin numbers
# Physical pin 11 = GPIO 17
# But code uses GPIO.setup(11, GPIO.OUT) — correct in BOARD mode
GPIO.setmode(GPIO.BCM) # uses GPIO numbers (Broadcom)
GPIO.setup(11, GPIO.OUT) # WRONG — GPIO 11 is physical pin 23
RIGHT — check the pinout diagram:
pinout
# (shows a visual map of all GPIO pins with numbers)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT) # GPIO 17 = physical pin 11
GPIO.output(17, GPIO.HIGH)
Fix 3: Using gpiozero (Easier API)
from gpiozero import LED
from signal import pause
led = LED(17) # BCM pin numbering
led.on()
pause()
Fix 4: Pin Already in Use
# RuntimeError: A different mode has already been set!
WRONG — calling setmode twice:
GPIO.setmode(GPIO.BCM)
GPIO.setmode(GPIO.BOARD) # RuntimeError
RIGHT — use setmode once, or clean up first:
GPIO.cleanup() # reset all pins
GPIO.setmode(GPIO.BCM)
Fix 5: Overcurrent Protection
WRONG — connecting an LED directly without a resistor:
# LED connected between GPIO 17 and GND (3.3V)
# (no resistor — the LED draws too much current and may damage the GPIO pin)
RIGHT — always use a current-limiting resistor:
from gpiozero import LED
led = LED(17) # external resistor required (typically 330Ω)
# Circuit: GPIO 17 → 330Ω resistor → LED (anode) → LED (cathode) → GND
Fix 6: PWM Not Working
WRONG — using GPIO.PWM without starting it:
p = GPIO.PWM(17, 1000) # 1 kHz
# (p.start is not called — no output)
RIGHT — start the PWM signal:
p = GPIO.PWM(17, 1000)
p.start(50) # 50% duty cycle
# (LED is at half brightness)
p.ChangeDutyCycle(25) # dimmer
Fix 7: Import Error (RPi.GPIO Not Installed)
pip install RPi.GPIO
# ModuleNotFoundError: No module named 'RPi'
RIGHT — install the system package:
sudo apt update && sudo apt install python3-rpi.gpio -y
Use DodaTech's GPIO Simulator to prototype circuits virtually before connecting components to your Pi, preventing damage from wiring mistakes.
Prevention
- Always check the pinout with
pinoutbefore wiring. - Use current-limiting resistors on all outputs.
- Use
GPIO.cleanup()at the end of scripts. - Prefer
gpiozerooverRPi.GPIOfor simpler code. - Never connect more than 3.3V to GPIO pins.
Common Mistakes with pi gpio
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world RASPBERRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro