gpio_inputs_-_button_led_control
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gpio_inputs_-_button_led_control [2016/08/09 19:47] – walkeradmin | gpio_inputs_-_button_led_control [2017/10/22 15:38] (current) – [The Button in Action] walkeradmin | ||
|---|---|---|---|
| Line 20: | Line 20: | ||
| {{: | {{: | ||
| \\ | \\ | ||
| + | \\ | ||
| + | So for any input that we are waiting for a high signal (3.3v) we will use pull down so it stays a 0v, for any pin we are waiting for a low signal (0v) we will use a pull up so it stays at 3.3v (until a button press makes it low) | ||
| \\ | \\ | ||
| \\ | \\ | ||
| Line 29: | Line 31: | ||
| \\ | \\ | ||
| sudo nano LED-Button.py | sudo nano LED-Button.py | ||
| + | \\ | ||
| + | ** You can download the code {{ : | ||
| \\ | \\ | ||
| #import modules | #import modules | ||
| import RPi.GPIO as GPIO # This imports the GPIO libarary that allows the use of the GPIO pins, | 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. | # These libraries are built in to Raspbian. | ||
| - | | ||
| | | ||
| GPIO.setmode (GPIO.BOARD) | GPIO.setmode (GPIO.BOARD) | ||
| Line 41: | Line 43: | ||
| # 12 is called GPIO18 (a reference to its place on the chip). | # 12 is called GPIO18 (a reference to its place on the chip). | ||
| | | ||
| - | GPIO.setup(12, | + | |
| - | # | + | # 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 | ||
| + | |||
| + | | ||
| + | # | ||
| | | ||
| GPIO.output(12, | GPIO.output(12, | ||
| | | ||
| - | | + | |
| + | while True: # start a loop | ||
| + | if (GPIO.input(11) == 0): # if GPIO pin 11 is a 0 (Low (0v)) then.. | ||
| + | GPIO.output(12, | ||
| + | else: # if GPIO pin is anything other than High (3.3v) then.. | ||
| + | GPIO.output(12, | ||
| + | except KeyboardInterrupt: | ||
| + | GPIO.cleanup() | ||
| | | ||
| - | | + | #End |
| - | + | \\ | |
| - | | + | \\ |
| - | + | ==== The Button in Action ==== | |
| - | | + | Here is the button turning on the LED {{: |
| + | \\ | ||
| + | \\ | ||
| + | ==== A Small Issue ==== | ||
| + | \\ | ||
| + | While this code works without any real problems, because we are in a constant loop, this code is very heavy on CPU load, on the Pi Zero I am using this causes the processer to sit at 100 %, a better method is to use an **interrupt** | ||
| + | \\ | ||
| + | \\ | ||
| + | This guide is comprised of information from a youtube video by **Gaven MacDonald**. Thanks Gaven. | ||
| + | \\ | ||
| + | \\ | ||
gpio_inputs_-_button_led_control.1470772034.txt.gz · Last modified: (external edit)
