ESP32 S3 - Traffic Light
This tutorial shows you how to use the ESP32 S3 to control a traffic light module. You will learn how to wire the module, program the ESP32 S3 to cycle through traffic light states, and then optimize the code using functions and the non-blocking millis() approach.
What you'll build:
- Wire a traffic light module to the ESP32 S3 board
- Program the ESP32 S3 to cycle red, yellow, and green lights in sequence
- Refactor the code using a dedicated control function for cleaner logic
- Implement a non-blocking version using millis() instead of delay()

Hardware Preparation
| 1 | × | ESP32 S3 WROOM N16R8 | |
| 1 | × | USB Cable Type-A to Type-C (for USB-A PC) | |
| 1 | × | USB Cable Type-C to Type-C (for USB-C PC) | |
| 1 | × | Traffic Light Module | |
| 1 | × | Jumper Wires |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Traffic Light Module
A traffic light module integrates three colored LEDs — red, yellow, and green — into a single compact package that is easy to wire and program. It is commonly used in embedded projects to simulate intersection signals, teach sequencing logic, and demonstrate GPIO output control on microcontrollers like the ESP32 S3. The module handles current limiting internally, so no external resistors are required.
Key Specifications
- Operating voltage: 3.3V–5V (compatible with ESP32 S3 GPIO output)
- Pins: GND, R (red), Y (yellow), G (green)
- Built-in current-limiting resistors for each LED
Pinout
A traffic light module includes 4 pins:
- GND pin: The ground pin, connect this pin to GND of ESP32 S3.
- R pin: The pin to control the red light, connect this pin to a digital output of ESP32 S3.
- Y pin: The pin to control the yellow light, connect this pin to a digital output of ESP32 S3.
- G pin: The pin to control the green light, connect this pin to a digital output of ESP32 S3.

Wiring Diagram
Connect the traffic light module to the ESP32 S3 by linking each signal pin (R, Y, G) to a separate GPIO output pin and the GND pin to any ground on the board. Use jumper wires to make a clean and reliable connection between the module and the ESP32 S3 headers.
Safety Notes
The traffic light module includes built-in current-limiting resistors, so no additional resistors are needed between the module and the ESP32 S3 GPIO pins. Keep the wiring tidy and double-check GND connectivity before powering the board to avoid erratic LED behavior.

This image is created using Fritzing. Click to enlarge image
| GPIO Pin | Traffic Light Module |
|---|---|
| GPIO 40 | R (Red) |
| GPIO 41 | Y (Yellow) |
| GPIO 42 | G (Green) |
| GND | GND |
How To Program For Traffic Light Module
- Configure the ESP32 S3's pins to digital output mode by using the pinMode() function
- Program to turn ON red light by using digitalWrite() function:
ESP32 S3 Code
The following code demonstrates how to control the traffic light module with the ESP32 S3 using straightforward digitalWrite() calls and delay() for timing. Each stage of the traffic cycle — red, yellow, and green — is activated in turn by setting the appropriate pin HIGH while holding the others LOW. Upload this code to your ESP32 S3 board to see the traffic light sequence run immediately after boot.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Do the wiring as shown in the diagram above.
- Connect the ESP32 S3 board to your PC via a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the right ESP32 S3 board (e.g. ESP32S3 Dev Module) and COM port.
- Copy the above code and paste it into Arduino IDE.
- Click Upload button on Arduino IDE to upload code to ESP32 S3.
- Observe the traffic light module cycling through red, yellow, and green.
- Pro Tip: Adjust the RED_TIME, YELLOW_TIME, and GREEN_TIME constants at the top of the sketch to simulate different intersection timings used in your region.
It's important to note that the exact workings of a traffic light can vary depending on the specific design and technology used in different regions and intersections. The principles described above provide a general understanding of how traffic lights operate to manage traffic and enhance safety on the roads.
The code above demonstrates individual light control. Now, let's enhance the code for better optimization.
ESP32 S3 Code Optimization
- Let's improve the code by implementing a function for light control.
- Let's improve the code by using a for loop.
- Let's improve the code by using millis() function instead of delay().
Serial Monitor Output
Open the Serial Monitor at 9600 baud to observe runtime messages from the ESP32 S3. The following are example readings you might see during operation:
Applications
Controlling a traffic light module with the ESP32 S3 teaches sequencing and GPIO output management skills that apply directly to many practical embedded systems. Here are some real-world use cases:
- Intersection Simulation: Recreate a real traffic intersection controller with configurable timing for each light phase.
- Industrial Status Panel: Use the three colors to represent machine states such as idle (green), warning (yellow), and fault (red).
- Pedestrian Crossing Signal: Add a button input to the ESP32 S3 to let a pedestrian request a green light on demand.
- Production Line Indicator: Signal workflow stages on an assembly line using the red, yellow, and green outputs as process state indicators.
- Educational Demo: Demonstrate state machine concepts and non-blocking timing techniques to students learning embedded programming.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenges
Practicing variations of this project will deepen your understanding of GPIO sequencing and timing control on the ESP32 S3. Try the following exercises to extend your skills:
- Beginner: Change the timing constants so the yellow light stays on for 3 seconds and the green light stays on for 8 seconds, then re-upload and observe the difference.
- Intermediate: Add a pushbutton connected to the ESP32 S3 that, when pressed, immediately switches the light to red and holds it for 10 seconds before resuming the normal cycle.
- Advanced: Rewrite the traffic light controller using a finite state machine (FSM) with millis() so the sequence is fully non-blocking, allowing the ESP32 S3 to handle other tasks — such as reading a sensor — while the lights cycle independently.