0
0
Raspberry Piprogramming~10 mins

First GPIO program (LED blink) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - First GPIO program (LED blink)
Start Program
Setup GPIO Pin as Output
Turn LED ON
Wait (delay)
Turn LED OFF
Wait (delay)
Repeat Loop
The program sets up a pin to control an LED, then repeatedly turns it on and off with pauses in between.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

while True:
    GPIO.output(18, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(18, GPIO.LOW)
    time.sleep(1)
This code makes an LED connected to pin 18 blink on and off every second.
Execution Table
StepActionPin 18 StateDelay (seconds)Loop Iteration
1Setup pin 18 as outputLOW (default)00
2Turn LED ON (HIGH)HIGH01
3Wait 1 secondHIGH11
4Turn LED OFF (LOW)LOW01
5Wait 1 secondLOW11
6Repeat loop back to turn LED ONLOW02
7Turn LED ON (HIGH)HIGH02
8Wait 1 secondHIGH12
9Turn LED OFF (LOW)LOW02
10Wait 1 secondLOW12
...Loop continues indefinitelyAlternates HIGH/LOW1 each delay3,4,5,...
💡 Program runs forever until manually stopped (no automatic exit).
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 9Final
Pin 18 StateLOW (default)HIGHLOWLOWLOWAlternates HIGH/LOW
Loop Iteration01122Increases by 1 each cycle
Key Moments - 3 Insights
Why does the LED blink instead of staying on?
Because the program turns the LED ON, waits 1 second, then turns it OFF and waits 1 second repeatedly, as shown in steps 2-5 in the execution table.
What happens if we remove the time.sleep() calls?
The LED would appear to stay ON or OFF because the program would switch states too fast to see blinking, as the delays control visible timing (see steps 3 and 5).
Why does the program never stop on its own?
Because the while True loop runs forever, repeating the ON/OFF cycle endlessly, as noted in the exit_note and steps 6-10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of pin 18 at step 4?
ALOW
BUndefined
CHIGH
DFlashing
💡 Hint
Check the 'Pin 18 State' column at step 4 in the execution_table.
At which step does the program wait for 1 second after turning the LED ON?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Wait 1 second' after 'Turn LED ON' in the execution_table.
If the delay after turning LED OFF is removed, how would the blinking change?
ALED would stay ON
BLED would stay OFF
CLED would blink faster
DNo change
💡 Hint
Refer to key_moments about the role of time.sleep() delays controlling blink speed.
Concept Snapshot
First GPIO program (LED blink):
- Import GPIO and time modules
- Set GPIO mode (BCM)
- Setup pin 18 as output
- Loop: turn LED ON, wait 1s, turn OFF, wait 1s
- Repeat forever to blink LED
- Use delays to make blinking visible
Full Transcript
This program controls an LED connected to Raspberry Pi pin 18. It first sets the pin as an output. Then, inside an endless loop, it turns the LED on by setting the pin HIGH, waits one second, turns the LED off by setting the pin LOW, and waits another second. This cycle repeats forever, causing the LED to blink on and off every second. The delays are important to make the blinking visible to our eyes. Without delays, the LED would switch states too fast to notice. The program runs continuously until stopped manually.