gpio_inputs_-_button_led_control
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
gpio_inputs_-_button_led_control [2016/08/09 21:14] – walkeradmin | gpio_inputs_-_button_led_control [2017/10/22 16:38] – [The Button in Action] walkeradmin | ||
---|---|---|---|
Line 31: | Line 31: | ||
\\ | \\ | ||
sudo nano LED-Button.py | sudo nano LED-Button.py | ||
+ | \\ | ||
+ | ** You can download the code {{ : | ||
+ | \\ | ||
+ | #import modules | ||
+ | import RPi.GPIO as GPIO # This imports the GPIO libarary that allows the use of the GPIO pins, | ||
+ | # 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 | ||
+ | | ||
+ | GPIO.setup(12, | ||
+ | # from the LED to 0v via a 330 Ohm resistor. | ||
+ | | ||
+ | GPIO.output(12, | ||
+ | | ||
+ | try: | ||
+ | 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. | ||
\\ | \\ | ||
\\ | \\ | ||
- | #import modules | ||
- | import RPi.GPIO as GPIO # This imports the GPIO libarary that allows the use of the GPIO pins, | ||
- | # 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 | ||
- | |||
- | GPIO.setup(12, | ||
- | # from the LED to 0v via a 330 Ohm resistor. | ||
- | |||
- | GPIO.output(12, | ||
- | |||
- | try: | ||
- | 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 | ||
- | |||
- |
gpio_inputs_-_button_led_control.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1