Arduino Project: Simple Traffic Light

Home / Arduino Project: Simple Traffic Light

Arduino Project: Simple Traffic Light

December 11, 2015 | Article | 1 Comment

Traffic lights, also known as traffic signals, traffic lamps, signal lights, stop lights, or technically as traffic control signals are signalling device positioned at road intersection, pedestrian crossings, and other locations to control competing flows of traffic.

In this project we will create a simple “traffic light”, imitating what traffic light does and program it using Arduino.


  • Three light switch each other to perform a simple traffic light.
  • Multiple digital signal output.
  • The order of light in one cycle: red – yellow – green – yellow



Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LEDs ( red x1, yellow x1, green x1 )
  • Resistor 10k ohm x3
  • Breadboard

You can change the Arduino board to higher specification if you like.

Because we want to do this as realistic as we can, we will use three different LED: one red LED, one yellow LED, one green LED.

The resistors should be 1/4 Watt resistors with resistance value of 10k ohm. You can use resistors of any band.

You also need an USB A to B cable for upload our program to our Arduino.


Here is the circuit image, created using Fritzing.

Traffic Lamp

We use three LED at once. The first LED is red, the second LED should be yellow, and the last one is green LED. Remember which one is the positive leg and which one is negative leg (anode and cathode) for each LED. Connect attach the long leg of an LED (positive leg or anode) with resistor and then connect it to corresponding pin. Red one will use pin 12, yellow will be in 11, and green would use pin 10. Connect the short leg (negative leg or cathode) to the ground (GND).


Let’s see what code we use for this project and discuss it later.

#define RED_PIN 12
#define YELLOW_PIN 11
#define GREEN_PIN 10

  // timeout for each LED in second
int timeout[3] = {10, 1, 20};
int currentLED = 0;
int alternate = -1;

int enumLED[3] = { RED_PIN, YELLOW_PIN, GREEN_PIN };

void setup() {
  pinMode(RED_PIN, OUTPUT);

void loop() {
  digitalWrite(enumLED[currentLED], LOW);
  if (currentLED == 0) {
    alternate = 1;
  } else if (currentLED == 2) {
    alternate = -1;
  currentLED = currentLED + alternate;
  digitalWrite(enumLED[currentLED], HIGH);

  delay(timeout[currentLED] * 1000);

We define three constant here: RED_PIN, YELLOW_PIN, and GREEN_PIN for red, yellow, and green LED respectively. However this time we only define it using macro not as constant. Then we create an array of LED as enumLED. This array will be our shortcut to switch between LED. For indexing we use currentLED variable.

The timeout array is an array which stored delay interval for each LED in seconds. This means our lights won’t turned on in same duration.

The alternate variable is used to increment the currentLED index. Remember that in one cycle, we define the LED switching as red – yellow – green – yellow.


LED is turned on in following order:

  • red for 10 seconds
  • yellow for 1 second
  • green for 20 seconds
  • yellow for 1 second
  • red for 10 seconds at another cycle.

Why it Works

Our code is as simple as turned off current LED and turned on the next LED. The previous, current, and next LED are defined in an array and we use currentLED variable as an index. The alternate is used to increase or decrease the index and make our LED obey our pattern.


About Author

about author


A man who is obsessed to low level technology.

1 Comment
  1. Kanav german

    It was very nicely done

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Share Buttons and Icons powered by Ultimatelysocial