0% found this document useful (0 votes)
257 views10 pages

Arduino Traffic Light Control Code

This code controls a four-way traffic junction using an Arduino board. It defines pins for the north-south and east-west traffic lights. The lights alternate every 10 seconds of green and 3 seconds of yellow between the directions. The loop function turns the lights on and off according to this timing sequence indefinitely. This provides a basic traffic control system, and additional logic would be needed for more complex real-world intersections.

Uploaded by

Kiprotich Sirma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
257 views10 pages

Arduino Traffic Light Control Code

This code controls a four-way traffic junction using an Arduino board. It defines pins for the north-south and east-west traffic lights. The lights alternate every 10 seconds of green and 3 seconds of yellow between the directions. The loop function turns the lights on and off according to this timing sequence indefinitely. This provides a basic traffic control system, and additional logic would be needed for more complex real-world intersections.

Uploaded by

Kiprotich Sirma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Here's an example code for traffic control of a junction using the Arduino platform:

// Define pins for traffic signals

int redPin1 = 3;

int yellowPin1 = 4;

int greenPin1 = 5;

int redPin2 = 6;

int yellowPin2 = 7;

int greenPin2 = 8;

// Define variables for timing

unsigned long currentTime;

unsigned long previousTime = 0;

int cycleTime = 20000; // 20 seconds

int greenTime = 10000; // 10 seconds

void setup() {

// Set pins as output

pinMode(redPin1, OUTPUT);

pinMode(yellowPin1, OUTPUT);

pinMode(greenPin1, OUTPUT);
pinMode(redPin2, OUTPUT);

pinMode(yellowPin2, OUTPUT);

pinMode(greenPin2, OUTPUT);

void loop() {

// Get current time

currentTime = millis();

// Check if it's time to change the signal

if (currentTime - previousTime >= greenTime && currentTime - previousTime < greenTime + 3000) {

// Turn yellow light on for both signals

digitalWrite(yellowPin1, HIGH);

digitalWrite(yellowPin2, HIGH);

} else if (currentTime - previousTime >= greenTime + 3000 && currentTime - previousTime < cycleTime)
{

// Turn red light on for signal 1

digitalWrite(redPin1, HIGH);

digitalWrite(greenPin1, LOW);

// Turn green light on for signal 2

digitalWrite(greenPin2, HIGH);

digitalWrite(yellowPin2, LOW);

} else if (currentTime - previousTime >= cycleTime) {

// Reset timing variables

previousTime = currentTime;

// Turn green light on for signal 1


digitalWrite(greenPin1, HIGH);

digitalWrite(redPin1, LOW);

// Turn red light on for signal 2

digitalWrite(redPin2, HIGH);

digitalWrite(yellowPin2, LOW);

This code controls two traffic signals, each with three lights (red, yellow, and green). The signals
alternate every 20 seconds, with each signal having a green light for 10 seconds and a yellow light for 3
seconds before changing to red. The Arduino's millis() function is used to keep track of the timing, and
the digitalWrite() function is used to turn the lights on and off. Note that this is a simple example and
does not take into account more complex traffic scenarios or sensors.

// Define pins for traffic signals

int redPin1 = 3;

int yellowPin1 = 4;

int greenPin1 = 5;

int redPin2 = 6;

int yellowPin2 = 7;

int greenPin2 = 8;

int redPin3 = 9;

int yellowPin3 = 10;

int greenPin3 = 11;

int redPin4 = 12;


int yellowPin4 = 13;

int greenPin4 = A0;

// Define variables for timing

unsigned long currentTime;

unsigned long previousTime = 0;

int cycleTime = 40000; // 40 seconds

int greenTime = 20000; // 20 seconds

void setup() {

// Set pins as output

pinMode(redPin1, OUTPUT);

pinMode(yellowPin1, OUTPUT);

pinMode(greenPin1, OUTPUT);

pinMode(redPin2, OUTPUT);

pinMode(yellowPin2, OUTPUT);

pinMode(greenPin2, OUTPUT);

pinMode(redPin3, OUTPUT);

pinMode(yellowPin3, OUTPUT);

pinMode(greenPin3, OUTPUT);

pinMode(redPin4, OUTPUT);

pinMode(yellowPin4, OUTPUT);

pinMode(greenPin4, OUTPUT);

}
void loop() {

// Get current time

currentTime = millis();

// Check if it's time to change the signal

if (currentTime - previousTime >= greenTime && currentTime - previousTime < greenTime + 3000) {

// Turn yellow light on for all signals

digitalWrite(yellowPin1, HIGH);

digitalWrite(yellowPin2, HIGH);

digitalWrite(yellowPin3, HIGH);

digitalWrite(yellowPin4, HIGH);

} else if (currentTime - previousTime >= greenTime + 3000 && currentTime - previousTime < cycleTime)
{

// Turn red light on for signals 1 and 4

digitalWrite(redPin1, HIGH);

digitalWrite(greenPin1, LOW);

digitalWrite(redPin4, HIGH);

digitalWrite(greenPin4, LOW);

// Turn green light on for signals 2 and 3

digitalWrite(greenPin2, HIGH);

digitalWrite(yellowPin2, LOW);

digitalWrite(greenPin3, HIGH);

digitalWrite(yellowPin3, LOW);

} else if (currentTime - previousTime >= cycleTime) {

// Reset timing variables

previousTime = currentTime;
// Turn green light on for signals 1 and 4

digitalWrite(greenPin1, HIGH);

digitalWrite(redPin1, LOW);

digitalWrite(greenPin4, HIGH);

digitalWrite(redPin4, LOW);

// Turn red light on for signals 2 and 3

digitalWrite(redPin2, HIGH);

digitalWrite(greenPin2, LOW);

digitalWrite(redPin3, HIGH);

digitalWrite(greenPin3, LOW);

This code controls four traffic signals, with two signals for each direction. The signals alternate every 40
seconds, with each signal having a green light for 20 seconds and a yellow light for 3 seconds before
changing to red. The Arduino's millis() function is used to keep track of the timing, and the digitalWrite()
function is used to turn the lights on and off. Note that this is a simple example and does not take into
account more complex traffic scenarios or sensors.

Here's an example code for a traffic control system for a four-way junction using an Arduino board:
// Define pins

int northSouthGreen = 2;

int northSouthYellow = 3;

int northSouthRed = 4;

int eastWestGreen = 5;

int eastWestYellow = 6;

int eastWestRed = 7;

// Define time intervals in milliseconds

int greenTime = 10000; // 10 seconds

int yellowTime = 3000; // 3 seconds

void setup() {

// Set the pins as output

pinMode(northSouthGreen, OUTPUT);

pinMode(northSouthYellow, OUTPUT);

pinMode(northSouthRed, OUTPUT);

pinMode(eastWestGreen, OUTPUT);

pinMode(eastWestYellow, OUTPUT);
pinMode(eastWestRed, OUTPUT);

void loop() {

// North-south traffic green, east-west traffic red

digitalWrite(northSouthGreen, HIGH);

digitalWrite(northSouthYellow, LOW);

digitalWrite(northSouthRed, LOW);

digitalWrite(eastWestGreen, LOW);

digitalWrite(eastWestYellow, LOW);

digitalWrite(eastWestRed, HIGH);

delay(greenTime);

// North-south traffic yellow

digitalWrite(northSouthGreen, LOW);

digitalWrite(northSouthYellow, HIGH);

digitalWrite(northSouthRed, LOW);

digitalWrite(eastWestGreen, LOW);

digitalWrite(eastWestYellow, HIGH);

digitalWrite(eastWestRed, HIGH);

delay(yellowTime);

// East-west traffic green, north-south traffic red

digitalWrite(northSouthGreen, LOW);

digitalWrite(northSouthYellow, LOW);
digitalWrite(northSouthRed, HIGH);

digitalWrite(eastWestGreen, HIGH);

digitalWrite(eastWestYellow, LOW);

digitalWrite(eastWestRed, LOW);

delay(greenTime);

// East-west traffic yellow

digitalWrite(northSouthGreen, LOW);

digitalWrite(northSouthYellow, HIGH);

digitalWrite(northSouthRed, HIGH);

digitalWrite(eastWestGreen, LOW);

digitalWrite(eastWestYellow, HIGH);

digitalWrite(eastWestRed, HIGH);

delay(yellowTime);

In this code, the traffic lights for north-south traffic are connected to pins 2, 3, and 4, while the traffic
lights for east-west traffic are connected to pins 5, 6, and 7. The green light for each direction is turned
on for 10 seconds, followed by a 3-second yellow light, and then the green light for the other direction is
turned on for 10 seconds, followed by another 3-second yellow light. This pattern repeats indefinitely in
the loop() function.
Note: This is a basic example and does not include any sensors or pedestrian crossings. For a more
complex traffic control system, additional features and logic would need to be implemented.

Common questions

Powered by AI

The setup function in Arduino programming is crucial for initializing a traffic control system because it configures the environment for the main loop operation. Within the setup function, pin modes are defined (i.e., input or output), which is essential for correctly operating traffic signals . This clear initial setup ensures that the digitalWrite() function can appropriately control signal lights throughout the program. Any required hardware settings, such as initializing communication modules or setting default states for LEDs (all lights red), are also configured in this function, ensuring the system is correctly set up before the main loop begins .

To include pedestrian crossings in the basic traffic control systems, additional features and hardware could be incorporated. This might involve adding separate signals and timers specifically for pedestrian use. Moreover, push buttons could be installed for pedestrians to activate a crossing request. The system would need to be adjusted to recognize these requests, integrating additional decision logic to manage pedestrian lighting phases. The use of sensors or cameras could further enhance the system by ensuring pedestrian safety and detecting their presence at crossings, adapting the timing accordingly .

The use of Arduino's millis() function for traffic light timing allows the program to continue executing other tasks while keeping track of how much time has elapsed. This method is advantageous over traditional delay() methods because delay() halts the execution of the entire program for a specified time, which is inefficient for real-time applications like traffic control . Millis() helps facilitate more dynamic and concurrent operation of different parts of the program, such as handling multiple directions of traffic lights simultaneously .

The primary differences between the two- and four-signal traffic control systems as described in the sources are the number of signals controlled and the timing for signal changes. In the two-signal junction system, each signal has a green light for 10 seconds, a yellow light for 3 seconds before turning red, and alternates every 20 seconds . By contrast, the four-signal system controls two signals per direction, with each green light lasting 20 seconds, followed by 3 seconds of yellow before turning red . Furthermore, the cycle time for the four-signal setup is 40 seconds, providing a more complex setup suitable for more traffic lanes .

The digitalWrite() function is instrumental in the Arduino-based traffic control systems as it directly manages the on/off state of the traffic lights. This function sends either a HIGH or a LOW signal to a specified pin, thereby controlling the electrical state to simulate turning a light on or off . Within these systems, its role is pivotal to effectively alternate between red, yellow, and green lights according to the timing logic in the program. The function’s ability to control multiple outputs efficiently makes it a key component in ensuring the correct operation of traffic sequences across different pins assigned to different signals .

To include traffic light prioritization for emergency vehicles, the existing Arduino code could be modified by integrating a vehicle detection system such as RF transceivers paired with unique identifiers for emergency vehicles. The system would detect these identifiers as vehicles approached the intersection . The Arduino code could then include logic checks that, when triggered by the identified emergency vehicle, preempt normal traffic light cycles, switching greens in its direction to red in other directions to allow a clear path. Additional hardware may be required to verify the emergency status, or use a central control system to manage multiple intersections when vehicles signal entry into a zone .

To scale a traffic control system using Arduino boards for managing multiple intersections, a networked communication protocol could be implemented to allow boards to coordinate traffic signal phases. This could involve using technologies like Zigbee, Bluetooth, or Wi-Fi modules attached to the Arduino boards for inter-board communication. The system could then share real-time data on traffic conditions and synchronize signals to enhance flow across multiple intersections . Centralized control software could be introduced to monitor and modify timings based on traffic data analysis, ensuring efficiency across the network. Such a scalable setup would require developing algorithms that handle data from multiple intersections, adjusting cycles to optimize regional traffic flow .

Integrating sensors into Arduino-based traffic control systems could significantly enhance their functionality by allowing the system to adapt to real-time traffic conditions. For example, infrared sensors could detect the presence of vehicles at an intersection, triggering changes in signal phases to accommodate the flow . Ultrasonic sensors could be used to measure vehicle density or speed, adjusting traffic signals to optimize throughput. Pressure sensors embedded in the road or crosswalk buttons for pedestrian crossing requests could further refine system responsiveness to external demands . These enhancements would make the systems more dynamic and efficient in managing traffic, reducing wait times and congestion.

Adding real-time data analytics to the traffic control system could greatly enhance traffic management by enabling dynamic decision-making based on live conditions. Such analytics could predict traffic patterns, allowing the system to proactively adjust signal timings to minimize congestion . Real-time data could facilitate the identification of traffic anomalies or incidents as they occur, providing an opportunity for immediate mitigation strategies. By integrating historical traffic data, these systems could continuously refine and optimize traffic flow models, improving overall efficiency and reducing waiting times at intersections . This would also support better planning and resource allocation for traffic infrastructure improvements.

The basic traffic control system examples provided may encounter several drawbacks when scaled to more complex intersections. Firstly, they lack the integration of sensors, which are critical for adaptive traffic control that responds to real-time traffic conditions . Secondly, the timing intervals are fixed, which means the system cannot adjust dynamically to changes in traffic flow. Moreover, these systems do not account for pedestrian crossings or emergency vehicle preemption, which are essential features in urban environments. Finally, without networking capabilities, these systems would struggle to coordinate with other intersection controls, leading to inefficiencies and potential traffic congestion in larger networks .

You might also like