How to Create a Traffic Light Program with Arduino

A traffic light is a three-light signaling device that controls the flow of traffic. The three lights are red, yellow, and green, and they are arranged vertically or horizontally. In this program, we will use the Arduino board to simulate a traffic light system.

  • Connect three LEDs to digital pins on the Arduino board. For example, connect the red LED to pin 2, the yellow LED to pin 3, and the green LED to pin 4.
  • Here is the program to simulate a traffic light:

This program uses the pinMode() function to set the three LED pins as outputs. The loop() function contains several digitalWrite() statements that turn the LEDs on and off according to the traffic light sequence.

When you upload this program to your Arduino board, the red LED should turn on for 5 seconds, followed by the yellow LED for 2 seconds, then the green LED for 5 seconds, and finally the yellow LED for 2 seconds before the sequence repeats. This simulates the traffic light sequence seen at many intersections.