0
0
Raspberry Piprogramming~10 mins

Single LED control in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Single LED control
Start Program
Setup GPIO Pin as Output
Turn LED ON
Wait for a moment
Turn LED OFF
Cleanup GPIO
End Program
The program sets up the GPIO pin, turns the LED on, waits, turns it off, cleans up, then ends.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

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

GPIO.output(18, GPIO.HIGH)  # LED ON

time.sleep(1)  # wait 1 second

GPIO.output(18, GPIO.LOW)   # LED OFF

GPIO.cleanup()
This code turns an LED connected to pin 18 on for 1 second, then turns it off and cleans up.
Execution Table
StepActionPin 18 StateOutput/Effect
1Set GPIO mode to BCMN/AGPIO library ready to use BCM numbering
2Setup pin 18 as outputN/APin 18 ready to control LED
3Set pin 18 HIGHHIGHLED turns ON
4Wait 1 secondHIGHLED stays ON for 1 second
5Set pin 18 LOWLOWLED turns OFF
6Cleanup GPION/AGPIO pins reset to default
7Program endsN/ALED remains OFF, program stops
💡 Program ends after cleaning up GPIO, LED is OFF
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Pin 18 StateN/AHIGHLOWLOW
Key Moments - 3 Insights
Why do we need to call GPIO.cleanup() at the end?
GPIO.cleanup() resets the pins so they don't stay in output mode, preventing unexpected behavior. See step 6 in execution_table.
What happens if we skip the time.sleep(1) line?
The LED would turn ON and immediately OFF, so you might not see it light up. This is shown between steps 3 and 5.
Why do we use GPIO.setmode(GPIO.BCM)?
It tells the program to use the Broadcom pin numbering, matching the physical pin 18. This is step 1 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of pin 18 after step 3?
ALOW
BN/A
CHIGH
DUndefined
💡 Hint
Check the 'Pin 18 State' column at step 3 in the execution_table.
At which step does the LED turn OFF?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for when 'Pin 18 State' changes to LOW in the execution_table.
If we remove GPIO.cleanup(), what would likely happen?
AGPIO pins remain set, possibly causing issues later
BProgram will not run
CLED stays ON forever
DLED blinks automatically
💡 Hint
Refer to key_moments about why cleanup is important.
Concept Snapshot
Single LED control on Raspberry Pi:
- Use GPIO.setmode(GPIO.BCM) to select pin numbering
- Setup pin as output with GPIO.setup(pin, GPIO.OUT)
- Turn LED ON with GPIO.output(pin, GPIO.HIGH)
- Use time.sleep() to keep LED ON
- Turn LED OFF with GPIO.output(pin, GPIO.LOW)
- Always call GPIO.cleanup() at the end
Full Transcript
This program controls a single LED connected to Raspberry Pi pin 18. First, it sets the GPIO mode to BCM to use Broadcom pin numbers. Then it sets pin 18 as an output pin. The program turns the LED ON by setting pin 18 HIGH, waits for one second so the LED stays lit, then turns the LED OFF by setting pin 18 LOW. Finally, it calls GPIO.cleanup() to reset the pins to their default state. This prevents pins from staying in output mode and avoids unexpected behavior. The program then ends with the LED off.