traffic_lights

This is an old revision of the document!


Table of Contents

Traffic Lights

Jun 2026


Introduction


The following code works with both the Raspberry Pi Pico and the RP2040 boards from Ali. The difference is that we are using different GPIO pins. On the RPi Pico, we are using Pins 2, 6 and 10. The reason for this is that the RPi Pico has a GND pin next to each of these GPIO pins, so wiring is very simple.


Wiring

For our RP2040 mini device, we will not use these pins. There is only one GND pin so we will have to wire everything to a GND rail on our breadboard.


Above we can see the wiring for the RP2040 and the LEDs.

  • Pin 6 = Red LED
  • Pin 7 = Amber LED
  • Pin 8 = Green LED




| download
#Traffic light LEDs using an array
 
from machine import Pin
import time
 
# Define GPIO pins for the LEDs
RED = Pin(6, Pin.OUT)
AMBER = Pin(7, Pin.OUT)
GREEN = Pin(8, 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)
traffic_lights.1784481944.txt.gz · Last modified: by walkeradmin