Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino and ESP32-CAM Based Smart Traffic Light System with Car Detection

View and Edit Project In Cirkit DesignerImage of Arduino and ESP32-CAM Based Smart Traffic Light System with Car Detection

Circuit Documentation

Summary

This circuit is designed to manage a traffic light system using an Arduino UNO and multiple ESP32-CAM modules. The Arduino UNO controls four traffic lights, each with red, yellow, and green LEDs. The ESP32-CAM modules are used to monitor car counts in four different lanes. Based on the car counts, the Arduino UNO determines which lane should have the green light to optimize traffic flow.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. ESP32 CAM

    • Description: A low-cost microcontroller with integrated Wi-Fi and Bluetooth, used for image processing and streaming.
    • Pins: 5V, GND, GPIO12, GPIO13, GPIO15, GPIO14, GPIO2, GPIO4, 3.3V, GPIO16, GPIO0, 3.3V / 5V / P_OUT, GPIO3 / RX, GPIO1 / TX
  3. ESP32 - CAM

    • Description: Similar to the ESP32 CAM, used for image processing and streaming.
    • Pins: 5V, GND, IO12, IO13, IO15, IO14, IO2, IO4, VOT, VOR, VCC, IO0, IO16, 3V3
  4. Traffic Light

    • Description: A module representing a traffic light with red, yellow, and green LEDs.
    • Pins: Green, Yellow, Red, GND

Wiring Details

Arduino UNO

ESP32 CAM

ESP32 - CAM (multiple instances)

Traffic Light (multiple instances)

Code Documentation

#include <SoftwareSerial.h>

// Define SoftwareSerial for each ESP32-CAM
SoftwareSerial esp1(0, 1); // RX, TX for ESP32-CAM #1
SoftwareSerial esp2(2, 3); // RX, TX for ESP32-CAM #2
SoftwareSerial esp3(4, 5); // RX, TX for ESP32-CAM #3
SoftwareSerial esp4(6, 7); // RX, TX for ESP32-CAM #4

int carCounts[4] = {0, 0, 0, 0}; // Stores car counts for each lane
int selectedLane = -1;           // Lane with the most cars

// Define traffic light pins
const int trafficLights[4][3] = {
  {8, 9, 10}, // Lane 1: Red, Yellow, Green
  {11, 12, 13}, // Lane 2: Red, Yellow, Green
  {A0, A1, A2}, // Lane 3: Red, Yellow, Green
  {A3, A4, A5}  // Lane 4: Red, Yellow, Green
};

void setup() {
  Serial.begin(9600); // For debugging
  esp1.begin(9600);   // Initialize SoftwareSerial for ESP32-CAM #1
  esp2.begin(9600);   // Initialize SoftwareSerial for ESP32-CAM #2
  esp3.begin(9600);   // Initialize SoftwareSerial for ESP32-CAM #3
  esp4.begin(9600);   // Initialize SoftwareSerial for ESP32-CAM #4

  // Setup traffic light pins
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 3; j++) {
      pinMode(trafficLights[i][j], OUTPUT);
      digitalWrite(trafficLights[i][j], LOW); // Turn off all LEDs
    }
  }
}

void loop() {
  // Step 1: Read car counts from each ESP32-CAM
  if (esp1.available()) carCounts[0] = esp1.readStringUntil('\n').toInt();
  if (esp2.available()) carCounts[1] = esp2.readStringUntil('\n').toInt();
  if (esp3.available()) carCounts[2] = esp3.readStringUntil('\n').toInt();
  if (esp4.available()) carCounts[3] = esp4.readStringUntil('\n').toInt();

  // Step 2: Determine the lane with the most cars
  int maxCars = -1;
  for (int i = 0; i < 4; i++) {
    if (carCounts[i] > maxCars) {
      maxCars = carCounts[i];
      selectedLane = i;
    }
  }

  // Step 3: Control traffic lights based on the selected lane
  controlTrafficLights(selectedLane);

  delay(1000); // Update every second
}

void controlTrafficLights(int lane) {
  // Turn off all traffic lights first
  for (int i = 0; i < 4; i++) {
    digitalWrite(trafficLights[i][0], HIGH); // Red light ON
    digitalWrite(trafficLights[i][1], LOW); // Yellow light OFF
    digitalWrite(trafficLights[i][2], LOW); // Green light OFF
  }

  // Activate the green light for the selected lane
  digitalWrite(trafficLights[lane][0], LOW); // Red light OFF
  digitalWrite(trafficLights[lane][2], HIGH); // Green light ON

  delay