Author's Picture
Author: Joel Gray Published: 7 July 2020 Read Time: ~3 minutes

Traffic Light LEDs using Arduino

How to Make LEDs Light Up in a specific Sequence like Traffic Lights

Here we have an example of how using 3 different coloured LEDs and the delay function, we can mimic the operation of a traffic light system.

This project can be easily edited using more LEDs, different coloured LEDs and different
delays to create more unique sequences. A more advanced project which we will post in the future will be motion sensor traffic lights.

  • Traffic Lights Diagram
  • Traffic Lights Side View
  • Traffic Lights Top View
Links to what you’ll need:

1x Breadboard

1x Arduino Uno

3x 330 Ohm Resistor

3x LED (Green, Amber, Red)

4x Jumper Wires

1x USB-A Cable

How do I start?

The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.

The positive led of the Green LED is connected to Pin 13 via a current limiting resistor and the negative led of the Green LED is connected to the Ground Pin via a jumper wire.

This is the same for the Amber and Red LEDs. You can see how they share the same ground connection, this is perfectly fine to do and is encouraged to reduce wiring.

A quick summary of the circuit can be seen below:

Pin 11 > Resistor > Red LED > Ground Rail

Pin 11 > Resistor > Amber LED > Ground Rail

Pin 11 > Resistor > Green LED > Ground Rail

Ground Rail on Breadboard > Jumper Wire > Ground Pin


Let’s run through what the code is doing

We initialise the Pins that the LEDs are connected to. Eg. ‘int led1 = 13;’

Inside the Setup function we use the pinMode function to set our Pins to which the LEDs are connected to be Outputs. Then inside the main loop function we are using the digitalWrite function to write each LED either HIGH or LOW. Essentially turning them ON or OFF.

Also utilising the delay function to pause the program and keep the current state of the program/circuit static for a specific amount of time.

Check out the information given on Arduino documentation to learn more.


Now open the Arduino IDE and copy the code below

If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.

  
// This simple program uses the void loop() to run a sequence of LEDs using delays and the HIGH/LOW variables

int led1 = 13;               // Red
int led2 = 12;               // Yellow
int led3 = 11;               // Green 


void setup() {

  // Setting all LEDs as OUTPUTs
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  
}

void loop() {

  // Turning pins HIGH for a specified time and then LOW until the loop restarts
  digitalWrite(led1,HIGH);
  delay(3000);
  
  digitalWrite(led1,LOW);
  delay(1000);
  
  digitalWrite(led2,HIGH);
  delay(3000);

  digitalWrite(led2,LOW);
  delay(1000);
  
  digitalWrite(led3,HIGH);
  delay(3000);

  digitalWrite(led3,LOW);
  delay(1000);
  
}

/*
This code can be edited to include more LEDs running in different sequences. 
This is a simple 'traffic light' sequence.
*/


/*
  created 02/2020
  by Jamie Buick 

  graycode.ie
*/