RPi.GPIO

raw JSON →
0.7.1 verified Fri May 01 auth: no python maintenance

A module to control Raspberry Pi GPIO channels. Provides a simple interface for digital input/output, PWM, and interrupt handling on the Raspberry Pi. Latest version 0.7.1; no longer actively maintained since the BCM2835 GPIO driver update. Works on all Raspberry Pi models but requires root. Uses BCM (Broadcom) pin numbering by default.

error RuntimeError: No access to /dev/mem. Try running as root!
cause Script executed without root privileges.
fix
Run the script with sudo: sudo python your_script.py
error RuntimeError: This module can only be run on a Raspberry Pi!
cause Attempt to import RPi.GPIO on a non-Raspberry Pi system.
fix
Check platform before import: if platform.machine() not in ('armv7l', 'aarch64'): print('Not a Pi')
error ImportError: No module named RPi.GPIO
cause RPi.GPIO not installed or not found (e.g., virtual environment without --system-site-packages).
fix
Install with: sudo pip install RPi.GPIO
error RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
cause Called GPIO.setup or GPIO.output before calling GPIO.setmode.
fix
Add GPIO.setmode(GPIO.BCM) (or GPIO.BOARD) before any other GPIO calls.
gotcha Must run with root (sudo) or as root user. Otherwise RuntimeError: No access to /dev/mem.
fix Run script with `sudo python script.py`.
deprecated RPi.GPIO is no longer actively maintained and may not support newer kernels or Pi 5. Consider using gpiozero, lgpio, or rpi-lgpio.
fix Switch to gpiozero (high-level) or lgpio (low-level).
gotcha GPIO.setwarnings(False) is required to suppress warnings about channel already in use when not cleaning up.
fix Add GPIO.setwarnings(False) after import and before any setup calls.
gotcha After using GPIO, call GPIO.cleanup() to release pins. Failure can leave pins in unexpected states and cause issues on subsequent runs.
fix Use try/finally or atexit to ensure GPIO.cleanup() is called.
sudo pip install RPi.GPIO

Blink an LED on BCM pin 18 using BCM numbering.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

while True:
    GPIO.output(18, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(18, GPIO.LOW)
    time.sleep(1)