Arduino Traffic Light
The system must be used to control the traffic lights for the smooth and safe movement of traffic.
Components and Supplies
Arduino Uno
Resistor 10k ohm
Resistor 220 ohm
Breadboard
Connecting wires
Red LED
Green LED
Yellow LED
Switch
Steps to Follow
Traffic Lights are indicating devices that are used to control the flow of traffic. Generally, they are located at junctions, ‘X’ roads, pedestrian crossings, etc. and alternate the priority of who has to wait and who has to go.
The traffic lights will give instructions to the users (drivers and pedestrians) by showing lights of standard color. The three colors generally used in traffic lights are Red, Yellow and Green.
Connect the anode of each LED to digital pins eight, nine and ten (via a 220-ohm resistor). Connect the cathodes to the Arduino’s ground. Design a Code for Arduino Traffic Light Start by assigning variables so that you can address the lights by name rather than a number.
Next, add the setup function, where you’ll configure the red, yellow and green LEDs to output. As you have created variables to represent the pin numbers, you can mention to the pins by name instead.
The pinMode function configures the Arduino to use a given pin as an output. Now for the actual logic of the traffic light. Below is the code that you need. Upload the code to your Arduino and run (make sure to select the correct board and port from the Tools > Board and Tools > Port menus). The final outcome should have a working traffic light that changes every 15 seconds. The Internet of Things Course Training will help you to implement your own ideas and to build an IoT Application over several challenges.
Run a Program
int red = 10;
int yellow = 9;
int green = 8;
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop(){
changeLights();
delay(15000);
}
void changeLights(){
// green off, yellow on for 3 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(3000);
// turn off yellow, then turn red on for 5 seconds
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(5000);
// red and yellow on for 2 seconds (red is already on though)
digitalWrite(yellow, HIGH);
delay(2000);
// turn off red and yellow, then turn on green
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(3000); }