Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active July 24, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/eb268fc0d917e0d11d077511dfe28dcd to your computer and use it in GitHub Desktop.
Save ritiek/eb268fc0d917e0d11d077511dfe28dcd to your computer and use it in GitHub Desktop.
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