Arduino Traffic Light Project — Step-by-Step Guide

Build a two-way traffic light with Arduino Nano and two traffic signal modules. Includes wiring, full code and how the light sequence works.

  • 10 min read
On this page1. Introduction

1. Introduction

For this project, I wanted to build a simple traffic light system using an Arduino Nano and two traffic signal modules. It’s a small electronics project, but it gives you a good idea of how a microcontroller can control multiple LEDs in a specific sequence.

I connected two traffic light modules to my Arduino Nano and programmed them to work like a basic road intersection. One signal turns green while the other stays red. After a few seconds, the green light changes to yellow, and then both signals turn red before the second traffic light gets its turn.

I used an Arduino Nano because it’s compact and has enough digital pins for this setup. The wiring is also quite simple, so you don’t need a lot of extra components to get it working.

I built this as an electronics demonstration project. You can use a similar setup for a school science project, a STEM activity, or just to experiment with Arduino.

I’ve included the components, wiring connections, code, and the steps I followed to test the project.

Let’s start with the components I used.

2. Components Required

These are the components I used to put together the dual traffic light system.

ComponentQuantity
Arduino Nano1
Three-color traffic light signal module2
Compatible USB cable1
Male-to-male jumper wires6–12
Breadboard (optional)1
Computer with Arduino IDE1

Where to buy the components

Affiliate Disclosure: This article may contain affiliate links. As an Amazon Associate, I earn from qualifying purchases. If you purchase through these links, I may earn a commission at no additional cost to you.

Before You Start

Before connecting the modules, check their pin labels. The traffic light modules I have used for this wiring setup have separate red, yellow, and green inputs along with a common GND connection.

One thing worth checking is whether your module has built-in current-limiting resistors. Some modules do, while others may need external resistors.

The connections and sketch in this tutorial assume that the modules use active-HIGH inputs and are compatible with the Arduino Nano’s 5V logic. If your module works with active-LOW signals, you’ll need to adjust the code.

I recommend checking the module’s specifications before connecting everything.

3. How the Traffic Light Module Works

A traffic light module is basically a small board with three LEDs: red, yellow, and green. Each LED represents a different traffic signal.

  • Red: Stop.
  • Yellow: Prepare for the next signal change.
  • Green: Go.

Each LED has its own control input. By connecting these inputs to the Arduino Nano’s digital pins, I can turn the LEDs on and off whenever I want.

How I Made Two Traffic Lights Work Together

The Arduino Nano runs a sketch that controls all six LED inputs.

For my setup, I programmed the signals to work in the following order:

  1. Traffic Light 1 turns green, while Traffic Light 2 stays red.
  2. Traffic Light 1 changes to yellow.
  3. Both traffic lights turn red for a short time.
  4. Traffic Light 2 turns green, while Traffic Light 1 stays red.
  5. Traffic Light 2 changes to yellow.
  6. Both signals turn red again, and the sequence starts over.

This sequence keeps repeating as long as the Arduino Nano is powered.

Arduino Nano Specifications

I used a classic Arduino Nano based on the ATmega328P for this project.

SpecificationClassic Arduino Nano
MicrocontrollerATmega328P
Operating logic voltage5V
Digital I/O pins14
PWM-capable pins6
Analog input pins8
Recommended VIN input7–12V
Programming languageArduino C/C++

Specifications can vary depending on the Nano version you’re using.

Why I Used Arduino Nano

I chose the Arduino Nano mainly because of its size. It fits easily on a breadboard and doesn’t take up much space in the project setup.

I also didn’t need a larger board for this project. The Nano has enough digital pins to control both traffic light modules individually, which made it a convenient choice.

5. Wiring the Arduino Nano to Two Traffic Light Modules

The wiring is one of the easiest parts of this project. Each traffic light module needs three digital output pins from the Arduino Nano.

I connected the first traffic light to D2, D3, and D4. For the second module, I used D5, D6, and D7.

Here are the connections I used.

Traffic Light 1 Connections

Traffic Light ModuleArduino Nano
Red LED inputD2
Yellow LED inputD3
Green LED inputD4
GNDGND

Traffic Light 2 Connections

Traffic Light ModuleArduino Nano
Red LED inputD5
Yellow LED inputD6
Green LED inputD7
GNDGND

Wiring Notes

A few things I checked while making the connections:

  1. I disconnected the USB cable before changing the wiring.
  2. Both traffic light modules share the Arduino Nano’s GND.
  3. I checked the labels on each module before connecting the wires.
  4. I verified whether the modules had built-in resistors.
  5. I made sure the LED current was within the Arduino’s output-pin limits.
  6. I checked whether the modules used active-HIGH or active-LOW inputs.

If your modules require more current than the Arduino pins can safely provide, use suitable driver circuitry.

Safety note: This is an educational traffic signal model. It is not a certified traffic controller and must never be used to control actual road traffic.

6. Arduino IDE Setup

I didn’t need to install any additional libraries for this project. The sketch uses basic Arduino functions such as pinMode(), digitalWrite(), and delay().

Here is how I prepared the Arduino Nano for uploading the code.

  1. Download and install the Arduino IDE from the official website: https://www.arduino.cc/en/software
  2. Connect the Arduino Nano to your computer using a USB cable.
  3. Open the Arduino IDE.
  4. Select Tools → Board → Arduino AVR Boards → Arduino Nano (for a classic Nano).
  5. Select the correct COM port under Tools → Port.
  6. If you’re using a classic Nano clone, check the processor setting. Some older boards require ATmega328P (Old Bootloader).
  7. Create a new sketch and paste the code below.
  8. Click Verify to check whether the sketch compiles.

Once the code compiles without errors, you can upload it to the Arduino Nano.

7. Arduino Nano Dual Traffic Light Code

This is the sketch I used to control both traffic light modules.

I kept the pin definitions and timing values at the beginning of the code so they’re easy to find. If you want to change how long the green or yellow light stays on, you can modify those values without changing the rest of the program.

Code
1// Arduino Nano Dual Traffic Light Project2 3// Traffic Light 14const int RED_1    = 2;5const int YELLOW_1 = 3;6const int GREEN_1  = 4;7 8// Traffic Light 29const int RED_2    = 5;10const int YELLOW_2 = 6;11const int GREEN_2  = 7;12 13// Timing values in milliseconds14const unsigned long GREEN_TIME   = 5000;15const unsigned long YELLOW_TIME  = 2000;16const unsigned long ALL_RED_TIME = 1000;17 18// Turn all LEDs off19void allLightsOff() {20  digitalWrite(RED_1, LOW);21  digitalWrite(YELLOW_1, LOW);22  digitalWrite(GREEN_1, LOW);23 24  digitalWrite(RED_2, LOW);25  digitalWrite(YELLOW_2, LOW);26  digitalWrite(GREEN_2, LOW);27}28 29// Traffic Light 1 green, Traffic Light 2 red30void trafficLight1Green() {31  allLightsOff();32 33  digitalWrite(GREEN_1, HIGH);34  digitalWrite(RED_2, HIGH);35}36 37// Traffic Light 1 yellow, Traffic Light 2 red38void trafficLight1Yellow() {39  allLightsOff();40 41  digitalWrite(YELLOW_1, HIGH);42  digitalWrite(RED_2, HIGH);43}44 45// Traffic Light 2 green, Traffic Light 1 red46void trafficLight2Green() {47  allLightsOff();48 49  digitalWrite(RED_1, HIGH);50  digitalWrite(GREEN_2, HIGH);51}52 53// Traffic Light 2 yellow, Traffic Light 1 red54void trafficLight2Yellow() {55  allLightsOff();56 57  digitalWrite(RED_1, HIGH);58  digitalWrite(YELLOW_2, HIGH);59}60 61void setup() {62  pinMode(RED_1, OUTPUT);63  pinMode(YELLOW_1, OUTPUT);64  pinMode(GREEN_1, OUTPUT);65 66  pinMode(RED_2, OUTPUT);67  pinMode(YELLOW_2, OUTPUT);68  pinMode(GREEN_2, OUTPUT);69 70  allLightsOff();71 72  // Start with both signals red73  digitalWrite(RED_1, HIGH);74  digitalWrite(RED_2, HIGH);75 76  delay(ALL_RED_TIME);77}78 79void loop() {80  // Traffic Light 1 gets green81  trafficLight1Green();82  delay(GREEN_TIME);83 84  // Traffic Light 1 changes to yellow85  trafficLight1Yellow();86  delay(YELLOW_TIME);87 88  // Both signals remain red briefly89  allLightsOff();90  digitalWrite(RED_1, HIGH);91  digitalWrite(RED_2, HIGH);92  delay(ALL_RED_TIME);93 94  // Traffic Light 2 gets green95  trafficLight2Green();96  delay(GREEN_TIME);97 98  // Traffic Light 2 changes to yellow99  trafficLight2Yellow();100  delay(YELLOW_TIME);101 102  // Both signals remain red briefly103  allLightsOff();104  digitalWrite(RED_1, HIGH);105  digitalWrite(RED_2, HIGH);106  delay(ALL_RED_TIME);107}

Full source code: [Insert the URL of your Arduino code page here.]

8. Understanding the Arduino Code

When I was writing the sketch, I divided the traffic light operation into a few separate functions. This makes the code easier to follow, especially when you want to change the sequence later.

Let’s go through the main parts.

1. Pin Definitions

Code
1const int RED_1 = 2;2const int YELLOW_1 = 3;3const int GREEN_1 = 4;

These lines tell the Arduino Nano which pins are connected to the first traffic light module.

I used D2 for red, D3 for yellow, and D4 for green. The second traffic light uses D5, D6, and D7.

Using names like RED_1 and GREEN_1 makes it easier to understand the sketch than using pin numbers everywhere.

2. Traffic Light Timing

Code
1const unsigned long GREEN_TIME = 5000;2const unsigned long YELLOW_TIME = 2000;3const unsigned long ALL_RED_TIME = 1000;

I set the green light to stay on for 5 seconds, the yellow light for 2 seconds, and both red lights for 1 second during the transition.

These values are in milliseconds.

If you want to make the green light stay on longer, you can increase GREEN_TIME. For example, changing it to 8000 will keep the green light on for 8 seconds.

3. The setup() Function

The setup() function runs only once when the Arduino Nano starts.

In this section, I configured all six LED control pins as OUTPUT. I also made both traffic lights start with their red LEDs turned on.

This gives the project a clear starting point before the sequence begins.

4. The loop() Function

The loop() function is where the actual traffic light sequence happens.

First, Traffic Light 1 gets the green signal. After 5 seconds, it changes to yellow. Both signals then turn red for 1 second.

The same sequence happens for Traffic Light 2.

Once the second traffic light finishes its yellow phase, both signals turn red again, and the cycle repeats.

5. Why I Used Separate Functions

I created functions such as trafficLight1Green() and trafficLight2Yellow() to handle each traffic light phase.

For example, when I call trafficLight1Green(), the function first turns off all the LEDs and then switches on the green LED of Traffic Light 1 and the red LED of Traffic Light 2.

This approach keeps the code organized and makes it easier to add more features later.

For a more advanced version of this project, you could replace delay() with millis(). That would allow the Arduino to monitor sensors or buttons while the traffic light sequence is running.

9. Testing My Dual Traffic Light Project

After connecting the two modules and uploading the sketch, I checked whether the traffic lights were switching in the correct order.

Here are the steps I followed to test the setup.

  1. I checked all six signal connections and the GND wires.
  2. I connected the Arduino Nano to my computer using USB.
  3. I selected the correct board and COM port in the Arduino IDE.
  4. I clicked Verify to compile the sketch.
  5. I uploaded the code to the Arduino Nano.
  6. I observed the two traffic light modules.
  7. I checked whether the first signal turned green while the second stayed red.
  8. I confirmed that the first signal changed to yellow.
  9. I checked whether both signals turned red during the transition.
  10. I watched the second signal complete its sequence.
  11. I let the project run for several cycles to make sure the sequence kept repeating.

Expected Results

This is the sequence programmed into the Arduino Nano.

StageTraffic Light 1Traffic Light 2Duration
Initial stateRedRed1 second
Phase 1GreenRed5 seconds
Phase 2YellowRed2 seconds
TransitionRedRed1 second
Phase 3RedGreen5 seconds
Phase 4RedYellow2 seconds
TransitionRedRed1 second

After the last transition, the sequence starts again from the first traffic light.

10. Troubleshooting Common Problems

While working with Arduino projects, wiring mistakes and incorrect module configurations can sometimes cause problems. If your traffic light system isn’t working as expected, here are a few things to check.

Problem 1: One or More LEDs Do Not Turn On

Possible causes: Incorrect wiring, a loose GND connection, a damaged LED, or a different module pinout.

What to check: Disconnect the USB cable and inspect the connections. Make sure the red, yellow, and green inputs are connected to the correct Arduino pins. Also check whether the module uses active-HIGH or active-LOW inputs.

Problem 2: The Traffic Lights Do Not Alternate Correctly

Possible causes: The wires may be connected to the wrong pins, or the LED order on your module may be different.

What to check: Compare your connections with the wiring tables in this tutorial. Make sure each LED input is connected to the correct Arduino Nano pin.

Problem 3: The Arduino IDE Cannot Upload the Sketch

Possible causes: Incorrect COM port, incorrect board selection, or a bootloader mismatch.

What to check: Select the correct Arduino Nano board and COM port. If you’re using a classic Nano clone, try the ATmega328P (Old Bootloader) option.

Problem 4: The LEDs Are Dim or the Arduino Resets

Possible causes: Insufficient power, excessive current draw, or unsuitable wiring.

What to check: Check the voltage and current requirements of your traffic light modules. Use suitable resistors or driver circuitry if required. Avoid powering high-current loads directly from the Nano’s I/O pins.

Problem 5: The LEDs Stay On When They Should Be Off

Possible causes: Your traffic light module may use active-LOW inputs, or the onboard circuitry may invert the control signals.

What to check: Refer to the module’s documentation. If it uses active-LOW inputs, modify the sketch so LOW activates an LED and HIGH deactivates it.

11. Ideas to Improve the Traffic Light Project

Once you have the basic dual traffic light system working, there are several ways to make the project more interesting.

Here are a few ideas I’d like to explore in a future version:

  • Pedestrian crossing button: Add a push button to request a pedestrian crossing phase.
  • Countdown display: Use an OLED or 7-segment display to show the remaining time.
  • Buzzer alert: Add a buzzer to indicate when the pedestrian crossing phase starts.
  • Vehicle detection: Connect an ultrasonic sensor to detect the presence of a vehicle.
  • Four-way intersection: Add more traffic light modules to simulate a four-direction road intersection.
  • Night mode: Use an LDR sensor to adjust the brightness of compatible LED modules.
  • Non-blocking control: Replace delay() with millis() so the Arduino can respond to buttons and sensors during each phase.
  • Custom timing: Experiment with different green and yellow durations.

These additions would make the project more interactive and give you a chance to work with additional Arduino components.

Keep in mind that real traffic control systems require validated safety logic, suitable hardware, and compliance with applicable standards. This project is only for educational demonstrations.

12. Frequently Asked Questions

1. How do you make a traffic light with an Arduino Nano?

Connect the red, yellow, and green inputs of a traffic light module to Arduino Nano digital output pins. Upload a sketch that switches the LEDs on and off in sequence. For a dual traffic light project, you can use six digital output pins to control two modules.

2. Can an Arduino Nano control two traffic light modules?

Yes. A classic Arduino Nano has enough digital I/O pins to control two three-color traffic light modules individually, provided the modules are electrically compatible or suitable driver circuitry is used.

3. Do I need a library for an Arduino traffic light project?

No additional library is required for this basic project. The sketch uses standard Arduino functions such as pinMode(), digitalWrite(), and delay().

4. How long should each traffic light stay green or yellow?

In my sketch, the green light stays on for 5 seconds and the yellow light stays on for 2 seconds. You can change these values according to your model. These timings are for demonstration purposes and are not recommendations for real-road traffic signals.

5. Why do both traffic lights turn red at the same time?

I added a short all-red transition between the two green phases. In this project, both red lights remain on for 1 second before the next green phase begins.

Build a realistic Arduino Nano Traffic Light System! 🚦Watch on YouTube

13. Conclusion

This was a simple project to build using an Arduino Nano and two traffic light modules. It demonstrates how a few digital output pins and a basic Arduino sketch can be used to control multiple LEDs in a repeating sequence.

I used six digital pins to control the red, yellow, and green LEDs of both traffic lights. The code handles the timing, switches the signals in the correct order, and repeats the sequence automatically.

If you’re just getting started with Arduino, this is a good project to experiment with. You can change the timing, add a pedestrian button, or try building a four-way traffic signal system.

If you build this project, let me know how it goes. I’d also love to see what changes you make to the traffic light system.