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:

Schematic diagram of a basic Arduino traffic light

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):

Arduino traffic light in action

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:

Schematic diagram of the Arduino crosswalk

Note that the traffic light is exactly the same as in the previous example. Connect a button to digital pin 12. You will notice that there is a 10 kΩ high-resistance resistor connected to the switch, and you may be wondering why. This is a pull down resistor.

A switch either passes current or it doesn’t. This seems simple enough, but in a logic circuit, current must always flow in a high or low state (remember, 1 or 0, HIGH or LOW). It can be assumed that a button that is not actually pressed will be in a LOW state, but it is actually called «floating» because no current is being applied at all.

In this floating state, a false reading is possible as it oscillates with electrical noise. In other words, the floating switch does not give a reliable HIGH or LOW reading. The pull-down resistor holds a small amount of current flowing when the switch closes, thus providing an accurate low reading.

In other logic circuits, you may find a pull-up resistor instead, and this works on the same principle, but in reverse, ensuring that a particular logic element is high by default.

Now, in the looping part of the code, instead of changing the light every 15 seconds, you will instead read the state of the button switch and change the light only when it is activated.

Arduino crosswalk code

Start by adding a new variable to hold your button:

 int button = 12; // switch is on pin 12 

Now, in the setup function, add a new line to declare the radio button as an input. Add a line to set the traffic light on the green scene. Without this initial setup, they would turn off before the first launch changeLights .

 pinMode(button, INPUT); digitalWrite(green, HIGH); 

Instead, change the whole loop function like this:

 void loop() { if (digitalRead(button) == HIGH){ delay(15); // software debounce if (digitalRead(button) == HIGH) { // if the switch is HIGH, ie. pushed down - change the lights! changeLights(); delay(15000); // wait for 15 seconds } } } 

It should do it. You can ask, why is the button validation happening twice( digitalRead(button) ) separated by a small delay. It exposes. Like the button resistor, this simple check stops code detecting minor noise when the button is pressed.

Waiting at the operator if in within 15 seconds, the traffic light cannot change at least during this time. After 15 seconds, the cycle resumes. Each time the loop restarts, it reads the state of the button again, but if it is not pressed, the statement if never activates, the indicators never change, and the program restarts again.

Here’s what it looks like (speeded up):

Arduino crosswalk in action

Arduino traffic light with crossroad

Let’s try a more advanced model. Instead of a pedestrian crossing, change the scheme to two traffic lights:

Schematic diagram for Arduino traffic lights with decoupling

Connect a second traffic light to digital pins 11, 12 and 13.

Code for Arduino traffic light with crossroads

First, assign your new traffic light pins to variables and set them up as output, as in the first example:

 // light one int red1 = 10; int yellow1 = 9; int green1 = 8; // light two int red2 = 13; int yellow2 = 12; int green2 = 11; void setup(){ // light one pinMode(red1, OUTPUT); pinMode(yellow1, OUTPUT); pinMode(green1, OUTPUT); // light two pinMode(red2, OUTPUT); pinMode(yellow2, OUTPUT); pinMode(green2, OUTPUT); } 

Now update your loop to use the code from the first example (instead of the pedestrian crossing):

 void loop(){ changeLights(); delay(15000); } 

Once again, all the work is done in the function changeLights . Instead of going to red > red and yellow > green this code alternates traffic lights. When one is on green, the other is on red. Here is the code:

 void changeLights(){ // turn both yellows on digitalWrite(green1, LOW); digitalWrite(yellow1, HIGH); digitalWrite(yellow2, HIGH); delay(5000); // turn both yellows off, and opposite green and red digitalWrite(yellow1, LOW); digitalWrite(red1, HIGH); digitalWrite(yellow2, LOW); digitalWrite(red2, LOW); digitalWrite(green2, HIGH); delay(5000); // both yellows on again digitalWrite(yellow1, HIGH); digitalWrite(yellow2, HIGH); digitalWrite(green2, LOW); delay(3000); // turn both yellows off, and opposite green and red digitalWrite(green1, HIGH); digitalWrite(yellow1, LOW); digitalWrite(red1, LOW); digitalWrite(yellow2, LOW); digitalWrite(red2, HIGH); delay(5000); } 

Here’s what it looks like (speeded up):

Arduino traffic light with crossroads in action

Arduino Traffic Light Next Steps

It’s all for today. Your new understanding of Arduino LEDs and buttons applies to all different projects. If you want to expand those traffic lights, why not build a four-way (or more) intersection with lots of crosswalks and traffic lights?

Or why not expand your new skills with a bigger project like this DIY MIDI controller or an Arduino robot with Xod? You can also take a look at Arduino coding using VS Code and PlatformIO.

Image credit: androsvector/Shutterstock

Похожие записи