This program runs three LED patterns repeatedly until you stop it with Ctrl+C. It safely cleans up the pins afterward.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED_PINS = [17, 27, 22]
for pin in LED_PINS:
GPIO.setup(pin, GPIO.OUT)
try:
while True:
# Pattern 1: Turn on LEDs one by one
for pin in LED_PINS:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin, GPIO.LOW)
# Pattern 2: Blink all LEDs together
for _ in range(3):
for pin in LED_PINS:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5)
for pin in LED_PINS:
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
# Pattern 3: Light up in order, turn off in reverse
for pin in LED_PINS:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.3)
for pin in reversed(LED_PINS):
GPIO.output(pin, GPIO.LOW)
time.sleep(0.3)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()