Skip to content

Latest commit

 

History

History
112 lines (84 loc) · 3.25 KB

tutorial-rasp-switchledevent.md

File metadata and controls

112 lines (84 loc) · 3.25 KB
title description author
Button Switch and Event Detected Lighting of LEDs

Tutorial: Button Switch and Event Detected Lighting of LEDs

lnk_switchleds

In this tutorial, you learn how to:

  • Connect an ..
  • Test the ...

Prerequisites

Completed the

Supplies:

Quantity Item
1 Breadboard
6 Male to male jumper wires
2 LEDs
1 Momentary Push Button 4Pin Switch
1 1kΩ Resistor
1 10kΩ Resistor
1 (optional) GPIO Extension Board
1 (optional) 40 pin GPIO cable

Below is the circuit we'll construct.

lnk_schematicswitchleds

Wire your Switch and LEDs

In this section you'll wire your Raspberry Pi to light up an LED display bar by using the following diagram...

Create Code to Turn Sequentially Switch On or Off the LED Graph

  1. Connect to your Raspberry Pi using Visual Studio Code.

  2. Create a file ledbar.py in your cloned GitHub under the python/raspberrypi directory, for example ~/repos/IoT/python/raspberrypi/ledbar.py

  3. Copy and paste the following import statement

    import RPi.GPIO as GPIO
    import time
  4. Copy and paste the following method to obtain basic information about your Raspberry Pi. [todo] explain this!

    def main():
        LED_pins = [8,12,16,18,22,24,26,32,36,38]
    
        GPIO.setmode(GPIO.BOARD)
    
        for p in LED_pins:
            GPIO.setup(p, GPIO.OUT)
            GPIO.output(p, GPIO.HIGH)
    
        try:
            on = True
            while True:
                for p in LED_pins:
                    if on:
                        GPIO.output(p, GPIO.LOW)
                    else:
                        GPIO.output(p, GPIO.HIGH)
                    time.sleep(0.25)
                if(on):
                    on = False
                else:
                    on = True
                time.sleep(2)
        except KeyboardInterrupt:
            print("Program shut down")
        finally:
            GPIO.cleanup()
            print("Cleaning up and shutting down")
    
    if __name__ == "__main__":
        main()

Run It

  1. Start the debugger in Visual Studio Code
  2. Verify your LEDs in the bar graph sequentially turn on and off.

More to Explore

  1. Produce different patterns of turning on and off your LED bar display.
  2. Reverse the polarity of your LED bar graph:
    • Flip the LED bar graph to have the anode (+) pins connected to the resistors
    • Change the jumper lead from ground to a 3.3v pin
    • Change all occurrences in the code from GPIO.LOW to GPIO.HIGH and GPIO.HIGH to GPIO.LOW

Next steps

Tutorial: Remotely Control an LED Bar Graph