Smart Traffic Light System

Smart Traffic Light System

Introduction

Building a smart traffic light system is an exciting project that combines hardware and software to create a functional and customizable traffic control solution. Using an ESP8266 or ESP32 microcontroller, this system manages traffic lights efficiently and offers opportunities for enhancement, such as adding web-based monitoring and control. Designed for enthusiasts with an intermediate level of experience, this guide will walk you through the steps needed to bring your project to life, from wiring the circuit to programming the microcontroller.

Components Required

Note: Ensure you select resistors with appropriate resistance to prevent damage to the LEDs.

Wiring the Circuit

Follow these steps to connect your components:

Tip: Double-check your wiring to ensure proper connections and avoid short circuits.

Programming the Microcontroller

Use the following code to control the traffic light system:

#include <ESP8266WiFi.h> // or <WiFi.h> for ESP32

// Define LED pins
const int redPin = D1;
const int yellowPin = D2;
const int greenPin = D3;

void setup() {
    pinMode(redPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
}

void loop() {
    // Red light for 5 seconds
    digitalWrite(redPin, HIGH);
    delay(5000);
    digitalWrite(redPin, LOW);

    // Green light for 5 seconds
    digitalWrite(greenPin, HIGH);
    delay(5000);
    digitalWrite(greenPin, LOW);

    // Yellow light for 2 seconds
    digitalWrite(yellowPin, HIGH);
    delay(2000);
    digitalWrite(yellowPin, LOW);
}
        

Pro Tip: Test each LED separately before running the complete code to ensure they light up as expected.

Enhancing with Web Access

You can extend this project by adding a web interface to control the traffic lights. The following code provides a basic example of integrating a web server:

#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
AsyncWebServer server(80);

// Define LED pins
const int redPin = D1;
const int yellowPin = D2;
const int greenPin = D3;

void setup() {
    pinMode(redPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    pinMode(greenPin, OUTPUT);

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
    }

    server.on("/red", HTTP_GET, [](AsyncWebServerRequest *request){
        digitalWrite(redPin, HIGH);
        digitalWrite(yellowPin, LOW);
        digitalWrite(greenPin, LOW);
        request->send(200, "text/plain", "Red light ON");
    });

    server.on("/green", HTTP_GET, [](AsyncWebServerRequest *request){
        digitalWrite(redPin, LOW);
        digitalWrite(yellowPin, LOW);
        digitalWrite(greenPin, HIGH);
        request->send(200, "text/plain", "Green light ON");
    });

    server.on("/yellow", HTTP_GET, [](AsyncWebServerRequest *request){
        digitalWrite(redPin, LOW);
        digitalWrite(yellowPin, HIGH);
        digitalWrite(greenPin, LOW);
        request->send(200, "text/plain", "Yellow light ON");
    });

    server.begin();
}

void loop() {
    // Normal operation, can also implement automatic cycling
}
        

Important: Replace your_SSID and your_PASSWORD with your Wi-Fi credentials. Access the web interface using the ESP8266/ESP32's local IP address in your browser.

Future Expansion Ideas

These enhancements can make your traffic light system smarter and more efficient.

Conclusion

Your smart traffic light system is now ready! You can access it through the ESP8266/ESP32 IP address in a web browser. This project can be further developed with features like real-time monitoring, timers, or integrating with sensors for vehicle detection.

Keep exploring and enhancing your microcontroller projects!