Advanced Traffic Light System

by Jeet756868 in Circuits > Arduino

76 Views, 0 Favorites, 0 Comments

Advanced Traffic Light System

WhatsApp Image 2025-01-21 at 21.43.07_0b765301.jpg
WhatsApp Image 2025-01-22 at 04.16.19_66a12487.jpg

Once upon a time, the incredible concept of a 'traffic light' was invented to make street life a lot more simple. Who knew with the development of our minds and technology, that same concept could be built onto a breadboard with some creative adjustments.

Welcome back to yet another Instructable that shows you step by step on how to create your very own advanced and fun Arduino traffic light system. It works almost just like a regular traffic light you'd see on a day to day basis, but with even more - that's essentially the gist of it.

You might not be able to grasp it right away, but how one goes about building this project is actually much more easier than you think. But, how does this so-called advanced traffic light system even work? Great question. Essentially, like any other traffic light, the basis of it operates the exact same way. When the push button is pressed, the entire system turns on, therefore, the green LED turns on for about a few seconds, followed by yellow, then red. The red LED will stay on with the sequence on pause, however, if the distance sensor senses a distance less than 5 inches while it's on, it will trigger the seven segment display to start counting backwards to 0 from 9. When '9' is displayed, the RGB LED will turn red, indicating it is the farthest away from the green LED to signal "Go" and you must remain at a full stop. When '5' is displayed, the RGB LED will turn blue with an indication of the halfway mark, and when '0' is displayed, the RGB LED will turn green. Immediately after it turns green, the countdown will end and the RGB will turn off. The green LED will then turn on and follow the same exact sequence of the traffic light. If the pushbutton is pressed again, the entire system turns off.

Supplies

20250120_102359.jpg
IMG-20250122-WA0002 (1).jpg
20250120_102329.jpg
20250120_102324.jpg
20250120_102336.jpg
20250120_102334.jpg
20250120_102326.jpg
20250120_102320.jpg
20250120_102321.jpg
  1. Arduino Uno
  2. Arduino Cable
  3. Long Breadboard
  4. 1 x Pushbutton
  5. 1 x Red LED
  6. 1 x Yellow LED
  7. 1 x Green LED
  8. 1 x RGB LED (Common Anode)
  9. 6 x 330Ω Resistors
  10. 1 x 10kΩ Resistor
  11. 7 Segment Display
  12. Distance Sensor

Placing the Components Onto the Breadboard

Seriously .jpg

1 - The LEDs and 330Ω Resistors

The best idea would first be to plan out where you're going to place the green, yellow, and red LED that essentially serve the main part of the purpose. The initial plan is to have enough space between your LEDs and other components, similar to the example above, so that later you will be able to put them into a miniature traffic light prop as recommended - Because that's the best part.

  1. First, place the red LED onto your breadboard, then follow it by the yellow LED, and then green. This will later be taken and put into an upright prop.
  2. Then of course, connect a 330Ω resistor to the negative (cathode) of each led.


2 - The Pushbutton

Placing the pushbutton onto the breadboard is a rather simple process, but also a precise one. Once you have placed it onto the breadboard, follow these steps to ensure it is wired properly:

  1. Connect the 10kΩ resistor to the first terminal of the pushbutton (left side), and place it into the negative rail on the breadboard - This process connects the pushbutton to ground.
  2. Then, take a red wire and attach it to the second opposing terminal of the pushbutton, then connect it to power.


3 - The Distance Sensor

Wiring the distance sensor on the breadboard is a process that doesn't require much of an explanation. There is a label that says 'Vcc' on the far left which is what you use a red wire to connect to power, and as well as that, a label that says 'Gnd' on the far right which is what you use a black wire to connect to ground.


4 - The RBG LED

In a simple essence, the RGB LED only needs one 330Ω resistor, the rest of its legs will be later wired to the Arduino.

  1. In this case, the RGB LED in use is a common anode, therefore the 330Ω resistor will be connected to the positive rail on the breadboard (power).
  2. This leg is fairly easy to differentiate from the others - it is the longest leg of the RGB LED.


5 - The Seven Segment Display

Place the seven segment display onto the breadboard like the example in the image above and follow the simple two following steps:

  1. First, connect a 330Ω resistor to the common of the seven segment display, which is then connected to power.
  2. Then, connect another 330Ω resistor to the corresponding common on the opposite side of the seven segment display, which is also connected to power.


Last but not least, the final step is to use a red wire to connect each side of the positive rails to each other, and a black wire to connect each side of the negative rails to each other. This ensures that power and ground is equally transmitted throughout the entire circuit.

And there you have it, you are officially done with wiring the components onto the breadboard portion and can now move on to the next part. Even more wiring.

Begin Wiring the Entirety - Arduino

20250120_101503.jpg
20250121_205329.jpg

With the intricate wiring throughout this project, mainly due to the seven segment display, it is crucial to follow each pin connection carefully in an organized manner. Therefore, this step will guide one through the specific Arduino pins designated for each component.

For now, regarding the 3 LEDs, simply connect your wires to them and the designated pins. The steps on how to go about making the actual traffic light prop will be introduced later on.

(3D wiring is optional)


The LEDs:

  1. For this instance, the red LED is connected to pin '13' using a red wire
  2. The yellow LED is connected to pin '6' using a yellow wire
  3. And lastly, the green LED is connected to pin '7' using a green wire


The Pushbutton:

  1. Connect the corresponding terminal to where the resistor is connected to a digital input pin with a coloured wire of choice, in this case pin '12' is used.


The RGB LED:

  1. The RGB has 4 specified pins: Red, Anode, Green and Blue. Click here for image of RGB, follow Common Anode
  2. The Anode is connected to power with a resistor
  3. The red, green, and blue pins all must be connected to PWM (Pulse Width Modulation) pins.


Connect the red pin to pin '11'

Connect the green pin to pin '10'

Connect the blue pin to pin '9'


The 7-Segment Display

A 7-segment display consists of 7 segments, labeled a, b, c, d, e, f, and g. This step is quite the process, however, this image (click here) and specifications below will surely make it make sense:

Since most of the pins were used for the other components, majority of the seven segment display will be using the analog pins (A0 - A5) instead.

Connect pin 'a' of the 7-segment to Arduino pin 'A5'

Connect pin 'b' of the 7-segment to Arduino pin '2' (This is the only one that will not be using an analog pin)

Connect pin 'c' of the 7-segment to Arduino pin 'A3'

Connect pin 'd' of the 7-segment to Arduino pin 'A2'

Connect pin 'e' of the 7-segment to Arduino pin 'A0'

Connect pin 'f' of the 7-segment to Arduino pin 'A4'

And finally, connect pin 'g' of the 7-segment to Arduino pin 'A1'


For the wiring, it is recommended that you use the same colour for the entire seven segment display like the image above.

There you have it, the wiring portion is complete and we can now move on to actually getting the project working.

Breaking Down the Code - Declare Variable Names and Pins

1.png

This is the easy part of the code. Declare each variable for each component and which Arduino pin they use like so. You can use whichever variable name you'd like, or simply just use the same one's provided.

Breaking Down the Code - Void Setup

Void setup and Loopie .jpg
image (1).png


The first part of void setup states which specific components are declared as input or output. The output devices are always declared as output, and the input as input, simple as that. Regarding the distance sensor, 'trig' is declared as output, and 'echo' is declared as input.

This is the second part to void setup:

void zero (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}

void one (){
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}

void two (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);

}

void three (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);
}

void four (){
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void five (){
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void six (){
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void seven (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);

}

void eight (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}

void nine (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}



This portion of the code is what defines the functions to control a 7-segment display and shows the digits 0–9. Each digit is represented by a specific combination of segments turned on or off (LOW or HIGH). It essentially works in the opposite way of how one would think for it to. 'HIGH' in this case, turns a segment off, and 'LOW' to the contrary turns it on.

Take 'void zero' for example:

  1. Segments a, b, c, d, e, f are on (LOW).
  2. Segment g is off (HIGH).

This lights up the shape of the digit "0" on the 7-segment display.


This is the third and last part of void setup:

void setColour (int red, int green, int blue)
{
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
analogWrite (redPin, red);
analogWrite (greenPin, green);
analogWrite (bluePin, blue);
}

Serial.begin(9600);


The function 'void setColour' allows a colour on an RGB to be set by specifying values for the red, green, and blue components (each ranging from 0 to 255). By varying the red, green, and blue values, you can create any colour.

  1. Red, green, and blue are integers representing the brightness levels of the red, green, and blue LEDs.
  2. And as priory mentioned, each value can range from 0 (fully off) to 255 (fully on).


Lastly, Serial.begin(9600); is the command that opens up a serial port and will allow you to later on print your values.

Breaking Down the Code - Void Loop Part 1

image (2).png
image (3).png

The following code does not include the function for a pushbutton.


Now, this first portion for void loop includes both the functions for the LEDs and the distance sensor.

The function 'digitalWrite (green, HIGH);' is what turns the green LED on, and 'delay(5000);' leaves it on for 5 seconds. Then, 'digitalWrite(green, LOW); ' turns the LED off. As the code goes on, the same thing occurs for both the yellow LED (for 3 seconds instead, hence delay(3000);) and the red LED all in that specific order. However, with the red LED, there is no 'delay' or 'digitalWrite (red, LOW);' that turns it off - this is essentially because the red LED will stay on while the rest of the code operates. Later on in the code after a specific sequence, the green LED will turn on again. But we're not there yet.

Before we move on to the basis of how the term 'advanced traffic light' comes in, the second image provided shows the function used to measure distance with the distance sensor. The distance sensor essentially operates by emitting an ultrasonic pulse or sound waves and measures how long those sound waves take to bounce back from an object - hence this code being the setup needed to make that happen. As well as that, it also calculates the distance in inches.

  1. Serial.println (inches); allows the serial monitor to print the distance in inches.

Breaking Down the Code - Void Loop Part 2

image (5).png
image (6).png

Congratulations, you have made to the very last steps of the complete code. This is the section in void look that will do all the advanced main work at the 'red light'. If you're read the introduction, I'm sure you now have a bit of a grasp on what exactly this project does. When the light turns red, the 7-segment display will start counting down from nine. At 9, the RGB will turn red, then it will turn blue at 5, and once the countdown reaches zero, the RGB will turn green. This code does exactly that in the most simple manner.

This part of the code performs a countdown sequence (from 9 to 0) as mentioned using a 7-segment display and an RGB LED. It is only triggered when an object is detected less than 5 inches away from the distance sensor.


'if (inches < 5) {'

  1. This is an if statement and remains as the main trigger condition, the sequence should trigger because of this function (However, if you would like to remove the distance sensor as an easier method, the code operates without this statement and the sensor as well)


7- Segment Display

  1. The functions nine();, eight();, seven();, six();, etc., control the 7-segment display to show numbers from 9 to 0.
  2. 'delay(1000)' means that each number is displayed for 1 second


The RGB LED

  1. The RGB changes colours depending on what number the seven segment displays
  2. setColour(255, 0, 0); sets the RGB to Red.
  3. setColour(0, 0, 255); sets the RGB to Blue.
  4. setColour(0, 255, 0); sets the RGB to Green.
  5. setColour(0, 0, 0); turns the RGB off.


The sequence first shows 9 displayed on the 7-segment. This is when the RBG turns red, delay (1000) means it waits for one second. When numbers 8, 7, and 6 are displayed on the 7-segment, the RBG is off - 'setColour(0, 0, 0);'. When the 7-segment displays number 5, the RBG turns blue - 'setColour(0, 0, 255);'. Numbers 4 through 1 also have the RBG off, and when the 7-segment display shows zero, the RBG turns red - 'setColour(255, 0, 0);'.

The very last part of the code within the second image then turns the red LED off after the sequence ends, and turns the green LED on again. The entire sequence repeats itself in a loop.

The Entire Code

int a = A5;
int b = 2;
int c = A3;
int d = A2;
int e = A0;
int f = A4;
int g = A1;

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

int trig = 4;
int echo = 3;



int red = 13;
int yellow = 6;
int green = 7;

int push = 12;




void setup(){
{
pinMode (redPin, OUTPUT);
pinMode (greenPin, OUTPUT);
pinMode (bluePin, OUTPUT);
pinMode (a, OUTPUT);
pinMode (b, OUTPUT);
pinMode (c, OUTPUT);
pinMode (d, OUTPUT);
pinMode (e, OUTPUT);
pinMode (f, OUTPUT);
pinMode (g, OUTPUT);
pinMode (red, OUTPUT);
pinMode (yellow, OUTPUT);
pinMode (green, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode (push, INPUT);

}
}

void zero (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}

void one (){
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}

void two (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);

}

void three (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);
}

void four (){
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void five (){
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void six (){
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);

}

void seven (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);

}

void eight (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}

void nine (){
digitalWrite (a, LOW);
digitalWrite (b, LOW);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}


void setColour (int red, int green, int blue)
{
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
analogWrite (redPin, red);
analogWrite (greenPin, green);
analogWrite (bluePin, blue);
}

Serial.begin(9600);

void loop (){
digitalWrite(green, HIGH);
delay(5000);
digitalWrite(green, LOW);

digitalWrite (yellow, HIGH);
delay(3000);
digitalWrite (yellow, LOW);
digitalWrite (red, HIGH);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long duration, inches;
duration = pulseIn(echo, HIGH);
inches = duration/74/2;
Serial.println (inches);

if (inches < 5) {
nine();
setColour(255, 0, 0); //red
delay(1000);
eight();
setColour(0, 0, 0); //off
delay(1000);
seven();
setColour(0, 0, 0); //off
delay(1000);
six();
setColour(0, 0, 0); //off
delay(1000);
five();
setColour(0, 0, 255); //blue
delay(1000);
four();
setColour(0, 0, 0); //off
delay(1000);
three();
setColour(0, 0, 0); //off
delay(1000);
two();
setColour(0, 0, 0); //off
delay(1000);
one();
setColour(0, 0, 0); //off
delay(1000);
zero();
setColour(0, 255, 0); //green
delay(1000);
setColour(0, 0, 0); //off
digitalWrite (red, LOW);
}
digitalWrite (green, HIGH);
delay(5000);
digitalWrite (green, LOW);
}



The Traffic Light Prop

Traffic prop .jpg
Unfinished prop .jpg
Emo cardboard .jpg
Black huh .jpg
More black .jpg
Better solder.jpeg

This fun step can be done however you like, therefore, I will only go into it briefly.

You will need:

  1. Cardboard
  2. A Glue Gun
  3. Glue Gun Sticks
  4. Scissors or An Exacto Knife
  5. Black Paint and A Paint Brush
  6. Soldering Iron and Solder


  1. Cut out 4 miniature rectangles from the cardboard, small and big enough to fit all 3 of the LEDs. (Try to make them as equal in size as possible).
  2. Paint them all black.
  3. Then, stick your LEDs through what you'd see fit as the front piece, red at the top, yellow in the middle, and green and the bottom - Just like a traffic light.
  4. Now, you are going to bend the legs of the LEDs downwards, connecting the cathodes to the resistors you placed on the breadboard.
  5. Solder the anode of the LEDs using a soldering iron to their designated wire colour and re-connect them to the Arduino pins.
  6. Since you now have your LEDs prepared, carefully glue the other pieces of cardboard onto the main one together in a somewhat 'box' or 'cube' like shape with your glue gun.
  7. Cut out another piece of cardboard, this one smaller than the rest and in a square shape, then paint it black as well.
  8. Finally, glue that last piece on top of the others to close your traffic light off.


And that's it - You have officially made it to the end of this Intractable. I hope you've given this project a shot and enjoyed making it to the fullest.

Who wouldn't want to make their own traffic light system?

A Video Demonstration

20250122_103458_1_1_1
20250122_103422_1