Traffic Light Control Project using Arduino
Last Updated :
18 Apr, 2024
Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards can read digital & analog inputs from the sensors. With Arduino, you can write and upload computer code to a physical programmable circuit board (commonly called a microcontroller which is ATmega328P 8-bit Microcontroller) using a piece of software called the Arduino IDE (Integrated Development Environment), which runs on your computer.
The Arduino is the most widely used open-source microcontroller board for numerous electronics and do-it-yourself projects. The Arduino traffic light controller is a simple project. Nowadays, everyone desires to drive their own car. As a result, the number of cars on the road is constantly rising, resulting in traffic bottlenecks.
A traffic light controller assists in traffic management and traffic control. These devices are installed at road crossroads or crossings to prevent traffic congestion and accidents. The systems alert the motorist by employing various colours of light. As a result, avoiding congestion at junctions is straightforward.
In this project, we are going to make a traffic light control for a four-way intersection. We will learn about traffic lights and how they work through an electronic project. This project is a condensed version of the four-side or direction traffic light system that we have exhibited.
Required components:
- Arduino
- 12 pieces of 220-ohm resistors
- 4 pieces of breadboard
- Connecting wires
- Red, yellow, and green LEDs
Circuit Diagram:
Connect the components as mentioned in the circuit diagram
Circuit DiagramCode for the Arduino Traffic Light
1-Assign the traffic lights pins to variables
int d_red =10;
int d_yellow =9;
int d_green =8;
int r_red =4;
int r_yellow =3;
int r_green =2;
int l_red =13;
int l_yellow =12;
int l_green =11;
int u_red =7;
int u_yellow =6;
int u_green =5;
2-Configure the traffic lights as outputs
void setup()
{
pinMode(d_red, OUTPUT);
pinMode(d_yellow, OUTPUT);
pinMode(d_green, OUTPUT);
pinMode(r_red, OUTPUT);
pinMode(r_yellow, OUTPUT);
pinMode(r_green, OUTPUT);
pinMode(l_red, OUTPUT);
pinMode(l_yellow, OUTPUT);
pinMode(l_green, OUTPUT);
pinMode(u_red, OUTPUT);
pinMode(u_yellow, OUTPUT);
pinMode(u_green, OUTPUT);
}
3-Use loop function to keep the lights in a loop and use changeLIght() function to carry out the logic
void loop()
{
changeLights();
}
void changeLights()
{
//Start (all yellow)
digitalWrite(u_red,LOW);
digitalWrite(d_red,LOW);
digitalWrite(r_red,LOW);
digitalWrite(l_green,LOW);
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
delay(5000);
//upper lane go
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_green,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(d_red,HIGH);
delay(10000);
//ALL YELLOW
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_green,LOW);
digitalWrite(r_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(d_red,LOW);
delay(5000);
//RIGHT LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(d_red,HIGH);
digitalWrite(r_green,HIGH);
delay(10000);
//ALL YELLOW ON
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(d_red,LOW);
digitalWrite(r_green,LOW);
delay(5000);
//DOWN LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(d_green,HIGH);
delay(10000);
//ALL YELLOW
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(r_red,LOW);
digitalWrite(d_green,LOW);
delay(5000);
//LEFT LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(d_red,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(l_green,HIGH);
delay(10000);
}
Explanation:-Here the loop start with all the yellow light turned on and the green light moving in a clockwise direction when one of the green light is on the rest of the direction will be red and it all will do a transition with the yellow light turned on in every direction.
4-Compile the code and upload it to the Arduino
C++
int d_red =10;
int d_yellow =9;
int d_green =8;
int r_red =4;
int r_yellow =3;
int r_green =2;
int l_red =13;
int l_yellow =12;
int l_green =11;
int u_red =7;
int u_yellow =6;
int u_green =5;
void setup()
{
pinMode(d_red, OUTPUT);
pinMode(d_yellow, OUTPUT);
pinMode(d_green, OUTPUT);
pinMode(r_red, OUTPUT);
pinMode(r_yellow, OUTPUT);
pinMode(r_green, OUTPUT);
pinMode(l_red, OUTPUT);
pinMode(l_yellow, OUTPUT);
pinMode(l_green, OUTPUT);
pinMode(u_red, OUTPUT);
pinMode(u_yellow, OUTPUT);
pinMode(u_green, OUTPUT);
}
void loop()
{
changeLights();
}
void changeLights()
{
//Start (all yellow)
digitalWrite(u_red,LOW);
digitalWrite(d_red,LOW);
digitalWrite(r_red,LOW);
digitalWrite(l_green,LOW);
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
delay(5000);
//upper lane go
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_green,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(d_red,HIGH);
delay(10000);
//ALL YELLOW
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_green,LOW);
digitalWrite(r_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(d_red,LOW);
delay(5000);
//RIGHT LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(d_red,HIGH);
digitalWrite(r_green,HIGH);
delay(10000);
//ALL YELLOW ON
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(d_red,LOW);
digitalWrite(r_green,LOW);
delay(5000);
//DOWN LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(l_red,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(d_green,HIGH);
delay(10000);
//ALL YELLOW
digitalWrite(u_yellow,HIGH);
digitalWrite(d_yellow,HIGH);
digitalWrite(r_yellow,HIGH);
digitalWrite(l_yellow,HIGH);
digitalWrite(u_red,LOW);
digitalWrite(l_red,LOW);
digitalWrite(r_red,LOW);
digitalWrite(d_green,LOW);
delay(5000);
//LEFT LANE GO
digitalWrite(u_yellow,LOW);
digitalWrite(d_yellow,LOW);
digitalWrite(r_yellow,LOW);
digitalWrite(l_yellow,LOW);
digitalWrite(u_red,HIGH);
digitalWrite(d_red,HIGH);
digitalWrite(r_red,HIGH);
digitalWrite(l_green,HIGH);
delay(10000);
}
Output:
Similar Reads
Servo motor Interfacing and Control using Arduino In this article, we will learn how to interface and control servo motors using Arduino Uno R3. Arduino is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in t
3 min read
Smart Collision Detector using Arduino Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards can read digital & analog inputs from the sensors and the HR SC-04 Ultrasonic sensor uses sonar to find the distance between objects. It can measure distances ranging from 2cm to 400cm with
4 min read
Smart SunLight detection using Arduino In our daily technological advancements we should utilize the renewable sources like sun ,wind, water etc. Particularly for the solar energy the operations and applications are huge. For the applications of solar energy we should monitor and optimize the solar powered systems using these types of em
8 min read
Smart Parking System Using Arduino Arduino is an open-source electronics platform comprising hardware and software components. It was created to make it easier for non-experts, such as artists, designers, and hobbyists, to work with electronics and create interactive projects. The platform includes an easy-to-use development board, a
6 min read
Control Arduino with Python and pyFirmata In this article, we will learn how to link an Arduino to a Python script in order to operate the Arduino. This example of constructing a 4-bit binary up-counter using Python script to control Arduino helps us understand this better. Components Required:An Arduino Board (We will be using Arduino UNO,
4 min read
Overview of the Arduino UNO Components Arduino is an incredibly important part of modern-day electronics. The ease with which these Arduino boards can be programmed makes them the best choice especially when it comes to integrating them with large-scale projects. In this article, we will get an overview of the basic components that make
9 min read
How to make Motion Detection System using Arduino? Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards can read digital & analog inputs from the sensors and The PIR sensor is a special type of sensor which is usually used for security purposes. It detects the objects by reading the Infrared r
2 min read
Top 7 Projects in Robotics For Beginners and Intermediates Today, Robotics is one of the best technologies which deals with the design, working, and applications of computer systems for their control and information processing, and in many industries robots are used. This technology also deals with automated machines and is very useful in manufacturing prod
11 min read
How to Set Up Azure Traffic Manager: A Step-by-Step Guide Azure Traffic Manager (ATM) is a powerful Azure service to manage user traffic and distributes it to service endpoints using various routing methods. In this article, I will be guiding you with the process of creating a web app till setting up an Azure Traffic Manager. Primary TerminologiesAzure: Az
6 min read
Transport Demand Prediction using Regression Transport demand prediction is a crucial aspect of transportation planning and management. Accurate demand forecasts enable efficient resource allocation, improved service planning, and enhanced customer satisfaction. Regression analysis, a statistical method for modeling relationships between varia
8 min read