gpio_inputs_-_button_led_control_using_an_interrupt
This is an old revision of the document!
GPIO Inputs - Button LED Control using an Interrupt
blah blah
#import modules import RPi.GPIO as GPIO # This imports the GPIO libarary that allows the use of the GPIO pins, import time # This imports the time libarary (for delays among other things) # These libraries are built in to Raspbian. GPIO.setmode (GPIO.BOARD) # This sets the GPIO pin numbering. Our LED is connected to Pin 12, # so we can reference it by using BOARD as pin 12. However there is # another option (BCM) where we can reference a pin by it's name, pin # 12 is called GPIO18 (a reference to its place on the chip). GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # setup GPIO Pin 11 as an input, and set # the resistor to Pull Down (PUD_DOWN) # this is the pin the button is connected to # button is connected from pin 11 to the # +3.3v pin on the GPIO # this def buttonPressed needs to be defined before it can be reference in the GPIO.add_event_detect def buttonPressed(channel): # this is where our code will look when button is pressed print "Button is Pressed" # print something to the screen GPIO.output(12,1) # set GPIO pin 12 to high (3.3v) so LED will come on # this is where we setup the GPIO input to use the event buttonPressed that was # defined previously. bouncetime is a simple switch debouncer in mS. GPIO.add_event_detect(11, GPIO.RISING, callback=buttonPressed, bouncetime=500) GPIO.setup(12, GPIO.OUT) # Sets the GPIO pin as output. This is connected to the LED, then # from the LED to 0v via a 330 Ohm resistor. GPIO.output(12, 0) # sets the GPIO Pin 12 to low (so 0v) try: while True: # start a loop time.sleep(1) # 1 second delay (or LED wont stay illuminated GPIO.output(12,0) # Set PIN 12 to 0v so LED is off except KeyboardInterrupt: # if Ctrl-C is pressed, exit loop GPIO.cleanup() # reset GPIO pins to default state #End
gpio_inputs_-_button_led_control_using_an_interrupt.1470780030.txt.gz ยท Last modified: 2023/03/09 22:35 (external edit)