Traffic Light Controller With Arduino

by JamesDaviss in Circuits > Arduino

464 Views, 2 Favorites, 0 Comments

Traffic Light Controller With Arduino

Traffic Light Controller with Arduino.jpg

In this project, you'll simulate a traffic light with LEDs with the Arduino, teaching you about timing and sequencing. This is a great way to understand how to control multiple outputs in a coordinated manner.

Supplies

Components you'll need:

  1. Arduino board (e.g., Arduino Uno)
  2. 3 LEDs (Red, Yellow, and Green)
  3. 220-ohm resistors (3)
  4. Breadboard and jumper wires

Set Up Your Circuit

  • Start by placing your Arduino board next to the breadboard.
  • Insert the Red, Yellow, and Green LEDs into the breadboard. Ensure that the cathodes (shorter leads) of the LEDs are connected to a common ground rail on the breadboard.
  • Connect the anodes (longer leads) of the Red, Yellow, and Green LEDs to the breadboard as follows:
  • Insert the anode of the Red LED into a hole in a row on the breadboard.
  • Connect a 220-ohm resistor to the same row as the Red LED's anode.
  • Connect the other end of the resistor to a ground rail on the breadboard.
  • Repeat this process for the Yellow and Green LEDs, each with their own resistor.
  • Connect jumper wires from the other ends of the LED anodes (the holes in the rows) to the digital pins on the Arduino board (e.g., Red to Pin 2, Yellow to Pin 3, Green to Pin 4).
  • Ensure that the jumper wire connecting to each LED's anode is placed in the same row as the corresponding resistor's free end.

It is important to connect resistors in series to the LEDs to prevent them from over-intensifying and burning out. This is because, when current flows through any component connected to the circuit, some resistance heat is generated. And by adding the resistor, we smooth it out.

This setup allows you to control the LEDs using the Arduino while protecting them with current-limiting resistors. Now, you can proceed to the coding and logic part of the project.

Code the Traffic Light Logic

  • Write an Arduino sketch (program) that controls the LEDs to mimic a traffic light.
  • You'll need to define the pin modes in the setup() function (e.g., pinMode(2, OUTPUT); for the Red LED).
  • In the loop() function, create a sequence that turns on and off the LEDs in the order of Red -> Red + Yellow -> Green -> Yellow.
  • Add delays between each state to control the timing of the traffic light cycle.

Here's a simplified example of the Arduino code:

void setup() {
  pinMode(2, OUTPUT);  // Red LED
  pinMode(3, OUTPUT);  // Yellow LED
  pinMode(4, OUTPUT);  // Green LED
}


void loop() {
  digitalWrite(2, HIGH);  // Red on
  delay(3000);           // Stay red for 3 seconds
  digitalWrite(2, LOW);   // Red off


  digitalWrite(3, HIGH);  // Yellow on
  delay(1000);           // Stay yellow for 1 second
  digitalWrite(3, LOW);   // Yellow off


  digitalWrite(4, HIGH);  // Green on
  delay(3000);           // Stay green for 3 seconds
  digitalWrite(4, LOW);   // Green off
}

Upload and Run Your Code

  • Connect your Arduino to your computer and upload the code using the Arduino IDE.
  • Once uploaded, you'll see the LEDs simulate a traffic light sequence.

This project introduces you to the basics of Arduino coding, digital outputs, and timing control. You can further enhance the project by adding a pedestrian crossing button, pedestrian walk signal, or even simulating real traffic light timing for a specific intersection.