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 20:48] – walkeradmin | gpio_inputs_-_button_led_control [2023/03/09 22:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 31: | 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 43: | 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, | |
- | time.sleep(3) # waits 3 seconds, so LED will be on for 3 seconds | + | else: # if GPIO pin is anything other than High (3.3v) |
+ | GPIO.output(12, | ||
+ | | ||
+ | GPIO.cleanup() # reset GPIO pins to default state | ||
| | ||
- | | + | #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.1470772116.txt.gz · Last modified: 2023/03/09 22:35 (external edit)