Project 1.3: "Basic Traffic Light"
🚀 Project 1.3 – Basic Traffic Light
🎯 What you’ll learn
- ✅ Goal 1: Control three LEDs (red, yellow, green) in sequences.
- ✅ Goal 2: Create realistic traffic light timing and night mode.
- ✅ Goal 3: Add an integrated pedestrian button for interaction.
Key ideas
- Digital output: Control multiple pins to turn LEDs on/off.
- Delay: Timing creates realistic behavior.
- Loop: Repeat sequences continuously.
- Input: A button lets pedestrians request a crossing.
🧱 Blocks glossary (used in this project)
- Digital output: Turn a pin ON (HIGH) or OFF (LOW).
- Delay: Pause the program for a set time.
- Loop (while True): Repeat actions forever.
- Input (button): Read the button state with pull-up to detect presses.
🧰 What you need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable (30 cm) |
| LED Red | 1 | Pin 2 → GND |
| LED Yellow | 1 | Pin 4 → GND |
| LED Green | 1 | Pin 5 → GND |
| Button | 1 | Pin 14 → GND |
| Shield | 1 | For easy wiring |
🔌 Wiring tip: Long leg of each LED → pin; short leg → GND. Use pull‑up for the button.
📍 Pin map snapshot: Pin 2 = Red, Pin 4 = Yellow, Pin 5 = Green, Pin 14 = Button (PULL_UP).
✅ Before you start
- USB: Cable plugged in.
- LEDs: Wired to 2, 4, 5; button to 14 with PULL_UP.
- Test print: Run this first.
print("Ready!") # Confirma que el puerto serial muestra mensajes
🎮 Microprojects (5 mini missions)
🎮 Microproject 1.3.1 – Red-green sequence
Goal: Alternate red and green LEDs.
Blocks used:
- Digital output: Control LEDs.
- Delay: Wait 3 seconds.
- Loop: Repeat sequence.
Block sequence:
- Red ON → Delay 3s → Red OFF
- Green ON → Delay 3s → Green OFF
- Repeat
MicroPython code:
import machine, time # Importa librerías para pines y tiempo
pin2 = machine.Pin(2, machine.Pin.OUT) # Define LED rojo en pin 2 como salida
pin5 = machine.Pin(5, machine.Pin.OUT) # Define LED verde en pin 5 como salida
while True: # Repite la secuencia para siempre
pin2.value(1) # Enciende rojo (STOP)
print("RED ON") # Mensaje serial: rojo encendido
time.sleep(3) # Espera 3 segundos
pin2.value(0) # Apaga rojo
print("RED OFF") # Mensaje serial: rojo apagado
pin5.value(1) # Enciende verde (GO)
print("GREEN ON") # Mensaje serial: verde encendido
time.sleep(3) # Espera 3 segundos
pin5.value(0) # Apaga verde
print("GREEN OFF") # Mensaje serial: verde apagado
Reflection: You built a simple stop/go cycle.
Challenge: Increase green to 5s for longer “go”.
🎮 Microproject 1.3.2 – Red-yellow-green sequence
Goal: Insert yellow between red and green.
Blocks used:
- Digital output: Control LEDs.
- Delay: 3s red, 1s yellow, 3s green.
- Loop: Repeat.
Block sequence:
- Red ON → 3s → OFF
- Yellow ON → 1s → OFF
- Green ON → 3s → OFF
- Repeat
MicroPython code:
import machine, time # Importa librerías
pin2 = machine.Pin(2, machine.Pin.OUT) # Rojo salida
pin4 = machine.Pin(4, machine.Pin.OUT) # Amarillo salida
pin5 = machine.Pin(5, machine.Pin.OUT) # Verde salida
while True: # Bucle continuo
pin2.value(1) # Rojo ON
print("RED ON") # Serial: rojo encendido
time.sleep(3) # Espera 3s
pin2.value(0) # Rojo OFF
print("RED OFF") # Serial: rojo apagado
pin4.value(1) # Amarillo ON
print("YELLOW ON") # Serial: amarillo encendido
time.sleep(1) # Espera 1s
pin4.value(0) # Amarillo OFF
print("YELLOW OFF") # Serial: amarillo apagado
pin5.value(1) # Verde ON
print("GREEN ON") # Serial: verde encendido
time.sleep(3) # Espera 3s
pin5.value(0) # Verde OFF
print("GREEN OFF") # Serial: verde apagado
Reflection: Yellow adds safe transition.
Challenge: Try 2s yellow for a longer caution.
🎮 Microproject 1.3.3 – Realistic traffic light times
Goal: Use 5s red, 4s green, 2s yellow.
Blocks used:
- Digital output: Control LEDs.
- Delay: Realistic durations.
- Loop: Repeat.
Block sequence:
- Red 5s → OFF
- Green 4s → OFF
- Yellow 2s → OFF
- Repeat
MicroPython code:
import machine, time # Importa librerías
pin2 = machine.Pin(2, machine.Pin.OUT) # Rojo salida
pin4 = machine.Pin(4, machine.Pin.OUT) # Amarillo salida
pin5 = machine.Pin(5, machine.Pin.OUT) # Verde salida
while True: # Ciclo realista
pin2.value(1) # Rojo ON (5s)
print("RED ON 5s") # Serial: rojo 5s
time.sleep(5) # Espera 5s
pin2.value(0) # Rojo OFF
print("RED OFF") # Serial: rojo apagado
pin5.value(1) # Verde ON (4s)
print("GREEN ON 4s") # Serial: verde 4s
time.sleep(4) # Espera 4s
pin5.value(0) # Verde OFF
print("GREEN OFF") # Serial: verde apagado
pin4.value(1) # Amarillo ON (2s)
print("YELLOW ON 2s") # Serial: amarillo 2s
time.sleep(2) # Espera 2s
pin4.value(0) # Amarillo OFF
print("YELLOW OFF") # Serial: amarillo apagado
Reflection: Timing choices make systems feel real.
Challenge: Adapt times to your city’s style.
🎮 Microproject 1.3.4 – Night mode (flashing yellow)
Goal: Flash yellow on/off at 0.5s.
Blocks used:
- Digital output: Yellow LED.
- Delay: 0.5s intervals.
- Loop: Repeat.
Block sequence:
- Yellow ON → 0.5s → OFF → 0.5s
- Repeat
MicroPython code:
import machine, time # Importa librerías
pin4 = machine.Pin(4, machine.Pin.OUT) # Amarillo salida
while True: # Parpadeo continuo
pin4.value(1) # Amarillo ON
print("YELLOW FLASH ON") # Serial: amarillo encendido
time.sleep(0.5) # Espera 0.5s
pin4.value(0) # Amarillo OFF
print("YELLOW FLASH OFF") # Serial: amarillo apagado
time.sleep(0.5) # Espera 0.5s
Reflection: Night mode improves safety.
Challenge: Change to 1s for slower flashing.
🎮 Microproject 1.3.5 – Integrated pedestrian button
Goal: Trigger a safe crossing when the button is pressed.
Blocks used:
- Input: Button with pull-up.
- Digital output: LEDs.
- Delay: Crossing times.
- Loop: Wait and act.
Block sequence:
- Wait for button press (value == 0)
- Red ON 5s → OFF
- Green ON 4s → OFF
- Yellow ON 2s → OFF
- Return to waiting
MicroPython code:
import machine, time # Importa librerías
pin2 = machine.Pin(2, machine.Pin.OUT) # Rojo salida
pin4 = machine.Pin(4, machine.Pin.OUT) # Amarillo salida
pin5 = machine.Pin(5, machine.Pin.OUT) # Verde salida
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) # Botón entrada con pull-up
while True: # Espera y atiende al peatón
if button.value() == 0: # Si se presiona el botón (LOW por pull-up)
print("PEDESTRIAN REQUEST") # Mensaje serial: solicitud de cruce
pin2.value(1) # Rojo ON (detener tráfico)
print("RED ON (PEDESTRIAN)") # Serial: rojo encendido para peatones
time.sleep(5) # Espera 5s
pin2.value(0) # Rojo OFF
print("RED OFF") # Serial: rojo apagado
pin5.value(1) # Verde ON (cruce permitido)
print("GREEN ON (PEDESTRIAN)") # Serial: verde encendido peatón
time.sleep(4) # Espera 4s
pin5.value(0) # Verde OFF
print("GREEN OFF") # Serial: verde apagado
pin4.value(1) # Amarillo ON (precaución fin de cruce)
print("YELLOW ON (PEDESTRIAN)") # Serial: amarillo encendido
time.sleep(2) # Espera 2s
pin4.value(0) # Amarillo OFF
print("YELLOW OFF") # Serial: amarillo apagado
else: # Si no se presiona el botón
time.sleep(0.05) # Pequeña espera para evitar lectura rápida
Reflection: A button adds human interaction to the system.
Challenge: Add a message “WAIT…” while the system is idle.
✨ Main project – Basic traffic light
🔧 Blocks steps (with glossary)
- Digital output: Control LEDs ON/OFF.
- Delay: Create realistic timing.
- Loop: Repeat the cycle continuously.
- Input: Button triggers pedestrian crossing.
Block sequence:
- Setup pins for red (2), yellow (4), green (5), and button (14, pull‑up).
- Run standard cycle (red → yellow → green) with realistic times.
- Monitor button; when pressed, run pedestrian crossing sequence.
- Return to standard cycle or keep waiting.
🐍 MicroPython code (mirroring blocks)
# Project 1.3 – Basic Traffic Light
import machine, time # Importa librerías
pin2 = machine.Pin(2, machine.Pin.OUT) # LED rojo salida
pin4 = machine.Pin(4, machine.Pin.OUT) # LED amarillo salida
pin5 = machine.Pin(5, machine.Pin.OUT) # LED verde salida
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP) # Botón con pull-up
while True: # Bucle principal del semáforo
# Secuencia estándar
pin2.value(1) # Rojo ON (alto)
print("RED ON (cycle)") # Serial: rojo encendido ciclo
time.sleep(5) # Tiempo realista rojo
pin2.value(0) # Rojo OFF
print("RED OFF (cycle)") # Serial: rojo apagado ciclo
pin4.value(1) # Amarillo ON (transición)
print("YELLOW ON (cycle)") # Serial: amarillo encendido ciclo
time.sleep(2) # Tiempo realista amarillo
pin4.value(0) # Amarillo OFF
print("YELLOW OFF (cycle)") # Serial: amarillo apagado ciclo
pin5.value(1) # Verde ON (avance)
print("GREEN ON (cycle)") # Serial: verde encendido ciclo
time.sleep(4) # Tiempo realista verde
pin5.value(0) # Verde OFF
print("GREEN OFF (cycle)") # Serial: verde apagado ciclo
# Revisa botón peatonal
if button.value() == 0: # Si el botón está presionado
print("PEDESTRIAN MODE START") # Serial: inicio modo peatón
pin2.value(1) # Rojo ON (detener)
print("RED ON (pedestrian)") # Serial: rojo encendido peatón
time.sleep(5) # Espera 5s
pin2.value(0) # Rojo OFF
print("RED OFF (pedestrian)") # Serial: rojo apagado peatón
pin5.value(1) # Verde ON (cruce)
print("GREEN ON (pedestrian)") # Serial: verde encendido peatón
time.sleep(4) # Espera 4s
pin5.value(0) # Verde OFF
print("GREEN OFF (pedestrian)") # Serial: verde apagado peatón
pin4.value(1) # Amarillo ON (final)
print("YELLOW ON (pedestrian)") # Serial: amarillo encendido peatón
time.sleep(2) # Espera 2s
pin4.value(0) # Amarillo OFF
print("YELLOW OFF (pedestrian)") # Serial: amarillo apagado peatón
print("PEDESTRIAN MODE END") # Serial: fin modo peatón
📖 External explanation
- What it teaches: How to simulate a real traffic light and respond to pedestrian input.
- Why it works: Digital outputs control LEDs; delays set durations; the loop repeats behavior; button input triggers a safe crossing.
- Key concept: Multiple outputs + timing + input = interactive system.
✨ Story time
Your robot is now the guardian of a busy crosswalk. It keeps traffic flowing, flashes caution at night, and helps pedestrians cross safely with a single button press.
🕵️ Debugging (2 common problems)
🐞 Debugging 1.3.A – Incorrect LED order
Problem: LEDs don’t follow the expected sequence.
Clues: Yellow turns on at the wrong time or green skips.
Broken code:
pin5.value(1) # GREEN ON antes de YELLOW OFF (desorden)
Fixed code:
pin4.value(0) # YELLOW OFF (cierra transición correctamente)
pin5.value(1) # GREEN ON (ahora sí, orden correcto)
Why it works: Turning off the previous LED before turning on the next keeps the proper order.
Avoid next time: Follow the exact block sequence: Red → Yellow → Green.
🐞 Debugging 1.3.B – Out-of-sync timing
Problem: Durations feel wrong or overlap.
Clues: Green seems too long or yellow too short.
Broken code:
time.sleep(0.2) # Amarillo demasiado corto para ser visible
Fixed code:
time.sleep(2) # Ajusta amarillo a 2s (realista y visible)
Why it works: Proper delay values make each phase visible and safe.
Avoid next time: Set delays to 5s red, 4s green, 2s yellow for realism.
✅ Final checklist
- I saw the sequence red → yellow → green in order.
- Night mode flashed yellow at a steady rate.
- The pedestrian button triggered a safe crossing.
📚 Extras
- 🧠 Student tip: Add a “WAIT…” message while no button press is detected.
- 🧑🏫 Instructor tip: Verify pin mapping (2/4/5 LEDs, 14 button) before running.
- 📖 Glossary: Digital output, Delay, Loop, Input (pull‑up).
- 💡 Mini tips: Use clear serial messages and keep delays realistic.