0
0
Iot-protocolsProgramBeginner · 2 min read

Raspberry Pi Program for LED Chaser with GPIO Control

Use Python with the 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

Input3 LEDs connected to GPIO pins 17, 27, 22
OutputLEDs light up one by one in a repeating sequence with a 0.2 second delay
Input5 LEDs connected to GPIO pins 5, 6, 13, 19, 26
OutputLEDs chase from first to last pin repeatedly with visible on/off pattern
InputNo LEDs connected
OutputProgram runs but no visible LED changes; no errors if pins are set as output
🧠

How to Think About It

To create an LED chaser on Raspberry Pi, think of each LED as a light you can turn on or off by controlling a GPIO pin. You set each pin as output, then in a loop, turn on one LED at a time while turning off the others, pausing briefly between changes to create a moving light effect.
📐

Algorithm

1
Set up the GPIO pins connected to LEDs as outputs
2
Create a list of these GPIO pins
3
In an infinite loop, for each pin in the list:
4
Turn on the current LED by setting its pin HIGH
5
Turn off the previous LED by setting its pin LOW
6
Wait for a short delay to make the effect visible
7
Repeat the sequence continuously
💻

Code

python
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")
Output
Program runs with LEDs lighting up one by one in a loop until stopped; on stop, prints: 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.

1

Setup GPIO pins

Pins 17, 27, 22 are set as outputs.

2

Start loop

Begin infinite loop to turn LEDs on and off.

3

Turn on LED at pin 17

GPIO.output(17, HIGH) turns LED on.

4

Wait 0.2 seconds

Pause so LED stays lit visibly.

5

Turn off LED at pin 17

GPIO.output(17, LOW) turns LED off.

6

Repeat for pins 27 and 22

Same on/off with delay for each pin.

IterationLED Pin OnLED Pin Off
117None
22717
32227
41722
💡

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

Using PWM for brightness fade
python
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 method creates a fading LED chaser effect but is more complex and uses PWM signals.
Using a shift register for more LEDs
python
# This requires additional hardware and libraries; code controls many LEDs via shift register
# Not shown here due to hardware complexity
Using a shift register allows controlling many LEDs with fewer GPIO pins but needs extra hardware and setup.

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.

ApproachTimeSpaceBest For
Simple GPIO On/Off LoopO(n)O(n)Basic LED chaser with few LEDs
PWM Brightness FadeO(n)O(n)Smooth fading effects, more CPU usage
Shift Register ControlO(n)O(1)Controlling many LEDs with fewer pins
💡
Always call GPIO.cleanup() at the end to reset pins and avoid warnings on next run.
⚠️
Beginners often forget to set GPIO mode to BCM or BOARD, causing pin numbering errors.