Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Daniel Ramsgard
Published

Arduino Smart Traffic Light

Smart Traffic upgrades the usual everyday traffic light to something modern, smart, and exceedingly useful, simply and cheaply!

BeginnerFull instructions provided1 hour1,532
Arduino Smart Traffic Light

Things used in this project

Hardware components

LED (generic)
LED (generic)
×3
Resistor 100 ohm
Resistor 100 ohm
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Arduino UNO
Arduino UNO
×1

Story

Read more

Schematics

Circuit Schematic

Code

Smart Traffic

Arduino
//Code produced by Daniel Ramsgard
//Initialize pins and global variables
int trig = 10;
int echo = 3;
int timeInMicro;
int distanceInCm;

void setup() {
  //Set serial-port baud rate to 9600 and pinmodes for HC-SR04
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
}

void loop() {
   //Not needed for program but useful for testing displacement
   //Prints displacement to serial port
   Serial.println(ultrasonicDisplacement(trig, echo));
   //Code for red light: light set to on for ten seconds unless displacement function returns <50
   //Then green light goes on
   for (int i = 0; i < 10; i++)
   {
     digitalWrite(8, HIGH);
     delay(1000);
      if (ultrasonicDisplacement(trig, echo) < 50)
      {
        digitalWrite(8, LOW);
        digitalWrite(13, HIGH);
        delay(5000);
        digitalWrite(13, LOW);
      }
      if (i == 9)
      {
        digitalWrite(8, LOW);
      }
    }
    //Code for yellow light: light set to on for seven seconds unless displacement function returns <50
    //Then green light goes on
    for (int j = 0; j < 7; j++)
    {
     digitalWrite(4, HIGH);
     delay(1000);
      if (ultrasonicDisplacement(trig, echo) < 50)
      {
        digitalWrite(4, LOW);
        digitalWrite(13, HIGH);
        delay(5000);
        digitalWrite(13, LOW);
      }
      if (j == 6)
      {
        digitalWrite(4, LOW);
      }
    }
    //Code for green light: light set to on for ten seconds
    for (int f = 0; f < 10; f++)
    {
      digitalWrite(13, HIGH);
      delay(1000);
      if (f == 9)
      {
        digitalWrite(13, LOW);
      }
    }
 }
//Code for function used for displacement
int ultrasonicDisplacement(int trig, int echo){
 //Code for HC-SR04
 int timeInMicro;
 int distanceInCm;
 digitalWrite(trig, LOW);
 delayMicroseconds(2);
 digitalWrite(trig, HIGH);
 delayMicroseconds(10);
 digitalWrite(trig, LOW);
 //Variables
 timeInMicro = pulseIn(echo, HIGH);
 distanceInCm = ((timeInMicro/29)/2);
 delay(1000);
 //Function's return value
 return distanceInCm;
}

Credits

Daniel Ramsgard
18 projects • 15 followers
I'm a freshman at UNC-Chapel Hill from NY & love Arduino, electronics, PCB design, and smart devices. www.github.com/DanielRamsgard

Comments