Two-Board ESP32 Traffic Lights — ESP32 simulation

Two ESP32 controllers drive four South African traffic lights (12 WS2812 rings) over UART: Controller 1 owns the six-phase red-green-amber sequence for North/South, Controller 2 receives and executes commands for East/West.

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by ndingibr.

Components

Sketch code

#include <Arduino.h>

// =====================================
// SOUTH AFRICAN FOUR WAY TRAFFIC INTERSECTION
// BOSS - Controller 1  (NORTH + SOUTH)
//
// Plain LEDs, each lamp on its OWN GPIO - no parallel rings, so every
// lamp updates independently.
//
//   NORTH: red GPIO 25, amber GPIO 26, green GPIO 27
//   SOUTH: red GPIO 32, amber GPIO 33, green GPIO 14
//
// Sequence: red -> green -> amber -> red with an all-red gap between
// directions. There is NO red+amber state.
// =====================================

// =====================================
// 1. CONFIGURATION
// =====================================

const int BAUD_RATE     = 115200;
const int UART_RX       = 16;
const int UART_TX       = 17;
const int SEND_INTERVAL = 250;

const int N_RED   = 25;
const int N_AMBER = 26;
const int N_GREEN = 27;

const int S_RED   = 32;
const int S_AMBER = 33;
const int S_GREEN = 14;

// =====================================
// 2. SIGNAL MAPPING
// =====================================

enum class Signal : uint8_t {
  RED   = 0,
  AMBER = 1,
  GREEN = 2
};

const char* signalName(Signal signal) {
  switch (signal) {
    case Signal::RED:   return "RED";
    case Signal::AMBER: return "AMBER";
    case Signal::GREEN: return "GREEN";
  }
  return "?";
}

// =====================================
// 3. SEQUENCE (six phases)
// =====================================

struct Phase {
  Signal local;
  Signal remote;
  unsigned long duration;
};

constexpr Phase SEQUENCE[] = {
  {Signal::GREEN, Signal::RED,  16000},   // phase 1 - N/S go
  {Signal::AMBER, Signal::RED,   4000},   // phase 2 - N/S clear
  {Signal::RED,   Signal::RED,   4000},   // phase 3 - all red gap
  {Signal::RED,   Signal::GREEN,16000},   // phase 4 - E/W go
  {Signal::RED,   Signal::AMBER, 4000},   // phase 5 - E/W clear
  {Signal::RED,   Signal::RED,   4000}    // phase 6 - all red gap
};

constexpr int PHASE_COUNT = sizeof(SEQUENCE) / sizeof(SEQUENCE[0]);

int currentPhase = 0;
unsigned long lastChange = 0;
unsigned long lastSend   = 0;

// =====================================
// 4. LAMP CONTROL
// =====================================

// Drive one three-lamp head: exactly one lamp per signal state.
void showHead(Signal signal, int pinRed, int pinAmber, int pinGreen) {
  digitalWrite(pinRed,   signal == Signal::RED   ? HIGH : LOW);
  digitalWrite(pinAmber, signal == Signal::AMBER ? HIGH : LOW);
  digitalWrite(pinGreen, signal == Signal::GREEN ? HIGH : LOW);
}

void showSignal(Signal signal) {
  showHead(signal, N_RED, N_AMBER, N_GREEN);
  showHead(signal, S_RED, S_AMBER, S_GREEN);
}

// =====================================
// 5. COMMUNICATION
// =====================================

void sendSignal(Signal signal) {
  char message[2];
  message[0] = '0' + static_cast<uint8_t>(signal);
  message[1] = '\0';
  Serial2.println(message);
}

// =====================================
// 6. CONTROLLER
// =====================================

void applyPhase(int phaseIndex) {

  const P

More projects by ndingibr