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 20:48] walkeradmingpio_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 {{ :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,
-    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)  # 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,
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, GPIO.OUT)   # Sets the GPIO pin as output. (as opposed to inputwhich would +    GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # setup GPIO Pin 11 as an input, and set 
-                               # read in a voltage (but only in terms of 0 or a 1))+                                                        # 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, GPIO.OUT)   # Sets the GPIO pin as output. This is connected to the LEDthen 
 +                               # from the LED to 0v via 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)
          
-    time.sleep(1             waits one second (settling time+    try: 
-     +            while True:                             # start a loop 
-    GPIO.output(12, 1        sets the GPIO Pin 12 to high (so 3.3v) LED will turn on +                    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) 
-    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) then.. 
 +                            GPIO.output(12,1)       # set Pin 12 to 3.3v (LED comes on) 
 +    except KeyboardInterrupt:                       # if Ctrl-C is pressed, exit loop 
 +            GPIO.cleanup()                          reset GPIO pins to default state
          
-    GPIO.cleanup()             Resets all the GPIO pins to their default state (LED will go off)+    #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.1470772116.txt.gz · Last modified: 2023/03/09 22:35 (external edit)