Enhanced Traffic Light W/ Arduino

by TJ Jean Augustine in Circuits > Arduino

269 Views, 0 Favorites, 0 Comments

Enhanced Traffic Light W/ Arduino

download.png
Screenshot (117).png
Screenshot (120).png
Screenshot (118).png
Screenshot (115).png
Screenshot (119).png
Screenshot (122).png
Screenshot (121).png
Screenshot (123).png
Screenshot (116).png

Hello, If you are at this instructable that means you want to know how to make an Enhanced Traffic Light. If you are wondering about how this would be different from a regular traffic light I'll tell you. The addition of a motion sensor and a distance sensor are what enhance this traffic light. This entire project is done in tinkercad which is a website that can be used for simulations of circuits.

For people who would like to get straight into the tutorial skip the following text. However, for people who want to learn about how I stumbled upon this idea and how I researched it, continue reading the following text.

When thinking I first began thinking of an end-of-year task for my computer engineering class my mind was completely blank, I genuinely had no idea of what I wanted to make. I began thinking of previous projects that I had created within the last few years, a traffic light was one of them. It was very basic and I wouldn't be able to re-create it this year. However, I really liked the idea of a traffic light and wanted to create one, so I decided to add a bunch of extra parts to it to create an enhanced version of my old project.

Regarding the research aspect, I needed to figure out how to incorporate many new components into my circuit, I began thinking about what each new component could bring to the circuit and how they would work with each other. I immediately thought of using the motion sensor, the distance sensor together and the buzzer together as they could act as an alarm system when used together. Any other research that I had done was purely on how to code the 2 new sensors that I had added.

As for the material list you will need the following components to create this project.

Supplies

  • Arduino Uno R3 x 1
  • RGB LED x 1
  • LDR or Photoresistor x 1
  • White LED x 1
  • 200 ohms resistor x 3
  • 4-pin ultrasonic distance sensor x 1
  • PIR motion sensor x 1
  • buzzer x 1
  • large breadboard x 1
  • pushbutton x 1

Initial Assembly

Screenshot (129).png

Place all components onto the breadboard, the picture attached is one of the many ways that the components could be set up, make sure to spread out all the components as to not run into any wiring issues or issues involving space around the components. Remember to add the resistors to the RGB legs of the RGB LED, and to connect the Common Cathode to ground or the Common Anode to power.

Wiring

Screenshot (127).png
Screenshot (126).png

When wiring you could set up and wire the components however you would like, but if you are following my specific component placements then these are the pins that each component is connected to (look to image for visuals). Starting with the distance sensor, there are 2 legs that could be connected to the PWM pins. The Trigger Pin is connected to 3 and the Echo pin is connected to 4. The PIR motion sensor has only one signal pin which is connected to 5. the buzzer or piezo is connected to pin 6. The Photo-resistor or LDR is connected to pin 7. The RGB LED is connected to 3 separate pins being 9 for red, 10 for green, and 11 for blue. The white LED is connected to the second terminal of the LDR or photo-resistor. The pushbutton is connected to the anode of the white LED. The motion sensor and distance sensor both need to be connected to the power and ground rails. the LED, RGB LED, buzzer and pushbutton or only connected to the ground rail as their power comes from their PWM pins. When powering the power and ground rails, a wire should be connected from the 5v and GND pins to the side rails of the breadboard.

Code

Screenshot (132).png
Screenshot (133).png

In order for all these components to actually work together a little bit of code is needed. This project will not work without this code.

int redPin = 9;
int greenPin = 10;

int bluePin = 11;

int pirState = LOW;

int inputPin = 5;

int value = 0;

const int pingPin = 3;

const int echoPin = 4;

int buzzer = 6;

bool color = false;

#define COMMON_CATHODE

void setup(){

pinMode(7, OUTPUT);

pinMode(6, OUTPUT);

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

pinMode(inputPin, INPUT);

}

void setColor(int red, int green, int blue) {

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue); }

void loop() {

digitalWrite(7, HIGH);

value = digitalRead(inputPin);

if (value == HIGH){

if (pirState == LOW) {

pirState = HIGH;

}

}

else {

if (pirState == HIGH){

pirState = LOW;

}

}

long duration, inches, cm;

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(10);

digitalWrite(pingPin, LOW);

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);

cm = microsecondsToCentimeters(duration);

setColor(255, 0, 0);

bool color = true;

if (cm < 50 && pirState == HIGH && color != false){

digitalWrite(buzzer, HIGH);

}

else{

digitalWrite(buzzer, LOW);

}

delay(5000);

color = false;

if (cm < 50 && pirState == HIGH && color != false){

digitalWrite(buzzer, HIGH);

}

else{

digitalWrite(buzzer, LOW);

}

setColor(255, 0, 255);

delay(1500);

setColor(0, 0, 255);

delay(4000);

}

long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;

}

Completed

Great Job! You have completed the enhanced traffic light. Hopefully all these steps were easy to understand and follow and I hope that you have fun building many more Arduino projects.