Create a gist now

Instantly share code, notes, and snippets.

@fvdbosch /gpiobuttons.py Secret
Last active Mar 25, 2017

What would you like to do?
GPIO Button Adapter Demo
#!/usr/bin/env python
import time
import scrollphathd
import RPi.GPIO as GPIO
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Button callback
def my_callback(channel):
scrollphathd.clear()
if(channel == 5):
scrollphathd.write_string("1")
elif(channel == 6):
scrollphathd.write_string("2")
elif(channel == 12):
scrollphathd.write_string("3")
elif(channel == 13):
scrollphathd.write_string("4")
elif(channel == 16):
scrollphathd.write_string("5")
elif(channel == 26):
scrollphathd.write_string("6")
else:
scrollphathd.write_string("0")
scrollphathd.show()
# Wait for button presses
GPIO.add_event_detect(5, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(6, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(12, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(13, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(16, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=my_callback, bouncetime=300)
# Pause here, exit on Enter
raw_input("Press Enter to exit\n>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment