1. Introduction
After learning how to blink and fade a single LED, it is time to scale up. An LED Chaser (also known as a "Knight Rider" light) involves lighting up a series of LEDs one by one in a sequence. This project is perfect for learning how to organize code to handle many components at once without writing long, repetitive scripts.
2. Components
Raspberry Pi (Any model)
Breadboard
5x LEDs (Different colors look best)
5x 220 ohms Resistors
Jumper Wires (M-F)
3. Circuit and connections
| Raspberry Pi Pinout Diagram |
We will connect five LEDs to five different GPIO pins. Each LED must have its own resistor to protect the Pi.
LED 1: Anode to GPIO 17, Cathode through resistor to GND.
LED 2: Anode to GPIO 27, Cathode through resistor to GND.
LED 3: Anode to GPIO 22, Cathode through resistor to GND.
LED 4: Anode to GPIO 10, Cathode through resistor to GND.
LED 5: Anode to GPIO 9, Cathode through resistor to GND.
4. Detailed Step By Step Circuit working
Each LED is connected to an independent GPIO pin, meaning we can control them individually.
The shared Ground (GND) rail on the breadboard allows all five circuits to complete their path back to the Pi.
The Python script will send a High signal to one pin at a time while keeping others Low.
By moving the "High" signal across the pins with a short delay, we create the illusion of a single light "chasing" across the board.
5. Libraries to be included
We will use the LEDBoard class from GPIO Zero. This is much more efficient than defining five separate LED objects.
from gpiozero import LEDBoardfrom time import sleep
6. Code
from gpiozero import LEDBoard
from time import sleep
# Define the pins in a list
leds = LEDBoard(17, 27, 22, 10, 9)
print("LED Chaser Active... Press Ctrl+C to stop")
try:
while True:
# Loop through each LED in the sequence
for led in leds:
led.on() # Turn current LED on
sleep(0.1) # Wait a moment
led.off() # Turn current LED off
except KeyboardInterrupt:
leds.off()
print("\nChaser Stopped")
7. Detailed Step By Step Code working
LEDBoard(17, 27, 22, 10, 9): This creates a collection of LEDs. It treats them as a group, which makes our code much cleaner.
for led in leds:: This is a "For Loop." It automatically goes through the pins in the order you listed them (17 first, then 27, etc.).led.on()andled.off(): Inside the loop, the code turns one LED on, pauses for 0.1 seconds, and turns it off before moving to the next one in the list.while True:: This ensures that once the "chase" reaches the 5th LED, it starts back at the 1st LED immediately.
8. Tips
Speed Control: Change the
sleep(0.1)value to0.05for a faster chase or0.5for a slower one.Wiring Neatness: Since there are many wires, use a common GND rail on your breadboard to keep the setup tidy.
Color Patterns: Try using 3 Red LEDs and 2 Yellow LEDs to create a "fire" or "warning" effect.
9. Uses
Decorative Lighting: Used in Diwali or Christmas decorations.
Scanning Effects: Like the "Cylon" eye or "Knight Rider" car lights.
Progress Bars: Indicating the status of a download or a physical process in a machine.
10. Conclusion
The LED Chaser is a gateway to complex projects like LED Cubes or scrolling displays. You have learned how to use Lists and Loops to control multiple outputs.