Smart Traffic Lights: Adaptive Control with Arduino

Smart traffic light systems combine electronics and intelligent logic to optimize urban mobility. This project progresses from basic LED sequencing to sensor-enhanced adaptive control, demonstrating Arduino's capability to bridge simple prototypesOptimizing the IDE for Faster PrototypingOptimizing the IDE for Faster PrototypingDiscover effective strategies and settings to boost your Arduino IDE performance. Save time with faster build cycles and streamlined prototyping. and real-world automation. Ideal for makers advancing from beginner to intermediate skills!

Table of Contents🔗

Components Required🔗

ComponentQuantityPurpose
Arduino Uno1Control logic and signal generation
Red/Yellow/Green LEDs3 eachSimulate traffic light states
220Ω Resistors (220-330Ω acceptable)3Current limiting for LEDs
Breadboard1Circuit prototyping
Jumper Wires10+Connections
HC-SR04 Ultrasonic Sensor1Vehicle detection (optional)

Circuit Setup🔗

Basic LED WiringConnecting LCD DisplaysConnecting LCD DisplaysDiscover how to connect and program LCD displays with Arduino in this comprehensive guide. Learn wiring, coding, and troubleshooting for optimum performance.:

Ultrasonic SensorIntroduction to Sensors for ArduinoIntroduction to Sensors for ArduinoLearn the fundamentals of Arduino sensors, including setup, calibration, and coding examples—perfect for building interactive, smart projects with precision. (Optional):

Programming the Traffic Sequence🔗

Core Traffic Cycle (DelayYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily.-Based):

const int red = 9, yellow = 10, green = 11;
const unsigned long greenTime = 10000, yellowTime = 3000, redTime = 10000;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
}
void loop() {
  // Green phase
  digitalWrite(green, HIGH);
  delay(greenTime);
  digitalWrite(green, LOW);
  // Yellow phase
  digitalWrite(yellow, HIGH);
  delay(yellowTime);
  digitalWrite(yellow, LOW);
  // Red phase
  digitalWrite(red, HIGH);
  delay(redTime);
  digitalWrite(red, LOW);
}

Key Improvements for Scalability:

1. Modular FunctionsCreating Custom FunctionsCreating Custom FunctionsElevate your Arduino projects with custom functions. Our guide features practical examples, troubleshooting advice, and best practices for clear, modular code.:

void setLights(bool r, bool y, bool g) {
  digitalWrite(red, r);
  digitalWrite(yellow, y);
  digitalWrite(green, g);
}

2. Non-Blocking Timing (Millis):

Replace delaysYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily. to enable concurrent sensor polling:

unsigned long previousMillis = 0;
const long interval = 10000;
void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    // Update light states here
  }
}

Adding Vehicle Detection Sensors🔗

Ultrasonic IntegrationIntegrating Third-Party LibrariesIntegrating Third-Party LibrariesLearn to integrate third-party libraries into Arduino projects with our guide. Discover tips on selection, installation, coding, and troubleshooting. for Adaptive Control:

#include <NewPing.h>
#define TRIGGER_PIN 6
#define ECHO_PIN 7
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void loop() {
  int distance = sonar.ping_cm();
  if (distance < 50) { // Vehicle within 0.5m
    extendGreenPhase();
  } else {
    runStandardCycle();
  }
}
void extendGreenPhase() {
  setLights(LOW, LOW, HIGH);
  delay(15000); // Extended green duration
}

Testing and Calibration🔗

Comprehensive TroubleshootingYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily. Guide:

1. LEDYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily. Issues:

2. SensorIntroduction to Sensors for ArduinoIntroduction to Sensors for ArduinoLearn the fundamentals of Arduino sensors, including setup, calibration, and coding examples—perfect for building interactive, smart projects with precision. False Positives:

int getSmoothedDistance() {
  static int readings[5], index = 0;
  readings[index] = sonar.ping_cm();
  index = (index + 1) % 5;
  return (readings[0] + readings[1] + readings[2] + readings[3] + readings[4]) / 5;
}

3. Timing Accuracy:

CalibrationImplementing a Light SensorImplementing a Light SensorLearn how to set up and code an Arduino light sensor using an LDR, a voltage divider circuit, and reliable calibration techniques. Steps:

Advanced Enhancements🔗

1. Real-Time Clock (RTC):

Sync light cycles to rush hours using a DS3231 module.

2. Wireless ControlControlling a DC Motor with a Transistor and ArduinoControlling a DC Motor with a Transistor and ArduinoLearn how to safely control DC motors with Arduino using transistor circuits, code examples, and practical wiring diagrams for your robotics projects.:

Add Bluetooth (HC-05) or Wi-Fi (ESP8266Connecting Arduino to the InternetConnecting Arduino to the InternetDiscover how to connect your Arduino to the Internet with our complete guide covering hardware, protocols, coding tips, and troubleshooting for IoT projects.) for remote management:

if (Serial.available() > 0) {
  char cmd = Serial.read();
  if (cmd == 'E') activateEmergencyMode();
}

3. Pedestrian Crosswalk:

Integrate a push buttonConnecting Push Buttons to ArduinoConnecting Push Buttons to ArduinoLearn essential strategies for wiring, programming, and debouncing push buttons in Arduino projects using our comprehensive tutorial guide. to trigger red lights after a delay.

4. Failsafe Mode:

Flash all LEDs if sensorIntroduction to Sensors for ArduinoIntroduction to Sensors for ArduinoLearn the fundamentals of Arduino sensors, including setup, calibration, and coding examples—perfect for building interactive, smart projects with precision. data is inconsistent for >30s.

5. Solar Power:

Pair with a 6V solar panel and TP4056 charging module for off-grid use.

Real-World Applications🔗

1. Educational Kits:

Demonstrate logic gates by adding a 7-segment countdown display.

2. City Planning Models:

Network multiple ArduinosWhat is Arduino? A Comprehensive OverviewWhat is Arduino? A Comprehensive OverviewDive into the world of Arduino with our in-depth guide covering hardware, software, and community projects ideal for students, hobbyists, and educators. via I2C to simulate coordinated intersections.

3. Emergency Priority Systems:

Use IR sensorsBuilding a Line-Following RobotBuilding a Line-Following RobotExplore our comprehensive guide on line-following robots, featuring sensor integration, motor control, and PID programming to build advanced automation systems. or RF modules (nRF24L01) to detect ambulances and create green waves.

4. ML-Powered Optimization:

Train a TensorFlow Lite model to predict traffic patterns using historical data.

5. V2X IntegrationIntegrating Third-Party LibrariesIntegrating Third-Party LibrariesLearn to integrate third-party libraries into Arduino projects with our guide. Discover tips on selection, installation, coding, and troubleshooting.:

Implement vehicle-to-infrastructure communication using ESP32Connecting Arduino to the InternetConnecting Arduino to the InternetDiscover how to connect your Arduino to the Internet with our complete guide covering hardware, protocols, coding tips, and troubleshooting for IoT projects. and LoRa.

Next Steps:

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles