0
0
Raspberry Piprogramming~10 mins

Button press detection in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Button press detection
Start program
Setup GPIO pin as input
Wait for button press event
Detect button state change
If pressed, execute action
Loop back to wait for next press
Program ends on exit
The program sets up a GPIO pin to read button presses, waits for the button to be pressed, then runs code when pressed, repeating until stopped.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

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

while True:
    if GPIO.input(18) == GPIO.LOW:
        print("Button pressed!")
        time.sleep(0.3)
This code waits for a button connected to pin 18 to be pressed and prints a message each time.
Execution Table
StepGPIO Pin 18 StateCondition (GPIO.input(18) == LOW)ActionOutput
1HIGH (not pressed)FalseWait
2HIGH (not pressed)FalseWait
3LOW (pressed)TruePrint messageButton pressed!
4LOW (pressed)TruePrint messageButton pressed!
5HIGH (released)FalseWait
6LOW (pressed)TruePrint messageButton pressed!
7HIGH (released)FalseWait
...............
ExitProgram stopped by userN/AExit loop
💡 Program runs until user stops it manually (e.g., Ctrl+C).
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
GPIO Pin 18 StateHIGHLOWHIGHLOWDepends on button
Condition (GPIO.input(18) == LOW)FalseTrueFalseTrueDepends on button
Key Moments - 3 Insights
Why does the program print multiple times when the button is held down?
Because the loop keeps checking the button state and prints each time it sees LOW (pressed), as shown in execution_table rows 3 and 4.
Why do we use pull_up_down=GPIO.PUD_UP in setup?
It sets the pin to HIGH by default so the button press pulls it LOW, making detection reliable, as seen in the initial HIGH state in the execution_table.
What stops the program from running forever?
The program runs in an infinite loop until the user manually stops it, as noted in the exit_note and final execution_table row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the GPIO Pin 18 state at step 5?
ALOW (pressed)
BFloating (undefined)
CHIGH (released)
DNot connected
💡 Hint
Check the 'GPIO Pin 18 State' column at step 5 in the execution_table.
At which step does the condition GPIO.input(18) == LOW become true for the first time?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Condition' column in the execution_table to find when it first becomes True.
If the button is never pressed, what will the output be?
ANo output, program waits
BButton pressed! printed repeatedly
CError message
DProgram crashes
💡 Hint
Refer to the execution_table rows where GPIO Pin 18 state is HIGH and condition is False.
Concept Snapshot
Button press detection on Raspberry Pi:
- Setup GPIO pin as input with pull-up resistor
- Loop: read pin state
- If LOW (button pressed), run action
- Use time.sleep to debounce
- Program runs until stopped manually
Full Transcript
This program sets up a Raspberry Pi GPIO pin 18 as an input with a pull-up resistor. It then enters an infinite loop checking the pin state. When the button connected to pin 18 is pressed, the pin reads LOW, triggering the program to print 'Button pressed!'. The program waits briefly to avoid multiple prints from one press. It continues checking until the user stops it. The execution table shows the pin state and actions step by step, helping beginners see how the program reacts to button presses.