0
0
Raspberry Piprogramming~10 mins

Button class with callbacks in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Button class with callbacks
Create Button object
Assign callback function
Wait for button press event
Detect button press
Call callback function
Callback runs user code
Wait for next event or exit
This flow shows how a Button object is created, a callback function is assigned, and when the button is pressed, the callback runs automatically.
Execution Sample
Raspberry Pi
import gpiozero

def on_press():
    print('Button pressed!')

button = gpiozero.Button(2)
button.when_pressed = on_press
This code creates a button on GPIO pin 2 and prints a message when the button is pressed.
Execution Table
StepActionEvaluationResult
1Import gpiozeroModule loadedgpiozero available
2Define on_press functionFunction createdon_press ready
3Create Button on pin 2Button object createdbutton assigned
4Assign on_press to when_pressedCallback linkedbutton.when_pressed = on_press
5Wait for button pressIdleWaiting...
6Button pressed detectedEvent triggeredCallback called
7Run on_press functionPrint executesOutput: 'Button pressed!'
8Return to waitIdleWaiting for next press
💡 Program runs indefinitely until stopped; callbacks run on each button press.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 7Final
buttonNoneButton object on pin 2Callback assignedCallback executedCallback ready for next press
on_pressNoneFunction definedFunction linkedFunction ranFunction ready
Key Moments - 2 Insights
Why doesn't the on_press function run immediately when assigned?
Because in the execution_table at Step 4, the function is assigned as a callback, not called. It runs only when the button press event happens at Step 6.
What happens if the button is never pressed?
As shown in Step 5, the program waits idle. The callback never runs until a press event triggers it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'button' after Step 3?
AButton pressed detected
BButton object created on pin 2
CCallback function assigned
DFunction on_press executed
💡 Hint
Check the 'Result' column for Step 3 in execution_table.
At which step does the callback function actually run?
AStep 7
BStep 4
CStep 6
DStep 5
💡 Hint
Look for when 'Run on_press function' happens in execution_table.
If the button is never pressed, which step will the program stay at indefinitely?
AStep 7
BStep 6
CStep 5
DStep 4
💡 Hint
See where the program is 'Idle' waiting for input in execution_table.
Concept Snapshot
Button class with callbacks:
- Create Button(pin_number)
- Define callback function
- Assign callback to button.when_pressed
- Callback runs automatically on press
- Program waits for events
- Useful for responsive hardware input
Full Transcript
This example shows how to use a Button class with callbacks on a Raspberry Pi. First, the gpiozero module is imported. Then, a function named on_press is defined to print a message. A Button object is created on GPIO pin 2. The on_press function is assigned as the callback to run when the button is pressed. The program waits for the button press event. When the button is pressed, the callback runs and prints 'Button pressed!'. The program then waits for the next press. This setup allows the program to respond immediately to button presses without constantly checking the button state.