Skip to content

How to Use Raspberry Pi GPIO Pins

DodaTech Updated 2026-06-24 4 min read

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 pinout before wiring.
  • Use current-limiting resistors on all outputs.
  • Use GPIO.cleanup() at the end of scripts.
  • Prefer gpiozero over RPi.GPIO for simpler code.
  • Never connect more than 3.3V to GPIO pins.

Common Mistakes with pi gpio

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### What is the difference between BOARD and BCM numbering?

BOARD numbering uses the physical pin numbers (1-40) on the GPIO header. BCM numbering uses the Broadcom chip's GPIO numbers. BOARD is easier for wiring, BCM is more portable across different Pi models. Choose one and stick with it.

Can I use 5V sensors with GPIO pins?

GPIO pins are 3.3V tolerant only. Connecting 5V to a GPIO pin will damage the Pi. Use a level shifter or voltage divider for 5V sensors. However, the 5V pin on the GPIO header can power 5V components safely.

How much current can a GPIO pin supply?

Each GPIO pin can safely supply up to 16 mA (recommended: 8 mA). The total draw across all pins should not exceed 50 mA. Use a transistor or relay for higher current loads like motors.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro