Traffic Light Model (Arduino) With Sensors

by mrbrightedu in Circuits > Arduino

25 Views, 1 Favorites, 0 Comments

Traffic Light Model (Arduino) With Sensors

IMG_1006.jpeg
Drawing 6.png

This project is an example of a simple two-direction traffic light system with a timer and two sensors. Traffic light systems are also a common state machine/diagram example. This project helps explore the state concept of system design. You will also learn how to integrate input actions (e.g. button press) alongside a timer.

Review the state diagram to understand the four possible light configurations and how we get to the next state, either by time, or by a button press. This button press could represent a sensor to detect a car on the road, or a pedestrian crosswalk.




Supplies

Arduino Components:

  1. Arduino Uno or compatible board (1x)
  2. USB cable for programming and power (1x)

.LEDs:

  1. Green LEDs (2x)
  2. Yellow LEDs (2x)
  3. Red LEDs (2x)

Resistors:

  1. 220Ω - 1KΩ resistors for LEDs (6x)

Push Buttons:

  1. Momentary push buttons (2x)

Wires and Connectors:

  1. Male-to-male jumper wires (10x)
  2. Breadboard (1x)

Install LEDs

Traffic Light_bb.png

As noted in the video, bend each LED such that the longer leg (positive) is bent to make it easier to place on the board.

Insert the six LEDs into the breadboard as per the diagram below.

Each LED is powered by a PIN, through a RESISTOR, and back to GROUND.

Take the time and care to position the components neatly and consistently. The more effort you put into designing an organized project, the more successful your project will become!

Install Buttons

Traffic Light_bb.png

Install the two buttons as shown. Remember to space them evenly and consistently.

Don't forget the two connections to the ground rail.


Downloads

Upload Arduino Sketch


Use the code attached here to configure the board. Open this sketch as a new project. Be sure to select the Arduino Uno board communication port specific to your hardware in the IDE.

Please upload when you have successfully connected and selected the Arduino board in the IDE.

Review the code comments for a description of each section and function.


// Pin definitions
int G1 = 13; // Green LED for light set 1
int Y1 = 12; // Yellow LED for light set 1
int R1 = 11; // Red LED for light set 1
int G2 = 10; // Green LED for light set 2
int Y2 = 9; // Yellow LED for light set 2
int R2 = 8; // Red LED for light set 2

int button1 = 7; // Button for light set 1
int button2 = 6; // Button for light set 2

// Timing intervals (in milliseconds)
unsigned long greenTime = 10000; // Green light duration
unsigned long yellowTime = 1000; // Yellow light duration

unsigned long lastChange = 0; // Tracks the last state change time
int state = 0; // Traffic light state (0: NS green, 1: NS yellow, 2: EW green, 3: EW yellow)

bool button1Pressed = false; // Flags for button press
bool button2Pressed = false;

void setup() {
Serial.begin(9600);
// Set LED pins as outputs
pinMode(G1, OUTPUT);
pinMode(Y1, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(G2, OUTPUT);
pinMode(Y2, OUTPUT);
pinMode(R2, OUTPUT);

// Set button pins as inputs with pull-up resistors
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);

// Initialize lights
digitalWrite(G1, HIGH);
digitalWrite(Y1, LOW);
digitalWrite(R1, LOW);
digitalWrite(G2, LOW);
digitalWrite(Y2, LOW);
digitalWrite(R2, HIGH);

lastChange = millis();
}

void loop() {
unsigned long currentTime = millis();

// Read button states (active low)
if (digitalRead(button1) == LOW) {
button1Pressed = true;
Serial.println("NS Button Press");
}
if (digitalRead(button2) == LOW) {
button2Pressed = true;
Serial.println("EW Button Press");
}

switch (state) {
case 0: // NS Green, EW Red
Serial.println("NS Green, EW Red");
if (currentTime - lastChange >= greenTime || button2Pressed) {
Serial.println("Transition to NS Yellow");
digitalWrite(G1, LOW);
digitalWrite(Y1, HIGH);
state = 1;
lastChange = currentTime;
button2Pressed = false; // Reset the button flag
}
break;

case 1: // NS Yellow, EW Red
Serial.println("NS Yellow, EW Red");
if (currentTime - lastChange >= yellowTime) {
Serial.println("Transition to EW Green");
digitalWrite(Y1, LOW);
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(G2, HIGH);
state = 2;
lastChange = currentTime;
}
break;

case 2: // NS Red, EW Green
Serial.println("NS Red, EW Green");

if (currentTime - lastChange >= greenTime || button1Pressed) {
Serial.println("Transition to EW Yellow");
digitalWrite(G2, LOW);
digitalWrite(Y2, HIGH);
state = 3;
lastChange = currentTime;
button1Pressed = false; // Reset the button flag
}
break;

case 3: // NS Red, EW Yellow
Serial.println("NS Red, EW Yellow");

if (currentTime - lastChange >= yellowTime) {
Serial.println("Transition to NS Green");
digitalWrite(Y2, LOW);
digitalWrite(R2, HIGH);
digitalWrite(R1, LOW);
digitalWrite(G1, HIGH);
state = 0;
lastChange = currentTime;

}
break;
}
button1Pressed = false; // Reset the button flag
button2Pressed = false; // Reset the button flag

}