DEPARTMENT OF COMPUTER SCIENCE
BAHAUDDIN ZAKARIYA UNIVERSITY,
MULTAN
FINAL TERM LAB REPORT
DIGITAL LOGIC DESIGN
TRAFFIC LIGHT
Submitted To:
Ma’am Sadia Latif
Submitted By:
GROUP # 4
Muhammad Usaid BSCS-M-22-02
Muhammad Anas BSCS-M-22-04
Muhammad Aneeq BSCS-M-22-05
Muhammad Zohaib BSCS-M-22-21
Muhammad Asif BSCS-M-22-`14
Shoukat Ali BSCS-M-22-24
Sheraz Kousar BSCS-M-22-19
Zain BSCS-M-21-
Submission Date: 13.12.2024
TOPIC:
Design and implement a simple traffic light controller using sequential circuits like
flip-flops and counters.
Problem Statement:
Design and Implementation of a Traffic Light Controller using
Sequential Circuits
Traffic lights are critical in controlling traffic flow and ensuring road safety at
intersections. This project aims to design and implement a simple traffic light
controller using sequential circuits such as flip-flops and counters. The
system should simulate the operation of a standard traffic light, switching
between green, yellow, and red lights for two intersecting roads (e.g., Road A
and Road B) based on a defined timing sequence.
Requirements:
1. Basic Functionality:
o The traffic lights must control two roads (A and B).
o Each road must have three lights: green, yellow, and red.
o Only one road can have a green light at any given time, while the
other road shows a red light.
2. Timing Control:
o The system must follow a fixed timing sequence:
Green: 10 seconds
Yellow: 2 seconds
Red: 12 seconds (while the other road's green and yellow
are active).
3. Circuit Design:
o The design must use flip-flops (e.g., D, JK, or T) and counters to
implement the state transitions.
o The circuit must have a clock signal to synchronize state changes.
4. State Transitions:
o Define the states for the traffic light (e.g., Road A green, Road A
yellow, Road B green, Road B yellow).
o Ensure proper sequencing of lights using a state diagram and
truth table.
5. Reset Functionality:
o The system must include a reset button to restart the traffic light
sequence from an initial state.
6. Scalability:
o The design should be modular to allow future expansion, such as
adding pedestrian signals or more roads.
Working Mechanism:
The traffic light controller operates based on a predefined sequence of states,
which dictate the on/off status of the green, yellow, and red lights for two
intersecting roads. Sequential circuits, such as flip-flops and counters, are
used to implement these states and control the transitions.
Key Components and Their Roles
1. Clock Signal:
o Provides the timing pulse to synchronize state transitions.
o Determines the duration of each state (green, yellow, red).
2. Counters:
o Count clock pulses to control the timing for each light.
o For example, a 10-second green light can be achieved by
counting 10 pulses of a 1-second clock.
3. Flip-Flops:
o Store the current state of the traffic light system.
o Transition between states based on the clock signal and input
conditions.
4. State Diagram:
o Defines the sequence of states for the system:
1. Road A: Green → Yellow → Red
2. Road B: Green → Yellow → Red
o Ensures no conflicting states occur (e.g., both roads cannot have
green lights simultaneously).
5. Logic Gates:
o Implement the state transition logic.
o Drive the outputs for the green, yellow, and red lights based on
the current state
Arduino IDE Code
#define s1_r 2 // Green Led Pin Out
#define s1_y 3 // Yellow Led Pin Out
#define s1_g 4 // Red Led Pin Out
#define s2_r 5 // Green Led Pin Out
#define s2_y 6 // Yellow Led Pin Out
#define s2_g 7 // Red Led Pin Out
#define s3_r 8 // Green Led Pin Out
#define s3_y 9 // Yellow Led Pin Out
#define s3_g 10// Red Led Pin Out
#define s4_r 11// Green Led Pin Out
#define s4_y 12// Yellow Led Pin Out
#define s4_g 13// Red Led Pin Out
int G1_time = 4; //Set singl 1 open time
int G2_time = 6; //Set singl 2 open time
int G3_time = 8; //Set singl 3 open time
int G4_time = 10; //Set singl 4 open time
int Y_time = 1; //Set All singl Wait time
int Mode=0;
int Second=0;
word MilliSecond=0;
void setup(){ // put your setup code here, to run once
pinMode(s1_r, OUTPUT);
pinMode(s1_y, OUTPUT);
pinMode(s1_g, OUTPUT);
pinMode(s2_r, OUTPUT);
pinMode(s2_y, OUTPUT);
pinMode(s2_g, OUTPUT);
pinMode(s3_r, OUTPUT);
pinMode(s3_y, OUTPUT);
pinMode(s3_g, OUTPUT);
pinMode(s4_r, OUTPUT);
pinMode(s4_y, OUTPUT);
pinMode(s4_g, OUTPUT);
noInterrupts(); // disable all interrupts
TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at
1kHz // 1 ms
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // set timer count for 1khz increments
OCR1A = 1999; // = (16*10^6) / (1000*8) - 1
//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2
with larger prescaler
// turn on CTC mode
TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11); // enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable
Second = Y_time;
yellow(Mode);
delay(100); // Waiting for a while
}
void loop(){
if(Mode==0 && Second==0){Second=G1_time; Mode=1; Open(Mode);}
if(Mode==1 && Second==0){Second=Y_time; Mode=2; yellow(Mode);}
if(Mode==2 && Second==0){Second=G2_time; Mode=3; Open(Mode);}
if(Mode==3 && Second==0){Second=Y_time; Mode=4; yellow(Mode);}
if(Mode==4 && Second==0){Second=G3_time; Mode=5; Open(Mode);}
if(Mode==5 && Second==0){Second=Y_time; Mode=6; yellow(Mode);}
if(Mode==6 && Second==0){Second=G4_time; Mode=7; Open(Mode);}
if(Mode==7 && Second==0){Second=Y_time; Mode=0; yellow(Mode);}
delay(10);
}
void yellow(int y){
digitalWrite(s1_r, 1);
if(y==0){digitalWrite(s1_y, 1);}
else{digitalWrite(s1_y, 0);}
digitalWrite(s1_g, 0);
digitalWrite(s2_r, 1);
if(y==2){digitalWrite(s2_y, 1);}
else{digitalWrite(s2_y, 0);}
digitalWrite(s2_g, 0);
digitalWrite(s3_r, 1);
if(y==4){digitalWrite(s3_y, 1);}
else{digitalWrite(s3_y, 0);}
digitalWrite(s3_g, 0);
digitalWrite(s4_r, 1);
if(y==6){digitalWrite(s4_y, 1);}
else{digitalWrite(s4_y, 0);}
digitalWrite(s4_g, 0);
}
void Open(int Set){
digitalWrite(s1_y, 0);
if(Set==1){digitalWrite(s1_g, 1); digitalWrite(s1_r, 0);}
else{digitalWrite(s1_g, 0); digitalWrite(s1_r, 1);}
digitalWrite(s2_y, 0);
if(Set==3){digitalWrite(s2_g, 1); digitalWrite(s2_r, 0);}
else{digitalWrite(s2_g, 0); digitalWrite(s2_r, 1);}
digitalWrite(s3_y, 0);
if(Set==5){digitalWrite(s3_g, 1); digitalWrite(s3_r, 0);}
else{digitalWrite(s3_g, 0); digitalWrite(s3_r, 1);}
digitalWrite(s4_y, 0);
if(Set==7){digitalWrite(s4_g, 1); digitalWrite(s4_r, 0);}
else{digitalWrite(s4_g, 0); digitalWrite(s4_r, 1);}
ISR(TIMER1_COMPA_vect){
MilliSecond++;
if(MilliSecond >= 1000){MilliSecond = 0;
Second = Second-1;
}
}
CIRCUIT DIAGRAMS:
schematic diagram
HARDWARE IMPLEMENTATION:
COMPONENTS USED:
1. Arduino Uno
2. Breadboard (Protoboard)
3. LEDs (Red, Yellow, Green)
4. Resistors
5. Power Supply
6. Wires and Connectors