User Tools

Site Tools


traffic_light_leds_using_array

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
traffic_light_leds_using_array [2026/06/17 21:19] – [Introduction] walkeradmintraffic_light_leds_using_array [2026/07/07 19:48] (current) – [Introduction] walkeradmin
Line 4: Line 4:
 \\  \\ 
 {{ :micro-python.png?100 |}} {{ :micro-python.png?100 |}}
 +----
 +===== Introduction =====
 +\\ 
 +In this LED example, we are creating a traffic light sequence, but instead of addressing every LED at every stage, we are going to put the GPIO pins in to an array, and address just the LEDs that need to change each time.
 +\\ 
 +\\ 
 +Here is a wiring diagram for the LED setup. This is what we will use when we run the software below.
 +\\ 
 +\\ 
 +{{ :pico-non-wireless-pinout-with-leds-traffic-light-order.png?300 |}}
 +\\ 
 +We have wired the LEDs to GPIO pins 2, 6 & 10. These are the GPIO Pin numbers, not the actual Pi Pico Pin numbers.
 +\\ 
 +\\ 
 +Below is the software being used to drive the LEDs
 +\\ 
 +<code:python | download>
 +#Traffic light LEDs using an array
 +
 +from machine import Pin
 +import time
 +
 +# Define GPIO pins for the LEDs
 +RED = Pin(2, Pin.OUT)
 +AMBER = Pin(6, Pin.OUT)
 +GREEN = Pin(10, Pin.OUT)
 +
 +# Put them in an array
 +lights = [RED, AMBER, GREEN]
 +
 +# Helper function to turn all lights off
 +def all_off():
 +    for light in lights:
 +        light.off()
 +
 +while True:
 +    # Red
 +    all_off()
 +    RED.on()
 +    time.sleep(3)
 +
 +    # Red + Amber
 +    AMBER.on()
 +    time.sleep(1)
 +
 +    # Green
 +    all_off()
 +    GREEN.on()
 +    time.sleep(3)
 +
 +    # Amber
 +    all_off()
 +    AMBER.on()
 +    time.sleep(1)
 +
 +</code>
 +
 +As you can see from the code, we have the four steps of the traffic light sequence. In step one, we can also turn off all the LEDs by calling the function we defined called <color #00a2e8>all_off()</color> with the command '<color #00a2e8>all_off()</color>' command.
 +\\ 
 +\\ 
 +Here is the sequence running.
 +\\ 
 +\\ 
 +{{ :traffic-light-array.mp4?640x360 |}}
 +\\ 
 +\\ 
 ---- ----
  
traffic_light_leds_using_array.1781731160.txt.gz · Last modified: by walkeradmin