Skip to content

3.12. Smart City Traffic Monitoring System

Description In this project, learners will build a Smart City Traffic Monitoring System using Ultrasonic Sensors, LEDs, a Buzzer, and an LCD Display. The system monitors traffic congestion levels and warns operators when congestion becomes excessive.
Use case This project is connected to real-world uses such as: This project demonstrates how smart-city systems monitor and manage traffic congestion to improve transportation efficiency.

Components (Things You will need)

Arduino Uno Arduino USB Cable Bread Board Jumper Wires LCD Display Resistor LED Buzzer Ultrasonic Sensor Traffic Light Module

Building the circuit

Things Needed:

  • Arduino Uno
  • Ultrasonic Sensor
  • LCD Display
  • Green LED
  • Yellow LED
  • Red LED
  • Buzzer
  • Breadboard
  • Jumper Wires
  • Arduino USB Cable
  • Resistor
  • Traffic Light Module

Mounting the component on the breadboard and wiring the circuit

Step 1: Connect the Arduino 5V pin to the breadboard’s red positive (+) power rail.

Step 2: Connect the Arduino GND pin to the breadboard’s blue negative (−) ground rail.

Step 3: Insert the Ultrasonic Sensor into the breadboard.

Step 4: Connect the Ultrasonic Sensor’s VCC pin to the 5V power rail.

Step 5: Connect the Ultrasonic Sensor’s GND pin to the ground rail.

Step 6: Connect the Ultrasonic Sensor’s Trig pin to Arduino digital pin 9.

Step 7: Connect the Ultrasonic Sensor’s Echo pin to Arduino digital pin 10.

Step 8: Connect the I2C LCD Display’s VCC to the 5V power rail.

Step 9: Connect the I2C LCD Display’s GND to the ground rail.

Step 10: Connect the I2C LCD Display’s SDA to Arduino analog pin A4.

Step 11: Connect the I2C LCD Display’s SCL to Arduino analog pin A5.

Step 12: Insert the Green LED into the breadboard and connect its anode (longer leg) to Arduino digital pin 5.

Step 13: Connect a 220 Ω resistor from the Green LED’s cathode (shorter leg) to the ground rail.

Step 14: Insert the Yellow LED into the breadboard and connect its anode (longer leg) to Arduino digital pin 6.

Step 15: Connect a 220 Ω resistor from the Yellow LED’s cathode (shorter leg) to the ground rail.

Step 16: Insert the Red LED into the breadboard and connect its anode (longer leg) to Arduino digital pin 7.

Step 17: Connect a 220 Ω resistor from the Red LED’s cathode (shorter leg) to the ground rail.

Step 18: Insert the Buzzer into the breadboard.

Step 19: Connect the Buzzer’s positive (+) pin to Arduino digital pin 8.

Step 20: Connect the Buzzer’s negative (−) pin to the ground rail.

Step 21: Carefully check all connections to ensure the Ultrasonic Sensor, LCD, LEDs, resistors, and Buzzer are connected correctly before powering the Arduino.

Components on breadboard

Make sure to connect the Arduino USB cable to the Arduino board.

PROGRAMMING

Step 1: Open your Arduino IDE. See how to set up here: Getting Started.

Step 2: Write the complete program implementing the system logic with appropriate pin definitions, setup configuration, and the main control loop.


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Pin definitions
const int trigPin = 9;
const int echoPin = 10;

const int greenLED = 5;
const int yellowLED = 6;
const int redLED = 7;

const int buzzerPin = 8;

void setup() {

  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Configure pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(greenLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(redLED, OUTPUT);

  pinMode(buzzerPin, OUTPUT);

  // Ensure outputs are OFF initially
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(buzzerPin, LOW);

  // Startup message
  lcd.setCursor(0, 0);
  lcd.print("Traffic System");
  lcd.setCursor(0, 1);
  lcd.print("Starting...");
  delay(1500);
  lcd.clear();
}

void loop() {

  // Send ultrasonic pulse
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  // Measure echo duration
  long duration = pulseIn(echoPin, HIGH);

  // No object detected
  if (duration == 0) {

    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, LOW);
    digitalWrite(buzzerPin, LOW);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("No Object");
    lcd.setCursor(0, 1);
    lcd.print("Detected");
    delay(300);

    return;
  }

  // Calculate distance (cm)
  int distance = duration * 0.034 / 2;

  // Display distance
  lcd.setCursor(0, 0);
  lcd.print("Traffic:");
  lcd.print(distance);
  lcd.print("cm   ");

  // Traffic conditions
  if (distance > 30) {

    digitalWrite(greenLED, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, LOW);
    digitalWrite(buzzerPin, LOW);

    lcd.setCursor(0, 1);
    lcd.print("Free Flow      ");

  }
  else if (distance > 15) {

    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(redLED, LOW);
    digitalWrite(buzzerPin, LOW);

    lcd.setCursor(0, 1);
    lcd.print("Moderate       ");

  }
  else {

    digitalWrite(greenLED, LOW);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, HIGH);
    digitalWrite(buzzerPin, HIGH);

    lcd.setCursor(0, 1);
    lcd.print("Congestion!    ");

  }

  delay(300);
}

Step 7: Save your code. See the Getting Started section

Step 8: Select the arduino board and port _See the Getting Started.

Step 9: Upload your code.

CONCLUSION

When traffic is light, the LCD displays the measured distance (for example, Traffic: 40 cm) and the message "Free Flow." The green LED turns on to indicate normal traffic conditions, while the yellow LED, red LED, and buzzer remain off.

When traffic becomes heavy, the LCD displays the measured distance (for example, Traffic: 8 cm) and the message "Congestion." The red LED turns on and the buzzer sounds to alert users that traffic congestion has been detected.