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:18] walkeradmintraffic_light_leds_using_array [2026/07/07 19:48] (current) – [Introduction] walkeradmin
Line 6: Line 6:
 ---- ----
 ===== Introduction ===== ===== 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.
 \\  \\ 
 \\  \\ 
Line 11: Line 13:
 \\  \\ 
 \\  \\ 
-{{ :pico-non-wireless-pinout-with-leds-wiring.png?300 |}}+{{ :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. We have wired the LEDs to GPIO pins 2, 6 & 10. These are the GPIO Pin numbers, not the actual Pi Pico Pin numbers.
Line 19: Line 21:
 \\  \\ 
 <code:python | download> <code:python | download>
-Start by importing lib files "machine" & "time" +#Traffic light LEDs using an array 
-# Imports the machine module, which provides access to hardware features such as GPIO pins, ADCs, PWM, etc. + 
-import machine +from machine import Pin
-# Imports the time module, which provides delay functions like sleep().+
 import time import time
  
-#we're initalizing pin 25 (which is the onboard LED) & pins 2, 6 and 6 as outputs +Define GPIO pins for the LEDs 
-# Creates a pin object called led1. +RED = Pin(2, Pin.OUT) 
-# Uses GPIO pin 25. +AMBER = Pin(6, Pin.OUT) 
-# Sets the pin mode to output (Pin.OUT). +GREEN = Pin(10, Pin.OUT)
-# On a Raspberry Pi Pico, GPIO 25 is usually connected to the onboard LED. +
-led1 machine.Pin(25, machine.Pin.OUT) +
-# Creates an output pin object for GPIO 2. +
-led2 = machine.Pin(2, machine.Pin.OUT) +
-# Creates an output pin object for GPIO 6. +
-led3 machine.Pin(6, machine.Pin.OUT) +
-# Creates an output pin object for GPIO 10. +
-led4 machine.Pin(10, machine.Pin.OUT)+
  
-#Run a loop 5 times +Put them in an array 
-# Starts a loop that repeats 5 times. +lights = [RED, AMBER, GREEN]
-# The variable i takes values from 0 to 5. +
-for i in range(5): +
-# Sets GPIO 2 HIGH (3.3V). +
-# Turns LED 2 on (assuming the LED is wired active-high). +
-    led2.value(1) +
-# Sets GPIO 6 & 10 LOW. +
-# Turns LED 3 and 4 off. +
-    led3.value(0) +
-    led4.value(0) +
-     +
-    #Use time to wait for 0.5 second +
-    time.sleep(0.2)+
  
-The rest of the code is a repetition of what has been done above.  +Helper function to turn all lights off 
-    #Turn the onbaord LED off +def all_off(): 
-    led1.value(0+    for light in lights: 
-     +        light.off()
-    #Turn external LED on pin 2 (only) on +
-    #led1.value(0) +
-    led2.value(1) +
-    led3.value(0) +
-    led4.value(0) +
-    #Use time to wait for 1 second +
-    time.sleep(0.11) +
-    #Turn external LED on pin 3 (only) on +
-    #led1.value(0) +
-    led2.value(0) +
-    led3.value(1) +
-    led4.value(0) +
-    #Use time to wait for 1 second +
-    time.sleep(0.2) +
-    #Turn external LED on pin 4 (only) on +
-    #led1.value(0) +
-    led2.value(0) +
-    led3.value(0) +
-    led4.value(1) +
-    #Use time to wait for 1 second +
-    time.sleep(0.2) +
-     +
-    #Turn the external LED on pin 4 off +
-    led4.value(0) +
-</code>+
  
-The software works by performing the following tasks. +while True: 
-\\  +    # Red 
-  * Import Libraries (Machine and Time)+    all_off() 
 +    RED.on() 
 +    time.sleep(3)
  
-  * * Set GPIO Pins to output+    # Red + Amber 
 +    AMBER.on() 
 +    time.sleep(1)
  
-  * Start a loop that will loop 5 times+    # Green 
 +    all_off() 
 +    GREEN.on() 
 +    time.sleep(3)
  
-  * Turn on the first LED and turn off all other LEDs+    # Amber 
 +    all_off() 
 +    AMBER.on() 
 +    time.sleep(1)
  
-  * Turn on the second LED and turn off all other LEDs+</code>
  
-  * Turn on the third LED and turn off all other LEDs +As you can see from the code, we have the four steps of the traffic light sequenceIn step onewe 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. 
- +\\  
-That's itPretty simple. Rememberyou can control other devices using the GPIO pins other than LEDs, but the current output is limited to around 20mAIf you try to pull more than this, you could damage that GPIO Pin.+\\  
 +Here is the sequence running. 
 +\\  
 +\\  
 +{{ :traffic-light-array.mp4?640x360 |}}
 \\  \\ 
 \\  \\ 
 ---- ----
  
traffic_light_leds_using_array.1781731128.txt.gz · Last modified: by walkeradmin