LED Control with Python
Control LEDs with a Raspberry Pi and Python using GPIO pins. A beginner-friendly electronics project with code examples.
Introduction
Controlling LEDs with Python on a Raspberry Pi is the foundation for building interactive hardware projects. Whether you're blinking indicators, controlling status lights, or dimming display LEDs, GPIO pin control via Python is straightforward and powerful.
Prerequisites
- Raspberry Pi 4/5 with GPIO header
- Python 3.9+
- LED (any color, 5mm or 10mm)
- 330Ω resistor (current-limiting resistor)
- Jumper wires and breadboard
pip install RPi.GPIOorpip install gpiozero(we'll cover both)
Pin Wiring Reference
Connect your LED to GPIO 17 (pin 11) with a current-limiting resistor:
Raspberry Pi 3.3V (pin 1) ──┐
│
[LED]
│
[330Ω]
│
GPIO 17 (pin 11) ───────────┘
GND (pin 6/9/14/20/25/30/34/39) ──[LED cathode via resistor]
Pin diagram note: Always use a 330Ω resistor (or 220Ω minimum) to protect the GPIO pin. A 3.3V GPIO pin can supply ~16mA max; the resistor limits current to ~10mA, keeping the LED safe and within GPIO specifications.
Step 1 — Basic LED Blink with RPi.GPIO
Install and import the library:
sudo apt update
sudo apt install python3-rpi.gpio python3-pip -y
Create a file blink.py:
import RPi.GPIO as GPIO
import time
# Setup GPIO mode (BCM = Broadcom numbering)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Define GPIO pin
LED_PIN = 17
# Set pin as output
GPIO.setup(LED_PIN, GPIO.OUT)
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off
time.sleep(1)
except KeyboardInterrupt:
print("\nShutdown")
finally:
GPIO.cleanup()
Run it:
python3 blink.py
Your LED should blink at 1-second intervals. Press Ctrl+C to stop.
Step 2 — PWM Brightness Control
Pulse-width modulation (PWM) lets you adjust LED brightness by varying the duty cycle. Same wiring, different code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED_PIN = 17
GPIO.setup(LED_PIN, GPIO.OUT)
# Create PWM object: pin and frequency (50 Hz)
pwm = GPIO.PWM(LED_PIN, 50)
pwm.start(0) # Start at 0% duty cycle
try:
while True:
# Fade in: 0 to 100%
for brightness in range(101):
pwm.ChangeDutyCycle(brightness)
time.sleep(0.02)
# Fade out: 100 to 0%
for brightness in range(100, -1, -1):
pwm.ChangeDutyCycle(brightness)
time.sleep(0.02)
except KeyboardInterrupt:
print("\nShutdown")
finally:
pwm.stop()
GPIO.cleanup()
Run it and watch the LED smoothly fade in and out.
Step 3 — Multiple LEDs
Wire 3 LEDs to GPIO 17, 27, and 22. Control them independently:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LEDS = [17, 27, 22]
for pin in LEDS:
GPIO.setup(pin, GPIO.OUT)
try:
while True:
for pin in LEDS:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.3)
GPIO.output(pin, GPIO.LOW)
except KeyboardInterrupt:
print("\nShutdown")
finally:
GPIO.cleanup()
This pattern creates a running light effect.
Step 4 — Using gpiozero (Simpler API)
The gpiozero library abstracts GPIO complexity. Install it:
pip3 install gpiozero
Rewrite the fade example:
from gpiozero import PWMLED
from time import sleep
led = PWMLED(17)
try:
while True:
led.pulse() # Built-in fade effect
except KeyboardInterrupt:
print("\nShutdown")
finally:
led.close()
Or simple blink:
from gpiozero import LED
from time import sleep
led = LED(17)
try:
while True:
led.on()
sleep(1)
led.off()
sleep(1)
except KeyboardInterrupt:
print("\nShutdown")
finally:
led.close()
gpiozero handles cleanup automatically and reads more intuitively. Use it for new projects.
Troubleshooting
No LED light — Verify polarity (longer leg to 3.3V), check resistor value isn't too high (1kΩ+ will be dim), and confirm GPIO 17 is physically accessible on your board.
Permission denied error — Either run with sudo or add your user to the GPIO group: sudo usermod -aG gpio $USER (then logout and login).
LED too bright/dim — Adjust resistor value or PWM duty cycle. For 10mm LEDs at 3.3V, try 220Ω for brighter, 470Ω for dimmer.
GPIO stays high after exit — Always call GPIO.cleanup() or use gpiozero.close(). If you skip it, the pin remains set, and you may have conflicts on next run.
Summary
GPIO LED control is the gateway to Raspberry Pi hardware projects. RPi.GPIO gives you raw control; gpiozero simplifies the API. Start with simple blink, move to PWM fading, then expand to multi-LED patterns. From there, add buttons for input, sensors for data, or relays for high-power switching.