Mini project — A traffic light simulation

Lesson 25 of 26 · Basic Python

This project simulates how traffic lights work by printing text. Later you will be able to move the same program to a real device (LEDs in MicroPython).


How it works


  • A red light
  • A yellow light
  • A green light

The simulation code



import time

while True:
    print("RED")
    time.sleep(3)

    print("YELLOW")
    time.sleep(1)

    print("GREEN")
    time.sleep(3)

Connecting with MicroPython


In MicroPython, instead of print() you will use LEDs, and instead of a computer — a microcontroller. The logic of the program stays the same.


The same traffic lights on the micro:bit