Traffic Light Control Project
Using Arduino
What is Arduino?
So you might have heard about Arduino from your friends at school or on the
internet, it seemed quite exciting, but you don’t know where to start, fret not
as this article will let you know the basics of getting started with your shiny
new Arduino.
ARDUINO-THE COMPANY
Arduino was a project started at Interaction Design Institute Ivrea (IDII) in
Ivrea, Italy, with its primary goal being creating affordable and
straightforward tools for non-engineers to use and create digital projects.
During its infancy, the project consisted of just three members- Hernando
Barragán, Massimo Banzi, and Casey Reas. Hernando Barragán worked
under the guidance of Massimo Banzi and Casey Reas and created a
development platform called Wiring as his masters’ thesis project at IDII. The
development platform consisted of the ATMega168 microcontroller as its
brains and used an IDE based on Processing, which was co-created by Casy
Reas. Later, Massimo Banzi, along with two other students from IDII,
namely- David Mellis and David Cuartielles, added support for the cheaper
ATMega8 microcontroller. The three, instead of working on developing and
improving Wiring, they forked it and renamed the project to Arduino. The
initial core Arduino team consisted of Massimo Banzi, David Cuartielles, Tom
Igoe, Gianluca Martino, and David Mellis, but Barragán was not included.
THE HARDWARE
Now that you know the origin of Arduino, it is essential to get yourself
acquainted with the hardware that Arduino as a company offers. One of the
main reasons for Arduino being so accessible and affordable across the
globe is because all of the Arduino hardware is open-source. Being open-
source has a plethora of advantages- anyone can access the design and
build of the device and make improvements; anyone can use the same
hardware design to create their product lineup. Since Arduino is open-
source, it has its own devoted community that strives to help the core
company develop and improve its hardware products. Another significant
advantage of being open-source, especially in the case of hardware, is that
local companies can create replicas of the products, making it more
accessible and affordable to the local consumers as it avoids hefty customs
and shipping charges. All of these advantages contribute to Arduino being so
widespread, affordable and ever-improving.
It is necessary to know that Arduino doesn’t necessarily offer just one piece
of hardware, it provides a range of boards, each of which caters to a different
level of expertise and have different use-cases altogether. Arduino Uno is
one of the most basic and popular boards that Arduino offers. This is
because it features an ATMega328 microcontroller that is both cheap and
powerful enough for most basic beginner-level projects. Once you’re familiar
with Arduino IDE, you can move up to boards with more powerful and
sophisticated chipsets like the MKR range which is concerned with IoT
applications and inter compatibility, or the Nano range which as the name
suggests is designed to keep the form factor as small as possible while
packing most of the features and power of the full-sized boards.
UNDERSTANDING THE HARDWARE
So you got yourself an Arduino Uno, and you’re ready to jump into the world
of electronics and join the community of makers from around the world, but
before you begin with programming and external circuitry through
breadboards and whatnot, it is necessary to understand the layout and
circuitry of your Arduino
Uno.
Using the above image as a reference, the labeled components of the board
respectively are-
1. USB: can be used for both power and communication with the IDE
2. Barrel Jack: used for power supply
3. Voltage Regulator: regulates and stabilizes the input and output
voltages
4. Crystal Oscillator: keeps track of time and regulates processor
frequency
5. Reset Pin: can be used to reset the Arduino Uno
6. 3.3V pin: can be used as a 3.3V output
7. 5V pin: can be used as a 5V output
8. GND pin: can be used to ground the circuit
9. Vin pin: can be used to supply power to the board
10. Analog pins(A0-A5): can be used to read analog signals to the
board
11. Microcontroller(ATMega328): the processing and logical unit of
the board
12. ICSP pin: a programming header on the board also called SPI
13. Power indicator LED: indicates the power status of the board
14. RX and TX LEDs: receive(RX) and transmit(TX) LEDs, blink
when sending or receiving serial data respectively
15. Digital I/O pins: 14 pins capable of reading and outputting
digital signals; 6 of these pins are also capable of PWM
16. AREF pins: can be used to set an external reference voltage as
the upper limit for the analog pins
17. Reset button: can be used to reset the board
GETTING STARTED WITH THE ARDUINO IDE
Now that you’re familiar with the hardware, its time to learn about the
development environment using which you’re going to program your Uno.
The Arduino IDE is the best place to start your journey in programming your
Uno. To get started, visit this page and download the latest build of the
Arduino IDE for your Mac or PC. Go ahead and install the IDE on your PC or
Mac and open it.
As you open the IDE, you’ll be greeted by a window similar to the one shown
in the above image. The text editor is where you’ll be writing your code; you’ll
use the verify button to compile and debug the written program, the save
button to save the program and the upload button to upload the program to
the board. Before you click on the upload button, it is necessary to select
your board, Uno in this case, from the tools menu in the Menu Bar. After you
choose your appropriate board, make sure you specify the correct port on
your PC or Mac that you’ve connected your Uno to, in the IDE.
UPLOADING YOUR FIRST PROGRAM
In this example program, we’ll be blinking the inbuilt L LED located right
above the RX and TX LEDs. The Arduino IDE includes many basic programs
to help you get started with your Uno. For this example, we’ll be using the
inbuilt ‘Blink’ program. To open this program, go to the Files menu in the
Menu Bar; click on Examples; click on [Link]; select Blink. Now that
you’ve opened the example program, its time to upload the program, to do
this, click on the upload button and wait for the process to complete. If your
Output Pane header turns amber and shows an error which reads “Serial
Port COM’x’ not found”, you’ve not connected your board correctly or that
you’ve not specified the correct port that your board is connected to in the
IDE. When you advance and start writing your own programs, you might run
into errors while compiling and uploading; this can be because of a syntax
error in the program. After you’ve corrected the errors and uploaded the
program, you’ll see that the inbuilt LED blinks, alternating between the ON
and OFF state every second.
Congrats on uploading and executing your first piece of code on your
Arduino Uno. You can now tinker with the program you just uploaded by
changing the values of delay. This will change the pattern and the rate of
blinking. Do keep in mind that the default unit of time in the Arduino IDE is
milliseconds; also remember that you’ve to upload the program to the board
after you’ve made changes in the values of delay to notice the changes in
the rate and pattern of the blinking.
MOVING AHEAD
Now that you’re familiar with the IDE and the hardware on the board, you can
move up to programs that require external actuators and sensors using the
inbuilt example programs as a reference. After you’ve gained some expertise
with the board, you can move on to create projects that inculcate your
innovative and innovative ideas. Soon in your journey through electronics,
you’ll realize that the Uno is not powerful enough or does not pack the
features you require for your expert level programs, that is when you’ll have
to consider upgrading your board to something from the MKR line or to more
powerful lines like the Yun.
Let’s Talk About Our Project :
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:
1. Arduino
2. 12 pieces of 220-ohm resistors
3. 4 pieces of breadboard
4. Connecting wires
5. Red, yellow, and green LEDs
Circuit Diagram:
Connect the components as mentioned in the circuit diagram
Circuit Diagram
Code 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
Output :