Raspberry Pi Program for LED Chaser with GPIO Control
RPi.GPIO library to control GPIO pins and create an LED chaser by turning LEDs on and off in sequence with time.sleep() delays; for example, set up pins, then loop through them turning each LED on and off in order.Examples
How to Think About It
Algorithm
Code
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: for pin in led_pins: GPIO.output(pin, GPIO.HIGH) time.sleep(0.2) GPIO.output(pin, GPIO.LOW) except KeyboardInterrupt: GPIO.cleanup() print("Program stopped and GPIO cleaned up")
Dry Run
Let's trace the LED chaser with 3 LEDs connected to GPIO pins 17, 27, and 22.
Setup GPIO pins
Pins 17, 27, 22 are set as outputs.
Start loop
Begin infinite loop to turn LEDs on and off.
Turn on LED at pin 17
GPIO.output(17, HIGH) turns LED on.
Wait 0.2 seconds
Pause so LED stays lit visibly.
Turn off LED at pin 17
GPIO.output(17, LOW) turns LED off.
Repeat for pins 27 and 22
Same on/off with delay for each pin.
| Iteration | LED Pin On | LED Pin Off |
|---|---|---|
| 1 | 17 | None |
| 2 | 27 | 17 |
| 3 | 22 | 27 |
| 4 | 17 | 22 |
Why This Works
Step 1: GPIO Setup
We set the GPIO pins as outputs so we can control the LEDs by sending HIGH or LOW signals.
Step 2: Loop Through LEDs
The program loops through each LED pin, turning it on and then off to create the chasing effect.
Step 3: Delay for Visibility
Using time.sleep() pauses the program so each LED stays lit long enough to see the movement.
Alternative Approaches
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) led_pins = [17, 27, 22] pwm_objects = {} for pin in led_pins: GPIO.setup(pin, GPIO.OUT) pwm_objects[pin] = GPIO.PWM(pin, 100) pwm_objects[pin].start(0) try: while True: for pin in led_pins: for duty_cycle in range(0, 101, 5): pwm_objects[pin].ChangeDutyCycle(duty_cycle) time.sleep(0.02) for duty_cycle in range(100, -1, -5): pwm_objects[pin].ChangeDutyCycle(duty_cycle) time.sleep(0.02) except KeyboardInterrupt: for pwm in pwm_objects.values(): pwm.stop() GPIO.cleanup()
# This requires additional hardware and libraries; code controls many LEDs via shift register # Not shown here due to hardware complexity
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through each LED pin once per cycle, so time grows linearly with the number of LEDs.
Space Complexity
Space is proportional to the number of LEDs because we store their pin numbers in a list.
Which Approach is Fastest?
The simple on/off loop is fastest and easiest; PWM adds complexity and CPU usage but creates smoother effects.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple GPIO On/Off Loop | O(n) | O(n) | Basic LED chaser with few LEDs |
| PWM Brightness Fade | O(n) | O(n) | Smooth fading effects, more CPU usage |
| Shift Register Control | O(n) | O(1) | Controlling many LEDs with fewer pins |
GPIO.cleanup() at the end to reset pins and avoid warnings on next run.