ESP32 MicroPython Traffic Light

This tutorial instructs you how to control the traffic light module with ESP32 and MicroPython. We will discuss:

ESP32 MicroPython traffic light

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Traffic Light Module
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Traffic Light Module

Pinout

The traffic light module has four pins.

  • GND pin: Connect this ground pin to the ESP32's GND.
  • R pin: This pin operates the red light. Attach it to a digital output on the ESP32.
  • Y pin: This pin operates the yellow light. Attach it to a digital output on the ESP32.
  • G pin: This pin operates the green light. Attach it to a digital output on the ESP32.
Traffic Light Pinout

How It Works

Wiring Diagram

  • How to connect ESP32 and traffic light using breadboard
The wiring diagram between ESP32 MicroPython traffic light

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and traffic light

ESP32 MicroPython Code

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-traffic-light """ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 25 # The ESP32 pin GPIO25 connected to the traffic light module's red pin PIN_YELLOW = 26 # The ESP32 pin GPIO26 connected to the traffic light module's yellow pin PIN_GREEN = 27 # The ESP32 pin GPIO27 connected to the traffic light module's green pin # Define times in seconds (MicroPython uses seconds for time.sleep) RED_TIME = 4 # RED time in seconds YELLOW_TIME = 4 # YELLOW time in seconds GREEN_TIME = 4 # GREEN time in seconds # Setup pins as output red = machine.Pin(PIN_RED, machine.Pin.OUT) yellow = machine.Pin(PIN_YELLOW, machine.Pin.OUT) green = machine.Pin(PIN_GREEN, machine.Pin.OUT) # Main loop while True: # Red light on red.value(1) # turn on red yellow.value(0) # turn off yellow green.value(0) # turn off green time.sleep(RED_TIME) # keep red light on for the defined period # Yellow light on red.value(0) # turn off red yellow.value(1) # turn on yellow green.value(0) # turn off green time.sleep(YELLOW_TIME) # keep yellow light on for the defined period # Green light on red.value(0) # turn off red yellow.value(0) # turn off yellow green.value(1) # turn on green time.sleep(GREEN_TIME) # keep green light on for the defined period

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the traffic light module according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Check out the traffic light status.

Traffic lights work in different ways, depending on their design in each place. Here is an easy explanation of how traffic lights control traffic.

The code above lets you control each light one by one. Now, we will improve the code to make it work better.

ESP32 MicroPython Code Optimization

  • Let's improve the code by creating a function to manage the light.
""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-traffic-light """ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 25 # The ESP32 pin GPIO25 connected to the traffic light module's red pin PIN_YELLOW = 26 # The ESP32 pin GPIO26 connected to the traffic light module's yellow pin PIN_GREEN = 27 # The ESP32 pin GPIO27 connected to the traffic light module's green pin # Define times in seconds (MicroPython uses seconds for time.sleep) RED_TIME = 2 # RED time in seconds YELLOW_TIME = 1 # YELLOW time in seconds GREEN_TIME = 2 # GREEN time in seconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Main loop while True: # Red light on trafic_light_on(RED) time.sleep(times[RED]) # keep red light on during a period of time # Yellow light on trafic_light_on(YELLOW) time.sleep(times[YELLOW]) # keep yellow light on during a period of time # Green light on trafic_light_on(GREEN) time.sleep(times[GREEN]) # keep green light on during a period of time
  • We can improve the code by using a for loop.
""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-traffic-light """ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 25 # The ESP32 pin GPIO25 connected to the traffic light module's red pin PIN_YELLOW = 26 # The ESP32 pin GPIO26 connected to the traffic light module's yellow pin PIN_GREEN = 27 # The ESP32 pin GPIO27 connected to the traffic light module's green pin # Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms) RED_TIME = 2000 # RED time in milliseconds YELLOW_TIME = 1000 # YELLOW time in milliseconds GREEN_TIME = 2000 # GREEN time in milliseconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Main loop while True: for light in range(RED, GREEN + 1): trafic_light_on(light) time.sleep_ms(times[light]) # keep light on during a period of time
  • Let's improve the code by using the millis() function instead of time.sleep().
""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-traffic-light """ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 25 # The ESP32 pin GPIO25 connected to the traffic light module's red pin PIN_YELLOW = 26 # The ESP32 pin GPIO26 connected to the traffic light module's yellow pin PIN_GREEN = 27 # The ESP32 pin GPIO27 connected to the traffic light module's green pin # Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms) RED_TIME = 2000 # RED time in milliseconds YELLOW_TIME = 1000 # YELLOW time in milliseconds GREEN_TIME = 2000 # GREEN time in milliseconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] # Initialize variables last_time = time.ticks_ms() light = RED # start with RED light def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Initialize the first light trafic_light_on(light) # Main loop while True: current_time = time.ticks_ms() if time.ticks_diff(current_time, last_time) > times[light]: light += 1 if light >= 3: light = RED # reset to RED for a new cycle trafic_light_on(light) last_time = current_time # TO DO: your other code

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!