Traffic Signal Using Arduino and Ultrasonic Sensor

Traffic Signal Using Arduino and Ultrasonic Sensor

Introduction

With the era of smart technologies, everything is getting smarter and smart transport system is one of the fields which is going to put a huge impact on our lives. Traffic Signal Using Arduino and Ultrasonic Sensor is one of the example.

Arduino being one of the easiest microcontrollers to use, easy to program, easily available in local markets is popular among the students and hobbyists.

Knowing all that, I put my knowledge to make this Density-based traffic signal using Arduino with all the components that are easily available.

This project is a prototype of density based controlling of traffic lights which will check the densities on both ways and will decide which light should be on.

Lets get started.

Arduino MFRC522 tutorial – Is RFID tag present or removed?

[siteorigin_widget class=”WP_Widget_Media_Video”][/siteorigin_widget]

Video Tutorial

Hardware Required

  • ARDUINO UNO
  • HC-SR04
  • JUMPER WIRES
  • GREEN LEDs
  • RED LED
  • YELLOW LEDs

Circuit

we will be using two ultrasonic sensors for two ways and 6 LEDs, 3 for each side.

Ultrasonic Sensor 1:

  • trigger >>>>> Arduino pin D10
  • Echo >>>>>> Arduino pin D9
  • GND >>>>>> GND
  • VCC >>>>>> 5V

Ultrasonic Sensor 2:

  • trigger >>>>> Arduino pin D12
  • Echo >>>>>> Arduino pin D11
  • GND>>>>>>> GND
  • VCC>>>>>>> 5V

LEDs:

  • All the cathodes of the LEDs must go to GND and all of the GND must be common.
  • Red1 Anode>>>>>>Arduino D8
  • Yellow1 Anode>>>>>Arduino D7
  • Green1 Anode>>>>>Arduino D6
  • Green2 Anode>>>>>Arduino D5
  • Yellow2 Anode >>>>Arduino D4
  • Red2 Anode >>>>>Arduino D3
Traffic Signal Using Arduino and Ultrasonic Sensor
Traffic Signal Using Arduino and Ultrasonic Sensor

code

int red1=8;
int green1=6;
int yellow1=7;
int red2=3;
int green2=5;
int yellow2=4;
const int pingPin = 10; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 9; // Echo Pin of Ultrasonic Sensor
const int pingPin2 = 12; // Trigger Pin of Ultrasonic Sensor
const int echoPin2 = 11; // Echo Pin of Ultrasonic Sensor

void setup() {
  // put your setup code here, to run once:
pinMode(red1,OUTPUT);
pinMode(red2,OUTPUT);
pinMode(yellow1,OUTPUT);
pinMode(yellow2,OUTPUT);
pinMode(green1,OUTPUT); 
pinMode(green2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int distance1,distance2;
  distance1=calculatedistance(pingPin , echoPin);
  distance2=calculatedistance(pingPin2 , echoPin2);
  if(distance1>=distance2){
     digitalWrite(red1,LOW);
digitalWrite(green2,LOW);
digitalWrite(red2,LOW);
digitalWrite(green1,LOW);
digitalWrite(yellow1,HIGH);
digitalWrite(yellow2,HIGH);
delay(200);
    while(distance1>distance2){
   distance1=calculatedistance(pingPin , echoPin);
  distance2=calculatedistance(pingPin2 , echoPin2);
digitalWrite(red1,HIGH);
digitalWrite(green2,HIGH);
digitalWrite(red2,LOW);
digitalWrite(green1,LOW);
digitalWrite(yellow1,LOW);
digitalWrite(yellow2,LOW);
    }

  }



if(distance2>distance1){
    digitalWrite(red1,LOW);
digitalWrite(green2,LOW);
digitalWrite(red2,LOW);
digitalWrite(green1,LOW);
digitalWrite(yellow1,HIGH);
digitalWrite(yellow2,HIGH);
delay(200);
  while(distance2>distance1){
      distance1=calculatedistance(pingPin , echoPin);
  distance2=calculatedistance(pingPin2 , echoPin2);
digitalWrite(red1,LOW);
digitalWrite(green2,LOW);
digitalWrite(red2,HIGH);
digitalWrite(green1,HIGH);
digitalWrite(yellow1,LOW);
digitalWrite(yellow2,LOW);
  }


}

}


long microsecondsToCentimeters(long microseconds)
{
   return microseconds / 29 / 2;
}

int calculatedistance(int pingPin , int echoPin){
    long duration, inches, cm,meter;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  cm = microsecondsToCentimeters(duration);
  meter = cm/100;
  return meter;
}

Upload code and you are good to go .

Check video if you find any trouble.

4-way traffic Lights using Arduino Code

[siteorigin_widget class=”A2A_Follow_Widget”][/siteorigin_widget]

1 thought on “Traffic Signal Using Arduino and Ultrasonic Sensor”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.