gpio_inputs_-_button_led_control_using_an_interrupt
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
gpio_inputs_-_button_led_control_using_an_interrupt [2016/12/13 21:28] – external edit 127.0.0.1 | gpio_inputs_-_button_led_control_using_an_interrupt [2017/02/01 22:05] (current) – removed walkeradmin | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== GPIO Inputs - Button LED Control using an Interrupt ====== | ||
- | \\ | ||
- | \\ | ||
- | The previous example of using a button to light an LED, everything worked fine, but our processor was at 100%, this needs fixing, so in this example instead of constantly polling and setting GPIO pins, we are using a interrupt where by we do nothing until a button is actually pressed. | ||
- | \\ | ||
- | \\ | ||
- | Create a new file in the normal way ( you can download the python file {{ : | ||
- | \\ | ||
- | sudo nano LED-Button-i.py | ||
- | |||
- | #import modules | ||
- | import RPi.GPIO as GPIO # This imports the GPIO library that allows the use of the GPIO pins, | ||
- | import time # This imports the time library (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, | ||
- | | ||
- | # 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.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 | ||
- | \\ | ||
- | Once you have your file saved, you can run it by using: | ||
- | \\ | ||
- | sudo python LED-Button-i.py | ||
- | \\ | ||
- | ===== Example Output ===== | ||
- | \\ | ||
- | Here you can see the code running {{ : | ||
- | \\ | ||
- | The main advantage using the **interrupt** is that my processor occupancy on the Pi Zero is now around 10% normal, and 14% when I press the button. That's much better than the previous constant 100%. | ||
- | \\ | ||
- | \\ | ||
gpio_inputs_-_button_led_control_using_an_interrupt.1481664529.txt.gz · Last modified: 2023/03/09 22:35 (external edit)