DEV Community

Cover image for ESP32 C3 Super Mini - Traffic Light
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP32 C3 Super Mini - Traffic Light

This build walks you through wiring a traffic light module to an ESP32 C3 Super Mini and cycling red, yellow and green with code. It's a good first project for digital outputs and timing, and a nice way to practice moving from blocking delay() code to a non-blocking millis() version that can do other work while the lights run.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP32 C3 Super Mini - Traffic Light

What you need

Wiring

ESP32 C3 Super Mini - Traffic Light wiring diagram

The traffic light module has four pins: GND, R, Y and G. Connect GND to GND on the ESP32 C3 Super Mini, then wire the three signal pins to three GPIOs, GPIO7 for red, GPIO6 for yellow and GPIO5 for green. Unplug the USB cable while you change wires. If the lights behave strangely, check the GND connection first since a loose ground is the most common cause.

How it works

Each light on the module has its own control pin, so your code switches each one independently. Setting a pin HIGH turns its light on, LOW turns it off. A real traffic light only shows one color at a time, so the code turns one pin on while keeping the other two low, waits a set time, then moves to the next light.

Code

The first sketch sets GPIO7, GPIO6 and GPIO5 as outputs with pinMode(), then turns on red for 4 seconds, yellow for 4 seconds and green for 4 seconds using digitalWrite() and delay(), repeating forever. Later versions clean this up: a trafic_light_on() function turns on one light and off the other two, a for loop shortens loop(), and a millis()-based version removes delay() entirely so the board can do other work while the lights keep cycling.

esp32-c3-super-mini-traffic-light.ino

/*
 * 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
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

Always unplug USB before touching the wiring. If the lights flicker or behave oddly, the GND wire is the first thing to check, a loose ground is the usual culprit. Make sure the correct board and COM port are selected in the Arduino IDE before uploading.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-traffic-light

Top comments (0)