Skip to content

Instantly share code, notes, and snippets.

@ritiek

ritiek/push_switch.py

Last active Jul 24, 2019
Embed
What would you like to do?
Detect 4-pin push switch presses with Raspberry Pi's GPIO pins
# Code stolen shamelessly from
# https://www.raspberrypi.org/forums/viewtopic.php?p=876947&sid=44c21629719b08f74de2b3b465db8d5c#p876947
# I merely modified some bits
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pin = 4
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
presses = 1
def my_callback(channel):
global presses
print("callback called " + str(presses) + " times")
presses += 1
GPIO.add_event_detect(pin, GPIO.FALLING, callback=my_callback, bouncetime=500)
print("Waiting")
while True:
try:
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.