ESP32 C3 Super Mini - Traffic Light

This ESP32 C3 Super Mini traffic light tutorial shows you how to control a traffic light module with just four wires. You start with a simple red, yellow and green cycle. Then you make the code shorter and non-blocking, step by step.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Traffic Light

Hardware Preparation

1×ESP32 C3 Super Mini
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)
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

A traffic light module is an output module that puts red, yellow and green LEDs on one small board.

Key Features:

  • Three lights: red, yellow and green, each with its own control pin
  • Easy wiring: one GND pin and three signal pins
  • Beginner friendly: a good first project for learning digital outputs and timing

Technical Specifications:

  • Pins: GND, R, Y and G (4 pins)
  • Control signal: digital, driven by a GPIO pin of the ESP32 C3 Super Mini

Pinout

A traffic light module includes 4 pins:

  • GND pin: the ground pin; connect it to GND of the ESP32 C3 Super Mini
  • R pin: controls the red light; connect it to a digital output of the ESP32 C3 Super Mini
  • Y pin: controls the yellow light; connect it to a digital output of the ESP32 C3 Super Mini
  • G pin: controls the green light; connect it to a digital output of the ESP32 C3 Super Mini
Traffic Light Pinout

How It Works

Each light has its own pin, so your code controls each one on its own.

  • HIGH: set a pin to HIGH to turn its light on
  • LOW: set a pin to LOW to turn its light off
  • One at a time: turn one light on and the other two off, like a real traffic light
  • Timing: wait for a set time, then move to the next light

Wiring Diagram

Connect the three signal pins to three GPIO pins, and connect GND to GND.

  • Note: Unplug the USB cable while you change the wires.
  • Note: Check the GND wire first. A loose GND can make the lights act strangely.
The wiring diagram between ESP32 C3 Super Mini traffic light

This image is created using Fritzing. Click to enlarge image

Here is the same wiring without a breadboard:

The wiring diagram between ESP32 C3 Super Mini traffic light  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

Traffic Light Module Pin ESP32 C3 Super Mini Pin
GND GND
R GPIO7
Y GPIO6
G GPIO5

How To Program For Traffic Light Module

The program needs only two steps.

  • Set the pins as outputs: Configure the pins to digital output mode with the pinMode() function.
pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);
  • Turn on a light: Turn on the red light with the digitalWrite() function.
digitalWrite(PIN_RED, HIGH); // turn on RED digitalWrite(PIN_YELLOW, LOW); // digitalWrite(PIN_GREEN, LOW); delay(RED_TIME); // keep red led on during a period of time

ESP32 C3 Super Mini Code

This first sketch controls each light directly.

What This Code Does:

  • Sets GPIO7, GPIO6 and GPIO5 as outputs
  • Turns on the red light for 4 seconds
  • Turns on the yellow light for 4 seconds
  • Turns on the green light for 4 seconds, then starts again
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-traffic-light */ #define PIN_RED 7 // The ESP32 C3 SuperMini pin connected to R pin of traffic light module #define PIN_YELLOW 6 // The ESP32 C3 SuperMini pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The ESP32 C3 SuperMini pin connected to G pin of traffic light module #define RED_TIME 4000 // RED time in millisecond #define YELLOW_TIME 4000 // YELLOW time in millisecond #define GREEN_TIME 4000 // GREEN time in millisecond void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { // red light on digitalWrite(PIN_RED, HIGH); // turn on digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, LOW); // turn off delay(RED_TIME); // keep red light on during a period of time // yellow light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, HIGH); // turn on digitalWrite(PIN_GREEN, LOW); // turn off delay(YELLOW_TIME); // keep yellow light on during a period of time // green light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, HIGH); // turn on delay(GREEN_TIME); // keep green light on during a period of time }

Detailed Instructions

Follow these steps to run the code.

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire it: Connect the parts as shown in the wiring diagram above.
  • Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Type-C cable.
  • Open the IDE: Launch the Arduino IDE on your computer.
  • Pick the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Pick the port: Choose the COM port of your board.
  • Add the code: Copy the code above and paste it into the Arduino IDE.
  • Upload: Click the Upload button in the Arduino IDE.
  • Check the result: Watch the traffic light module change from red to yellow to green.
  • Serial Monitor: You do not need it here, because these sketches print nothing. If you add Serial code later, use a baud rate of 115200.
  • Pro Tip: Change RED_TIME, YELLOW_TIME and GREEN_TIME at the top of the code to try different timings.

Real traffic lights can work differently, depending on the design, the technology and the region. This project only shows the general idea of how traffic lights manage traffic and keep roads safe.

The code above controls each light directly. Now, let's make the code better.

ESP32 C3 Super Mini Code Optimization

Each version below does the same job with cleaner code. These versions use 2 seconds for red, 1 second for yellow and 2 seconds for green.

  • Use a function: A function called trafic_light_on() turns on one light and turns off the other two.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-traffic-light */ #define PIN_RED 7 // The ESP32 C3 SuperMini pin connected to R pin of traffic light module #define PIN_YELLOW 6 // The ESP32 C3 SuperMini pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The ESP32 C3 SuperMini pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { // red light on trafic_light_on(RED); delay(times[RED]); // keep red light on during a period of time // yellow light on trafic_light_on(YELLOW); delay(times[YELLOW]); // keep yellow light on during a period of time // green light on trafic_light_on(GREEN); delay(times[GREEN]); // keep green light on during a period of time } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Use a for loop: A for loop goes through the three lights, so loop() becomes much shorter.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-traffic-light */ #define PIN_RED 7 // The ESP32 C3 SuperMini pin connected to R pin of traffic light module #define PIN_YELLOW 6 // The ESP32 C3 SuperMini pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The ESP32 C3 SuperMini pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN}; const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME}; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // The loop function repeats indefinitely void loop() { for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light); delay(times[light]); // keep light on during a period of time } } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i ++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Use millis() instead of delay(): The board does not stop and wait, so it can do other work while the lights run.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-traffic-light */ #define PIN_RED 7 // The ESP32 C3 SuperMini pin connected to R pin of traffic light module #define PIN_YELLOW 6 // The ESP32 C3 SuperMini pin connected to Y pin of traffic light module #define PIN_GREEN 5 // The ESP32 C3 SuperMini pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; unsigned long last_time = 0; int light = RED; // start with RED light void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis(); } // The loop function repeats indefinitely void loop() { if ((millis() - last_time) > times[light]) { light++; if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); } // TO DO: your other code } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }

Application Ideas

An ESP32 C3 Super Mini traffic light can do more than copy a road signal.

  • Intersection Model: Build a small road crossing with your own light timings.
  • Machine Status Light: Show idle with green, warning with yellow and fault with red.
  • Pedestrian Crossing: Add a button so people can ask for a red light.
  • Noise Level Warning: Show quiet, loud and too loud in a classroom.
  • Teaching Tool: Show students how timing and non-blocking code work.

Challenge Yourself

Try these ideas to build on your traffic signal project.

  • Easy: Keep the yellow light on for 3 seconds and the green light on for 8 seconds.
  • Medium: Add a button that switches to the red light right away.
  • Medium: Print the name of the current light to the Serial Monitor at 115200 baud.
  • Advanced: Add a sensor reading to the millis() version while the lights keep running.

※ 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!