traffic_light_leds_using_array
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| traffic_light_leds_using_array [2026/06/17 21:20] – walkeradmin | traffic_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. | ||
| + | \\ | ||
| \\ | \\ | ||
| Here is a wiring diagram for the LED setup. This is what we will use when we run the software below. | Here is a wiring diagram for the LED setup. This is what we will use when we run the software below. | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ : | + | {{ : |
| \\ | \\ | ||
| 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 18: | Line 21: | ||
| \\ | \\ | ||
| < | < | ||
| - | # Start by importing lib files " | + | #Traffic light LEDs using an array |
| - | # Imports the machine | + | |
| - | import | + | from machine import |
| - | # 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, | + | |
| - | # Creates an output pin object for GPIO 2. | + | |
| - | led2 = machine.Pin(2, | + | |
| - | # Creates an output pin object for GPIO 6. | + | |
| - | led3 = machine.Pin(6, | + | |
| - | # Creates an output pin object for GPIO 10. | + | |
| - | led4 = machine.Pin(10, | + | |
| - | #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 | + | |
| - | | + | |
| - | # The rest of the code is a repetition of what has been done above. | + | # Helper function to turn all lights |
| - | #Turn the onbaord LED off | + | def all_off(): |
| - | | + | for light in lights: |
| - | | + | light.off() |
| - | #Turn external LED on pin 2 (only) on | + | |
| - | # | + | |
| - | 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 | + | |
| - | # | + | |
| - | 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 | + | |
| - | # | + | |
| - | 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) | + | |
| - | </ | + | |
| - | The software works by performing the following tasks. | + | while True: |
| - | \\ | + | # Red |
| - | * Import Libraries | + | |
| + | 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 | + | </ |
| - | * 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 sequence. In step one, we can also turn off all the LEDs by calling |
| - | + | \\ | |
| - | That's it. Pretty simple. Remember, you can control other devices using the GPIO pins other than LEDs, but the current output | + | \\ |
| + | Here is the sequence running. | ||
| + | \\ | ||
| + | \\ | ||
| + | {{ : | ||
| \\ | \\ | ||
| \\ | \\ | ||
| ---- | ---- | ||
traffic_light_leds_using_array.1781731201.txt.gz · Last modified: by walkeradmin
