Button Event Sample

This sample demonstrates reading onboard button press state using interrupt.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 Button_Event.py

Sample Code

Button_Event.py
#!/usr/bin/env python3

#This sample demonstrates reading onboard button press state using interrupt.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
import RPi.GPIO as GPIO

#Button Press Event
def button_event(ch):
    print ("Button is " + ("released. " if GPIO.input(button) else "pressed."))

#Button Pin
button = 5

#Initialize
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(button, GPIO.BOTH, callback=button_event, bouncetime=100)
#GPIO Edge detection options: GPIO.BOTH, GPIO.RISING or GPIO.FALLING

#Add your configuration code here

try:
    while True:
        #Add your code here

        #Wait
        sleep(1.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints button change state if you press the onboard button. The example output should be like this:

Button is pressed.
Button is released.

To stop the Python script, just press CTRL + C.

Last updated