gpio_inputs_-_button_led_control_using_an_interrupt
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
gpio_inputs_-_button_led_control_using_an_interrupt [2016/08/09 22:56] – created walkeradmin | gpio_inputs_-_button_led_control_using_an_interrupt [2016/08/09 22:58] – walkeradmin | ||
---|---|---|---|
Line 2: | Line 2: | ||
\\ | \\ | ||
\\ | \\ | ||
+ | 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) | ||
+ | # 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, | ||
+ | # 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): | ||
+ | print " | ||
+ | GPIO.output(12, | ||
+ | |||
+ | GPIO.add_event_detect(11, | ||
+ | # to use the event buttonPressed that was | ||
+ | # defined previously. bouncetime is a simp$ | ||
+ | # switch debouncer in mS. | ||
+ | |||
+ | GPIO.setup(12, | ||
+ | # from the LED to 0v via a 330 Ohm resistor. | ||
+ | |||
+ | GPIO.output(12, | ||
+ | |||
+ | try: | ||
+ | while True: # start a loop | ||
+ | time.sleep(1) | ||
+ | GPIO.output(12, | ||
+ | except KeyboardInterrupt: | ||
+ | GPIO.cleanup() | ||
+ | |||
+ | #End |