Raspberry Pi GPIO Multi-LED Control System
Build a sequential LED chaser using Raspberry Pi 2B GPIO pins — complete with circuit diagram, wiring guide, and code.
Project Overview
This project demonstrates how to control 10 LEDs (8 red + 2 yellow) using the GPIO pins on a Raspberry Pi 2 Model B. Each LED is connected through a current-limiting resistor on a breadboard. A loop in the Arduino/GPIO sketch sequentially turns each LED on and off, creating a chaser effect — similar to traffic lights or embedded status indicators.
GPIO Output Control
Drive 3.3V HIGH/LOW signals directly from Pi GPIO pins to LEDs via resistors.
LED Chaser Effect
Sequential 500ms on/off timing creates a looping light-show animation.
Expandable Design
Add sensors, buttons, or IoT connectivity to turn this into a smart system.
STEM Learning
Ideal for classrooms, lab experiments, and learning embedded electronics fundamentals.
Components Required
Circuit Diagram
The diagram below shows how each LED is wired to a GPIO pin through a 220Ω resistor. All LED cathodes (negative legs, shorter leg) connect to GND.
Figure 1: Schematic — each LED connects through a 220Ω resistor from a GPIO pin to the GND rail. All 10 LED cathodes share the common GND rail.
Step-by-Step Wiring Guide
-
1
Place all 10 LEDs on the breadboard
Insert 8 red LEDs and 2 yellow LEDs into the breadboard across different rows. Ensure each LED spans the center divider — anode (longer leg, +) on one side, cathode (shorter leg, –) on the other.
-
2
Connect a 220Ω resistor to each LED anode
Place one 220Ω resistor in series with the anode (longer leg) of each LED. The resistor's other end will connect to the GPIO pin wire. This limits current to ~6mA per LED, within the safe operating range of GPIO pins.
-
3
Connect all LED cathodes to the GND rail
Use short jumper wires to connect the cathode (short leg) of every LED to the breadboard's negative (–) power rail. This is the shared GND connection.
-
4
Wire GPIO pins to resistors with male-to-female jumpers
Connect male-to-female jumper wires from the Raspberry Pi GPIO pins to the free end of each resistor. Follow the pin map: GPIO 26 → LED 1 resistor, GPIO 19 → LED 2 resistor, and so on down to GPIO 8 → LED 10 resistor.
-
5
Connect GND rail to Pi GND pin
Run a jumper wire from the breadboard's GND rail to any GND pin on the Raspberry Pi GPIO header (e.g., physical pin 6, 9, 14, 20, 25, 30, 34, or 39). This completes the circuit.
-
6
Double-check all connections before powering on
Verify each resistor is between the GPIO pin wire and the LED anode — never between the cathode and GND. Confirm LED polarity. Check that no bare wires short across adjacent GPIO pins.
-
7
Upload the sketch and power the Raspberry Pi
Flash the Arduino sketch (shown below) to the Pi GPIO system, or run the Python equivalent. Power on the Pi via Micro USB. The LEDs should begin sequencing one by one every 500ms.
GPIO Pin Reference
The Code
The sketch initializes each GPIO pin as OUTPUT in setup(), then loops through all 10 LEDs sequentially — turning each on for 500ms, then off for 500ms — creating a continuous chaser effect.
/*
* Raspberry Pi GPIO Multi-LED Control System
*
* Controls 10 LEDs (8 red + 2 yellow) via GPIO pins.
* Each LED is connected through a 220Ω current-limiting resistor.
* The loop creates a sequential chaser effect with 500ms timing.
*
* Pin assignments:
* Red LEDs: GPIO 26, 19, 13, 6, 5, 21, 20, 16
* Yellow LEDs: GPIO 7, 8
* GND: Any GND pin on GPIO header
*/
void setup() {
// Initialize all GPIO pins as digital outputs
pinMode(26, OUTPUT); // Red LED 1
pinMode(19, OUTPUT); // Red LED 2
pinMode(13, OUTPUT); // Red LED 3
pinMode(6, OUTPUT); // Red LED 4
pinMode(5, OUTPUT); // Red LED 5
pinMode(21, OUTPUT); // Red LED 6
pinMode(20, OUTPUT); // Red LED 7
pinMode(16, OUTPUT); // Red LED 8
pinMode(7, OUTPUT); // Yellow LED 1
pinMode(8, OUTPUT); // Yellow LED 2
}
void loop() {
// Array of all GPIO pins in sequence
int ledPins[] = {26, 19, 13, 6, 5, 21, 20, 16, 7, 8};
int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
// Sequentially blink each LED with 500ms on / 500ms off
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH); // Turn LED ON (3.3V)
delay(500); // Wait 500ms
digitalWrite(ledPins[i], LOW); // Turn LED OFF (0V)
delay(500); // Wait 500ms
}
}
RPi.GPIO or gpiozero libraries directly on the Raspberry Pi. Use GPIO.output(pin, GPIO.HIGH) and time.sleep(0.5) to replicate the sketch behaviour.
How the timing works
Each iteration of the for loop takes 1 second (500ms ON + 500ms OFF). With 10 LEDs, one full pass through all LEDs takes 10 seconds, then it repeats continuously. Adjust the delay() value to change speed.
Simulation Links
Test and verify the circuit in browser-based simulators before building on real hardware. Both tools let you place components, wire them up, and run the code without any physical parts.
Cirkit Designer — Online Simulation
Drag-and-drop Raspberry Pi + LEDs + resistors. Paste the sketch, click Run, and watch the LEDs sequence in real time.
Wokwi — Free Raspberry Pi Simulator
Wokwi supports Raspberry Pi Pico and GPIO simulation with MicroPython or C. Ideal for testing GPIO LED chaser patterns instantly.
How It Works
GPIO Digital Output
The Raspberry Pi's GPIO pins can be set as either INPUT or OUTPUT. In OUTPUT mode, writing HIGH sends 3.3V to the pin, and LOW sends 0V (ground). This voltage difference is what turns an LED on or off.
Current Limiting
LEDs require a specific forward current (typically 10–20mA) and have a forward voltage of around 2V. The GPIO output is 3.3V, so the remaining 1.3V must be dropped across the resistor. Using 220Ω: I = 1.3V ÷ 220Ω ≈ 5.9mA — safe for both the LED and the GPIO pin (max 16mA per pin).
Sequential Chaser Logic
The ledPins[] array stores all 10 pin numbers. The for loop iterates through each, sending HIGH then LOW with delays. Only one LED is on at any given moment, creating the chaser pattern. The infinite loop() function repeats this forever.
Learning Outcomes
Extensions & Next Steps
Add a Push Button
Wire a button to a GPIO input pin and start/stop the sequence on press.
Sensor-Triggered LEDs
Connect a DHT11 or PIR sensor — light specific LEDs based on readings.
IoT Dashboard Control
Add Wi-Fi via ESP8266 and control LEDs from a cloud dashboard remotely.
PWM Brightness Control
Use analogWrite() / PWM to dim LEDs smoothly, not just on/off.
Comments
Post a Comment