DEV Community

Cover image for Traffic Light Simulator - Arduino
Ochwada Linda
Ochwada Linda

Posted on


Traffic Light Simulator - Arduino

Simulating a traffic light using LEDs and Arduino, I did the project with my 8-year-old son! He found it so cool.


  • Arduino UNO
  • Breadboard (Generic)
  • Jumper Wires (Generic)
  • LED (Red, Green, Yellow)
  • Arduino USB 2.0 Data Cable
  • Resistor 100 ohm

Image set-up

IDE used: Arduino IDE

This simple "for beginners " uses an Arduino and LEDs to simulate a traffic light.

Define variables, use lights by colour name:

// variables
int red = 10;
int yellow = 9;
int green = 8;
Enter fullscreen mode Exit fullscreen mode

Create a function to configure the output colours of the LEDs (Red, Yellow, Green):

void setup(){
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);
Enter fullscreen mode Exit fullscreen mode

This pinMODE function, configures the Arduino to use a defined pin as an output.

Write the code for the actual logic behind the traffic light simulation. The given values are in milliseconds

void loop(){
void changeLights(){
    // green off, yellow on for 3 seconds
    digitalWrite(green, LOW);
    digitalWrite(yellow, HIGH);

    // turn off yellow, then turn red on for 5 seconds
    digitalWrite(yellow, LOW);
    digitalWrite(red, HIGH);

    // red and yellow on for 2 seconds (red is already on though)
    digitalWrite(yellow, HIGH);

    // turn off red and yellow, then turn on green
    digitalWrite(yellow, LOW);
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
Enter fullscreen mode Exit fullscreen mode

Upload the above code to your Arduino and run (make sure the right board and port are selected from Tools menu)

Top comments (0)