In this project, we are creating a Two-way Traffic Light system to control the flow of cars coming from two different directions. The primary objectives of this system are:
State 1: One side of the road will display a green light, allowing traffic to pass, while the opposing side shows a red light, indicating that traffic must stop.
State 2: The green light from the first side will change to yellow for a short period, then to red. After a short delay to allow the last cars to clear the section, the second side will switch to green, allowing traffic to flow in the opposite direction.
Components Required
Circuit Diagram

Arduino Program Code
Download the Arduino Program Code for the Two-way Traffic Light Signal.
// Two-Way Traffic Light System
// Road A: Red, Yellow, Green
// Road B: Red, Yellow, Green
// Define pins for Road A
int redA = 2;
int yellowA = 3;
int greenA = 4;
// Define pins for Road B
int redB = 5;
int yellowB = 6;
int greenB = 7;
void setup() {
// Set all pins as OUTPUT
pinMode(redA, OUTPUT);
pinMode(yellowA, OUTPUT);
pinMode(greenA, OUTPUT);
pinMode(redB, OUTPUT);
pinMode(yellowB, OUTPUT);
pinMode(greenB, OUTPUT);
}
void loop() {
// Step 1: Road A GREEN, Road B RED
digitalWrite(greenA, HIGH);
digitalWrite(redA, LOW);
digitalWrite(yellowA, LOW);
digitalWrite(redB, HIGH);
digitalWrite(yellowB, LOW);
digitalWrite(greenB, LOW);
delay(5000); // A road green for 5 seconds
// Step 2: Road A YELLOW, Road B RED
digitalWrite(greenA, LOW);
digitalWrite(yellowA, HIGH);
digitalWrite(redA, LOW);
digitalWrite(redB, HIGH);
digitalWrite(yellowB, LOW);
digitalWrite(greenB, LOW);
delay(2000); // A yellow for 2 seconds
// Step 3: Road A RED, Road B GREEN
digitalWrite(redA, HIGH);
digitalWrite(yellowA, LOW);
digitalWrite(greenA, LOW);
digitalWrite(greenB, HIGH);
digitalWrite(yellowB, LOW);
digitalWrite(redB, LOW);
delay(5000); // B road green for 5 seconds
// Step 4: Road A RED, Road B YELLOW
digitalWrite(redA, HIGH);
digitalWrite(yellowA, LOW);
digitalWrite(greenA, LOW);
digitalWrite(greenB, LOW);
digitalWrite(yellowB, HIGH);
digitalWrite(redB, LOW);
delay(2000); // B yellow for 2 seconds
// Repeat cycle
}
Output



