title | description | author |
---|---|---|
Button Switch and Event Detected Lighting of LEDs |
In this tutorial, you learn how to:
- Connect an ..
- Test the ...
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.
In this section you'll wire your Raspberry Pi to light up an LED display bar by using the following diagram...
-
Connect to your Raspberry Pi using Visual Studio Code.
-
Create a file
ledbar.py
in your cloned GitHub under thepython/raspberrypi
directory, for example~/repos/IoT/python/raspberrypi/ledbar.py
-
Copy and paste the following import statement
import RPi.GPIO as GPIO import time
-
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()
- Start the debugger in Visual Studio Code
- Verify your LEDs in the bar graph sequentially turn on and off.
- Produce different patterns of turning on and off your LED bar display.
- 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