Traffic Light System Simulation with LEDs

 

What You'll Learn

In this project, we’ll simulate a basic traffic light system using three LEDs: red, yellow, and green. This simple project will help you understand how to control multiple LEDs using timed intervals to mimic the behavior of a real-world traffic light.

Components Needed

  • 1 x Arduino Uno (or compatible board)
  • 1 x Red LED
  • 1 x Yellow LED
  • 1 x Green LED
  • 3 x Resistors (220 ohms)
  • Jumper wires
  • Breadboard

The Concept

The traffic light system works on a timed sequence where:

  • Red LED (Stop) lights up for a specific duration.
  • Yellow LED (Wait) lights up for a shorter duration.
  • Green LED (Go) lights up for a specific duration.

We’ll use the Arduino to control these LEDs, turning them on and off in timed intervals.

Circuit Diagram

Here's how to wire the components:

  1. Connect the Red LED anode (long leg) to pin 13 and the cathode (short leg) to one end of a resistor. Connect the other end of the resistor to the ground (GND).
  2. Connect the Yellow LED anode to pin 12 and the cathode to a resistor, which is then connected to GND.
  3. Connect the Green LED anode to pin 11 and the cathode to a resistor, which is then connected to GND.



The Code

// Define pin numbers for the LEDs const int redLED = 13; const int yellowLED = 12; const int greenLED = 11; // Define durations (in milliseconds) for each LED state const int redLightDuration = 5000; // Red for 5 seconds const int yellowLightDuration = 2000; // Yellow for 2 seconds const int greenLightDuration = 5000; // Green for 5 seconds void setup() { // Initialize LED pins as outputs pinMode(redLED, OUTPUT); pinMode(yellowLED, OUTPUT); pinMode(greenLED, OUTPUT); } void loop() { // Red light ON, others OFF digitalWrite(redLED, HIGH); digitalWrite(yellowLED, LOW); digitalWrite(greenLED, LOW); delay(redLightDuration); // Keep red light ON for the duration // Yellow light ON, others OFF digitalWrite(redLED, LOW); digitalWrite(yellowLED, HIGH); digitalWrite(greenLED, LOW); delay(yellowLightDuration); // Keep yellow light ON for the duration // Green light ON, others OFF digitalWrite(redLED, LOW); digitalWrite(yellowLED, LOW); digitalWrite(greenLED, HIGH); delay(greenLightDuration); // Keep green light ON for the duration }

How the Code Works

  1. Pin Setup: The pinMode() function in setup() initializes the pins connected to the LEDs as output pins.

  2. Timed Sequences: The digitalWrite() function turns on and off the LEDs by sending a HIGH or LOW signal to the corresponding pin. The delay() function pauses the code for the specified duration, creating the timed intervals that control how long each light stays on.

  3. The Loop: The loop() function runs continuously, cycling through the red, yellow, and green LED states, with each light staying on for the specified time.

Traffic Light Behavior

  • The red LED lights up first, mimicking the stop signal.
  • After 5 seconds, the yellow LED lights up to signal waiting.
  • Then, the green LED lights up, allowing traffic to proceed.
  • The cycle repeats indefinitely.

Customizing the Project

  • Change Durations: Adjust the redLightDuration, yellowLightDuration, and greenLightDuration variables to modify how long each light stays on.
  • Add More Features: You can add pedestrian buttons, sound alerts, or even a countdown timer to make the project more complex.

Conclusion

This simple traffic light simulation introduces you to working with multiple LEDs and timed sequences using an Arduino. It's a great project for beginners to get a feel for how real-world systems can be controlled with simple code and basic components.

Comments

Popular Posts