The Arduino traffic light is a fun little project that you can build in less than an hour. Here’s how to build your own using the Arduino, and how to modify the circuit for an advanced version.
If you’d rather watch this tutorial as a video, we’ll walk you through:

What you need to build an Arduino traffic light controller
In addition to the basic Arduino, you will need:
- 1 x 10 kΩ resistor
- 1 x push button switch
- 6 x 220 ohm resistors
- Layout
- Connecting wires
- Red, yellow and green LEDs
Almost any Arduino will work for this project, as long as it has enough pins. Be sure to read our buying guide if you are not sure which model you need. You may already have these parts in your starter kit Arduino is included in your starter kit
Arduino Traffic Light: The Basics
Let’s start small. A basic, single traffic light is a good place to start. Here is the diagram:
Connect the anode (long leg) of each LED to digital pins 8, 9, and 10 (through a 220 ohm resistor). Connect the cathodes (short leg) to Arduino ground.
Arduino traffic light code
Start by defining variables so you can refer to lights by name instead of number. Start a new Arduino project and start with these lines:
int red = 10; int yellow = 9; int green = 8;
Next, let’s add a customization function where you’ll customize the red, yellow, and green LEDs for the output. Since you’ve created variables to represent contact numbers, you can now refer to contacts by name:
void setup(){ pinMode(red, OUTPUT); pinMode(yellow, OUTPUT); pinMode(green, OUTPUT); }
Function pinMode configures the Arduino to use the given pin as an output. You must do this in order for your LEDs to work at all. Now for the actual traffic light logic. Here is the code you need. Add this below your function definition and setting variables:
void loop(){ changeLights(); delay(15000); } void changeLights(){ // green off, yellow on for 3 seconds digitalWrite(green, LOW); digitalWrite(yellow, HIGH); delay(3000); // turn off yellow, then turn red on for 5 seconds digitalWrite(yellow, LOW); digitalWrite(red, HIGH); delay(5000); // red and yellow on for 2 seconds (red is already on though) digitalWrite(yellow, HIGH); delay(2000); // turn off red and yellow, then turn on green digitalWrite(yellow, LOW); digitalWrite(red, LOW); digitalWrite(green, HIGH); delay(3000); }
Upload this code to Arduino and run (make sure you select the correct board and port in the menu Tools > Fee and Tools > Menu ports ). You should have a working traffic light that changes every 15 seconds, like this (accelerated):
Let’s take a look at this code. Function changeLights does all the hard work. This turns the traffic light to yellow and red and then back to green. Since this is called inside a function cycle the Arduino will execute this code forever with a 15 second pause each time.
Function changeLights consists of four separate steps:
- Green on, yellow off
- Yellow off, red on
- yellow to red
- Green on, red off, yellow off
These four steps repeat the process used in real traffic lights. For each step, the code is very similar. The corresponding LED is switched on or off with digitalWrite . This is an Arduino function used to set the output pins to HIGH (to turn on) or LOW (to turn off).
After turning on or off the required LEDs, delay causes the Arduino to wait a certain amount of time. Three seconds in this case.
Going Deeper: The Arduino Crosswalk
Now that you know the basics, let’s improve this. Add a button for pedestrians to change the light whenever they like: