0
0
Raspberry Piprogramming~10 mins

LED toggle with button in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LED toggle with button
Start
Wait for button press
Button pressed?
NoWait
Yes
Toggle LED state
Update LED output
Back to wait
The program waits for a button press, then toggles the LED state and updates the LED output, repeating this cycle.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

led_state = False
while True:
    button = GPIO.input(23)
    if button == GPIO.LOW:
        led_state = not led_state
        GPIO.output(18, led_state)
        time.sleep(0.3)
This code toggles an LED on pin 18 each time the button on pin 23 is pressed.
Execution Table
StepButton InputCondition (button pressed?)LED State BeforeLED State AfterActionDelay
1HIGHNoFalseFalseWaitNo
2HIGHNoFalseFalseWaitNo
3LOWYesFalseTrueToggle LED ONYes (0.3s)
4LOWYesTrueFalseToggle LED OFFYes (0.3s)
5HIGHNoFalseFalseWaitNo
6LOWYesFalseTrueToggle LED ONYes (0.3s)
ExitN/ALoop continuesN/AN/AProgram runs indefinitelyN/A
💡 The program runs forever, toggling LED each time button is pressed (button LOW).
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6Final
led_stateFalseFalseTrueFalseTrueTrue
buttonN/AHIGHLOWLOWLOWLOW
Key Moments - 3 Insights
Why does the LED toggle only when the button input is LOW?
Because the button is set up with a pull-up resistor, pressing it connects the pin to ground, making input LOW. The execution_table rows 3,4,6 show toggling happens only when button input is LOW.
Why is there a delay after toggling the LED?
The delay (0.3s) prevents the program from toggling the LED multiple times during one press due to button bouncing, as seen in execution_table rows 3 and 4.
What happens if the button is held down?
The LED toggles once per loop iteration with button LOW, but the delay slows toggling. The program does not detect button release explicitly, so holding the button toggles LED repeatedly but slowly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED state after step 3?
ANo change
BFalse (LED OFF)
CTrue (LED ON)
DError
💡 Hint
Check the 'LED State After' column at step 3 in execution_table.
At which step does the program detect the button is pressed for the first time?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Condition (button pressed?)' column for the first 'Yes' in execution_table.
If the delay after toggling was removed, what would happen?
ALED would stay ON permanently
BLED would toggle very fast and flicker
CButton presses would be ignored
DProgram would crash
💡 Hint
Consider the purpose of the delay in execution_table rows 3 and 4.
Concept Snapshot
LED toggle with button:
- Setup button input with pull-up resistor
- Read button state in loop
- If button pressed (input LOW), invert LED state
- Update LED output
- Add delay to avoid multiple toggles per press
Full Transcript
This program runs on a Raspberry Pi to toggle an LED on and off using a button. The button is connected with a pull-up resistor, so when pressed, the input reads LOW. The program continuously checks the button state. When it detects a press (LOW), it changes the LED state from ON to OFF or OFF to ON. It then updates the LED output pin accordingly. A short delay after toggling prevents the LED from flickering due to button bounce. The program loops forever, waiting for button presses to toggle the LED.