0
0
Raspberry Piprogramming~10 mins

Multiple LED patterns in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple LED patterns
Start Program
Initialize LEDs
Select Pattern
Pattern 1: Blink All
Turn LEDs ON
Wait
Turn LEDs OFF
Wait
Back to Select Pattern
Pattern 2: Chase
Turn ON LED 1
Turn OFF LED 1
Turn ON LED 2
Back to Select Pattern
Pattern 3: Alternate
Turn ON even LEDs
Wait
Turn ON odd LEDs
Wait
Back to Select Pattern
The program initializes LEDs, then repeatedly selects and runs different LED patterns in a loop.
Execution Sample
Raspberry Pi
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
leds = [17, 27, 22]
for led in leds:
    GPIO.setup(led, GPIO.OUT)

# Pattern 1: Blink all LEDs
for _ in range(2):
    for led in leds:
        GPIO.output(led, True)
    time.sleep(0.5)
    for led in leds:
        GPIO.output(led, False)
    time.sleep(0.5)
This code sets up three LEDs and blinks them all on and off twice.
Execution Table
StepActionLED States (17,27,22)Wait (seconds)Output/Note
1Setup GPIO mode BCMOFF, OFF, OFF0GPIO initialized
2Setup LED pins as outputOFF, OFF, OFF0Pins 17,27,22 ready
3Turn ON all LEDsON, ON, ON0All LEDs lit
4WaitON, ON, ON0.5Pause for 0.5 seconds
5Turn OFF all LEDsOFF, OFF, OFF0All LEDs off
6WaitOFF, OFF, OFF0.5Pause for 0.5 seconds
7Turn ON all LEDsON, ON, ON0All LEDs lit again
8WaitON, ON, ON0.5Pause for 0.5 seconds
9Turn OFF all LEDsOFF, OFF, OFF0All LEDs off again
10WaitOFF, OFF, OFF0.5Pause for 0.5 seconds
11End of blink loopOFF, OFF, OFF0Pattern 1 complete
💡 Blink loop runs twice, then program can continue to next pattern or stop.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9Final
LED 17 stateOFFONOFFONOFFOFF
LED 27 stateOFFONOFFONOFFOFF
LED 22 stateOFFONOFFONOFFOFF
Key Moments - 3 Insights
Why do all LEDs turn off after being turned on?
Because the code explicitly sets each LED to OFF after the wait (see steps 5 and 9 in execution_table), turning them off to create the blinking effect.
Why is there a wait after turning LEDs on or off?
The wait (time.sleep) pauses the program so the LEDs stay in their current state long enough to be visible (see steps 4, 6, 8, 10). Without it, changes would be too fast to see.
How does the loop control how many times LEDs blink?
The for loop runs twice (see execution_sample code), so the on-off sequence repeats two times, shown by steps 3-6 and 7-10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED 27 state at Step 5?
AON
BBlinking
COFF
DUndefined
💡 Hint
Check the 'LED States' column at Step 5 in the execution_table.
At which step does the program wait for the first time?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the first 'Wait' action in the execution_table.
If the wait time is removed, what happens to the LED blinking pattern?
ABlinking happens too fast to see
BLEDs stay OFF permanently
CLEDs stay ON permanently
DProgram crashes
💡 Hint
Refer to key_moments about why wait is needed after turning LEDs on/off.
Concept Snapshot
Multiple LED patterns on Raspberry Pi:
- Initialize GPIO pins as outputs
- Use loops to create patterns (blink, chase, alternate)
- Turn LEDs ON/OFF with GPIO.output(pin, True/False)
- Use time.sleep() to pause and make patterns visible
- Repeat patterns in loops for continuous effect
Full Transcript
This example shows how to control multiple LEDs connected to a Raspberry Pi. First, the program sets up the GPIO pins for output. Then it runs a simple blink pattern where all LEDs turn on together, wait half a second, then turn off, and wait again. This on-off cycle repeats twice. The execution table traces each step, showing LED states and wait times. Variables track each LED's state after each step. Key moments explain why waits are needed and how loops control repetition. The quiz tests understanding of LED states at specific steps and the role of wait. The snapshot summarizes how to create multiple LED patterns using GPIO control and timing.