Button with interrupt (GPIO.add_event_detect)
📖 Scenario: You have a Raspberry Pi connected to a physical button. You want to detect when the button is pressed without constantly checking its state. Using an interrupt, your program will respond immediately when the button is pressed.
🎯 Goal: Build a Python program that sets up a button on GPIO pin 18 and uses GPIO.add_event_detect to run a function when the button is pressed.
📋 What You'll Learn
Use the RPi.GPIO library
Set up GPIO pin 18 as input with a pull-up resistor
Create a function called
button_pressed that prints 'Button was pressed!'Use
GPIO.add_event_detect to call button_pressed on a falling edgeKeep the program running to listen for button presses
💡 Why This Matters
🌍 Real World
Physical buttons are often used in Raspberry Pi projects to control devices or trigger actions immediately without delay.
💼 Career
Understanding GPIO interrupts is important for embedded systems, IoT devices, and hardware interfacing roles.
Progress0 / 4 steps