After mastering the single LED blink, the next step is to control multiple outputs. In this project, we will connect a Red LED and a Green LED to your Raspberry Pi 4 and make them blink alternately.
1. Components Required
- Raspberry Pi 4 (with Raspberry Pi OS)
- 2x LEDs (Red and Green)
- 220Ω Resistors
- Breadboard and Jumper Wires
2. The Circuit and Connections
Since we are using two LEDs, we need two separate GPIO pins. We will use GPIO 18 (Pin 12) and GPIO 24 (Pin 18).
Connection Table:
| Component | Color | GPIO Pin | Physical Pin |
|---|---|---|---|
| LED 1 | Red | 18 | 12 |
| LED 2 | Green | 24 | 18 |
| Ground | — | GND | 6 |
Note: Both LEDs can share the same Ground (GND) pin by using the "negative rail" on your breadboard.
3. The Python Code
Open Thonny IDE and paste the following code:
from gpiozero import LED
from time import sleep
# Define the two LEDs
red_led = LED(18)
green_led = LED(24)
print("Double LED Blink is running... Press Ctrl+C to stop.")
try:
while True:
red_led.on()
green_led.off()
print("Red is ON")
sleep(0.5)
red_led.off()
green_led.on()
print("Green is ON")
sleep(0.5)
except KeyboardInterrupt:
red_led.off()
green_led.off()
print("\nProgram stopped safely.")
4. Conclusion
You have now learned how to control multiple outputs! This is the foundation for building traffic lights or complex robotics.