-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt_example.py
29 lines (24 loc) · 1.19 KB
/
interrupt_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python2.7
# script by Alex Eames https://raspi.tv/
# https://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# GPIO 23 set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print("Make sure you have a button connected so that when pressed")
print("it will connect GPIO port 23 (pin 16) to GND (pin 6)\n")
input("Press Enter when ready\n>")
print("Waiting for falling edge on port 23")
# now the program will do nothing until the signal on port 23
# starts to fall towards zero. This is why we used the pullup
# to keep the signal high and prevent a false interrupt
print("During this waiting time, your computer is not")
print("wasting resources by polling for a button press.\n")
print("Press your button when ready to initiate a falling edge interrupt.")
try:
GPIO.wait_for_edge(23, GPIO.FALLING)
print("\nFalling edge detected. Now your program can continue with")
print("whatever was waiting for a button press.")
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit