Level 5 · pyfirmata2

LED Traffic Light

Control red, yellow, and green LEDs on an Arduino board. If you don't have hardware, read through the code and talk through the logic with your instructor.

What it teaches: Controlling hardware (Arduino)

How to run

Starter file: starter_pyfirmata_led.py

python starter_pyfirmata_led.py

Starter code

"""
LEVEL 5 - PYFIRMATA2
Starter: LED Traffic Light (Arduino required)

Hardware needed: Arduino board (with Firmata firmware uploaded) +
3 LEDs (red, yellow, green) with resistors, wired to digital pins 8, 9, 10.
If you don't have hardware, read through the code and talk through the
logic with your instructor - or ask your AI tool to explain each line.

Run with: python starter_pyfirmata_led.py
"""
from pyfirmata2 import Arduino
import time

PORT = Arduino.AUTODETECT   # or type your port manually e.g. "COM3" / "/dev/cu.usbmodem14101"

board = Arduino(PORT)
print("Connected to Arduino on:", board.get_port())

red = board.get_pin('d:8:o')     # digital pin 8, output
yellow = board.get_pin('d:9:o')  # digital pin 9, output
green = board.get_pin('d:10:o')  # digital pin 10, output

def all_off():
    red.write(0)
    yellow.write(0)
    green.write(0)

def traffic_light_cycle():
    all_off()
    green.write(1)
    print("GREEN - go!")
    time.sleep(4)

    green.write(0)
    yellow.write(1)
    print("YELLOW - slow down!")
    time.sleep(2)

    yellow.write(0)
    red.write(1)
    print("RED - stop!")
    time.sleep(4)

    red.write(0)

try:
    while True:
        traffic_light_cycle()
except KeyboardInterrupt:
    all_off()
    board.exit()
    print("Stopped. Board disconnected safely.")


# ---------------------------------------------------------------
# LEVEL UP! Try one (or more) of these upgrades:
# 1. Add a push button (digital input) that lets a "pedestrian" trigger
#    the light to change early.
# 2. Add a buzzer that beeps during the yellow light.
# 3. Read an analog sensor (like a light sensor) and change LED
#    behavior based on real-world brightness.
# 4. Make the timing adjustable with variables at the top of the file.
# 5. Combine this with the guizero app (Level 2) - add buttons in a
#    GUI window that control the LEDs instead of using code only.
# 6. Ask your AI tool: "How do I read a button press with pyfirmata2
#    and use it to change my program's behavior?"
# ---------------------------------------------------------------

Challenge ideas

No hardware? Walk through the code logic with a partner and explain what each line would do if hardware were connected. Ask your AI tool to explain what board.get_pin('d:8:o') means.

Easy

  • Change the timing of each light (how long red/yellow/green stay on).
  • Change which pins the LEDs are wired to (update the pin numbers).

Medium

  • Add a push button as a digital input to trigger a "pedestrian crossing".
  • Add a buzzer (digital output) that beeps during the yellow light.

Hard

  • Read an analog sensor (light/temperature) and change behavior based on real-world data.
  • Combine with the Level 2 guizero app: build a GUI with buttons that control the LEDs, instead of a fixed automatic cycle.

Ask your AI tool

How do I read a button press with pyfirmata2 and react to it in Python?