User Tools

Site Tools


gpio_inputs_-_button_led_control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
gpio_inputs_-_button_led_control [2016/08/09 21:14] walkeradmingpio_inputs_-_button_led_control [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 32: Line 32:
     sudo nano LED-Button.py     sudo nano LED-Button.py
 \\  \\ 
 +** You can download the code {{ :led-button.zip |Here}}: **
 \\  \\ 
-#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, 
-                           # These libraries are built in to Raspbian. +                               # These libraries are built in to Raspbian. 
- +     
-GPIO.setmode (GPIO.BOARD)  # This sets the GPIO pin numbering. Our LED is connected to Pin 12, +    GPIO.setmode (GPIO.BOARD)  # This sets the GPIO pin numbering. Our LED is connected to Pin 12, 
-                           # so we can reference it by using BOARD as pin 12. However there is +                               # 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 +                               # 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). +                               # 12 is called GPIO18 (a reference to its place on the chip). 
- +     
-GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # setup GPIO Pin 11 as an input, and set +    GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # setup GPIO Pin 11 as an input, and set 
-                                                    # the resistor to Pull Down (PUD_DOWN) +                                                        # the resistor to Pull Down (PUD_DOWN) 
-                                                    # this is the pin the button is connected to +                                                        # this is the pin the button is connected to 
-                                                    # button is connected from pin 11 to the +                                                        # button is connected from pin 11 to the 
-                                                    # +3.3v pin on the GPIO +                                                        # +3.3v pin on the GPIO 
- +     
-GPIO.setup(12, GPIO.OUT)   # Sets the GPIO pin as output. This is connected to the LED, then +    GPIO.setup(12, GPIO.OUT)   # Sets the GPIO pin as output. This is connected to the LED, then 
-                           # from the LED to 0v via a 330 Ohm resistor. +                               # from the LED to 0v via a 330 Ohm resistor. 
- +     
-GPIO.output(12, 0)         # sets the GPIO Pin 12 to low (so 0v) +    GPIO.output(12, 0)         # sets the GPIO Pin 12 to low (so 0v) 
- +     
-try: +    try: 
-        while True:                             # start a loop +            while True:                             # start a loop 
-                if (GPIO.input(11) == 0):       # if GPIO pin 11 is a 0 (Low (0v)) then.. +                    if (GPIO.input(11) == 0):       # if GPIO pin 11 is a 0 (Low (0v)) then.. 
-                        GPIO.output(12,0)       # set Pin 12 to 0v (LED Stays off) +                            GPIO.output(12,0)       # set Pin 12 to 0v (LED Stays off) 
-                else:                           # if GPIO pin is anything other than High (3.3v) then.. +                    else:                           # if GPIO pin is anything other than High (3.3v) then.. 
-                        GPIO.output(12,1)       # set Pin 12 to 3.3v (LED comes on) +                            GPIO.output(12,1)       # set Pin 12 to 3.3v (LED comes on) 
-except KeyboardInterrupt:                       # if Ctrl-C is pressed, exit loop +    except KeyboardInterrupt:                       # if Ctrl-C is pressed, exit loop 
-        GPIO.cleanup()                          # reset GPIO pins to default state +            GPIO.cleanup()                          # reset GPIO pins to default state 
- +     
-#End +    #End 
 +\\  
 +\\  
 +==== The Button in Action ==== 
 +Here is the button turning on the LED {{:led-button.mp4|950x574|autoplay,loop}}
  
 +\\ 
 +\\ 
 +==== 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.1470773646.txt.gz · Last modified: 2023/03/09 22:35 (external edit)