How to make Traffic lights with Arduino uno

 This project implements a traffic light system with a countdown timer, designed using an Arduino microcontroller. It controls a sequence of traffic lights (red, yellow, and green) and a 7-segment display counter. Here’s a detailed description:

Features:

  1. Traffic Light Phases:

    • Red Light Phase: The red light is active for 9 seconds, during which a countdown is displayed on a 7-segment display.
    • Yellow Light Phase: The yellow light blinks for 2 seconds before switching to green.
    • Green Light Phase: The green light is active for 9 seconds, with the countdown displayed.
  2. Countdown Display:

    • A 7-segment display shows the countdown from 9 to 0 during the red and green light phases. This is achieved using a 4511 IC, with the display driven by 4 output pins.

Components:

  1. Traffic Light LEDs:
    • Red, yellow, and green LEDs are connected to digital pins 11, 12, and 13 respectively.
    • Controlled using digitalWrite to turn them on or off.
  2. 7-Segment Display:
    • Driven by a 4511 IC, connected to pins 4, 5, 6, and 7.
    • Displays the countdown timer during the active phases of red and green lights.

Workflow:

  1. Setup Phase:

    • Configures the pins for the LEDs and the 7-segment display.
    • Initializes the system with the red light on and the counter set to 9.
  2. Loop Phase:

    • Executes the traffic light phases in sequence:
      • Red Light Phase: Lights the red LED and counts down from 9 to 0.
      • Yellow Light Phase: Blinks the yellow LED for 2 seconds.
      • Green Light Phase: Lights the green LED and counts down from 9 to 0.
    • Repeats the cycle continuously.

Schematic:
Schematic



Code:
// Pin Definitions
#define GREEN_LIGHT 13
#define YELLOW_LIGHT 12
#define RED_LIGHT 11

const int counterPins[] = {7, 6, 5, 4}; // Pins connected to 4511 (7-segment IC)

// Variables
int counter = 9; // Start countdown from 9

void setup() {
  // Setup Traffic Light Pins
  pinMode(GREEN_LIGHT, OUTPUT);
  pinMode(YELLOW_LIGHT, OUTPUT);
  pinMode(RED_LIGHT, OUTPUT);

  // Setup Counter Pins
  for (int i = 0; i < 4; i++) {
    pinMode(counterPins[i], OUTPUT);
  }

  // Initialize with Red Light and Counter at 9
  digitalWrite(GREEN_LIGHT, LOW);
  digitalWrite(YELLOW_LIGHT, LOW);
  digitalWrite(RED_LIGHT, HIGH);
  updateCounter(counter);
}

void loop() {
  // Red Light Phase
  digitalWrite(RED_LIGHT, HIGH);
  digitalWrite(YELLOW_LIGHT, LOW);
  digitalWrite(GREEN_LIGHT, LOW);
  countdown(9); // Countdown from 9 to 0 (9 seconds)

  // Yellow Light Phase (Blinking for 2 seconds)
  for (int i = 0; i < 4; i++) { // Blinks 4 times (2 seconds)
    digitalWrite(YELLOW_LIGHT, HIGH);
    delay(250);
    digitalWrite(YELLOW_LIGHT, LOW);
    delay(250);
  }

  // Green Light Phase
  digitalWrite(RED_LIGHT, LOW);
  digitalWrite(YELLOW_LIGHT, LOW);
  digitalWrite(GREEN_LIGHT, HIGH);
  countdown(9); // Countdown from 9 to 0 (9 seconds)

  // Loop starts again
}

// Update the counter value on the 4511 IC
void updateCounter(int value) {
  for (int i = 0; i < 4; i++) {
    digitalWrite(counterPins[i], (value >> i) & 0x01);
  }
}

// Countdown logic
void countdown(int startValue) {
  counter = startValue;
  while (counter >= 0) {
    updateCounter(counter);
    delay(1000); // 1 second delay for each count
    counter--;
  }
}


Code Highlights:

  • Pin Definitions: Predefined constants improve code readability by labeling the pin numbers.
  • Modular Functions:
    • updateCounter(int value): Updates the display based on the counter value.
    • countdown(int startValue): Handles the countdown logic with a 1-second delay for each decrement.
  • Timing Control: Uses delay() for time intervals, providing precise timing for LED and countdown changes.

Applications:

This project can be used as:

  • A simulation of a basic traffic light system for educational purposes.
  • A starting point for more complex traffic management systems.

Comments