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 = FalsewhileTrue:
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
Step
Button Input
Condition (button pressed?)
LED State Before
LED State After
Action
Delay
1
HIGH
No
False
False
Wait
No
2
HIGH
No
False
False
Wait
No
3
LOW
Yes
False
True
Toggle LED ON
Yes (0.3s)
4
LOW
Yes
True
False
Toggle LED OFF
Yes (0.3s)
5
HIGH
No
False
False
Wait
No
6
LOW
Yes
False
True
Toggle LED ON
Yes (0.3s)
Exit
N/A
Loop continues
N/A
N/A
Program runs indefinitely
N/A
💡 The program runs forever, toggling LED each time button is pressed (button LOW).
Variable Tracker
Variable
Start
After Step 1
After Step 3
After Step 4
After Step 6
Final
led_state
False
False
True
False
True
True
button
N/A
HIGH
LOW
LOW
LOW
LOW
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.