Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Arduino Traffic Light Controller: Step-by-Step

· 6 min read · Smart Home

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to build a simple traffic light controller with an Arduino board, a few LEDs, and resistors. It’s a classic first electronics project that teaches you about digital outputs, timing, and basic circuit wiring. By the end, you’ll have a working model that cycles through red, yellow, and green just like a real intersection. No prior Arduino experience is needed, but you should know how to plug components into a breadboard.

What You'll Need

  • Arduino board (Uno, Nano, or similar)
  • USB cable to connect the Arduino to your computer
  • Breadboard (half-size is fine)
  • 6 LEDs – 2 red, 2 yellow, 2 green (one set for each direction)
  • 6 resistors – 220Ω to 330Ω (any value in that range works)
  • Jumper wires (male-to-male, at least 10)
  • Arduino IDE installed on your computer (free from arduino.cc)

Step-by-Step

  1. Set up the Arduino IDE. Open the IDE, go to Tools > Board, and select your board model. Then go to Tools > Port and choose the port that shows your Arduino (on Windows it’s usually COM3 or higher; on Mac it’s /dev/cu.usbmodem…). If you’re unsure, plug in the board and see which port appears.
  2. Plan the pin connections. We’ll use pins 2, 3, and 4 for the first set of lights (red, yellow, green) and pins 5, 6, and 7 for the second set. The exact pins don’t matter, but write them down so you don’t mix them up.
  3. Place the LEDs on the breadboard. Each LED has two legs: the longer one is the anode (positive), the shorter one is the cathode (negative). Insert the first red LED so its anode goes to a row on the breadboard, and its cathode goes to a different row. Do the same for the yellow and green LEDs, leaving space between them.
  4. Add the resistors. Connect a 220Ω resistor between the anode of each LED and a new row on the breadboard. The resistor limits current so the LED doesn’t burn out. The exact row doesn’t matter, but keep it neat.
  5. Wire the LEDs to the Arduino. Use jumper wires to connect the resistor side of each LED to the Arduino pin you assigned. For example, connect the red LED’s resistor to pin 2, the yellow to pin 3, and the green to pin 4. Do the same for the second set on pins 5, 6, and 7.
  6. Connect the ground (GND). Connect the cathode of every LED to the ground rail on the breadboard. Then run a jumper wire from that ground rail to the GND pin on the Arduino. This completes the circuit for all LEDs.
  7. Write the code. In the Arduino IDE, create a new sketch and paste the following code:
    // Traffic Light Controller
    int red1 = 2, yellow1 = 3, green1 = 4;
    int red2 = 5, yellow2 = 6, green2 = 7;
    
    void setup() {
      pinMode(red1, OUTPUT);
      pinMode(yellow1, OUTPUT);
      pinMode(green1, OUTPUT);
      pinMode(red2, OUTPUT);
      pinMode(yellow2, OUTPUT);
      pinMode(green2, OUTPUT);
    }
    
    void loop() {
      // Set 1 green, Set 2 red
      digitalWrite(green1, HIGH);
      digitalWrite(red2, HIGH);
      delay(5000); // 5 seconds
    
      // Set 1 yellow, Set 2 still red
      digitalWrite(green1, LOW);
      digitalWrite(yellow1, HIGH);
      delay(2000); // 2 seconds
    
      // Set 1 red, Set 2 green
      digitalWrite(yellow1, LOW);
      digitalWrite(red1, HIGH);
      digitalWrite(green2, HIGH);
      digitalWrite(red2, LOW);
      delay(5000); // 5 seconds
    
      // Set 2 yellow
      digitalWrite(green2, LOW);
      digitalWrite(yellow2, HIGH);
      delay(2000); // 2 seconds
    
      // Reset to start
      digitalWrite(yellow2, LOW);
      digitalWrite(red1, LOW);
    }
  8. Upload the code. Click the upload button (right arrow icon) in the IDE. Wait for the message “Done uploading” in the status bar. If you get an error, double-check that the correct port is selected and the board is plugged in.
  9. Test it. The LEDs should start cycling: green on set 1, then yellow, then red, while set 2 does the opposite. If nothing happens, check your wiring and that the code uploaded successfully.

Common Mistakes

  • LED legs reversed – the anode must connect to the resistor, the cathode to ground. If the LED doesn’t light, try flipping it.
  • Missing resistors – without a resistor, the LED may burn out instantly. Always use one.
  • Wrong pins – double-check that the code’s pin numbers match the physical wires. A simple typo can cause a different light to blink.
  • Ground not connected – all LEDs need a common ground. If you forget the GND wire, nothing will light.
  • Using the wrong Arduino port – if the upload fails, check that you’ve selected the correct port in the IDE.

Tips

  • Use different colored jumper wires (e.g., red for power, black for ground) to make debugging easier.
  • Start with longer delays (like 5 seconds) so you can clearly see each state.
  • Add a pedestrian crossing later by adding two more LEDs and adjusting the timing.
  • If you want a more realistic cycle, include an all-red state for 1 second between changes.
  • Test each LED individually by connecting it directly to 5V and GND (with a resistor) before wiring everything.

Troubleshooting

  • Nothing lights up: Check that the Arduino is powered (the green LED on the board is on). Verify that all ground connections are secure. Make sure the code uploaded without errors.
  • Only one LED works: That LED’s circuit is correct; check the others for reversed legs, loose wires, or missing resistors.
  • LEDs are very dim: You might be using a high-value resistor (like 10kΩ). Use 220Ω to 330Ω for bright output.
  • Upload fails with “port not found”: Unplug the Arduino, wait a few seconds, plug it back in. Re-select the port in the IDE. On Windows, you may need to install the CH340 driver if you’re using a clone board.
  • Lights change too fast or too slow: Adjust the numbers in the delay() function. The value is in milliseconds, so 5000 = 5 seconds.

Frequently Asked Questions

Can I use any Arduino board?

Yes, any board with at least 6 digital output pins works. The Uno and Nano are the most common for beginners.

Do I need to use a breadboard?

No, you can solder the components directly, but a breadboard makes it easy to change connections and reuse parts.

What happens if I forget the resistor?

The LED will likely burn out quickly because it draws too much current. Always use a resistor between 220Ω and 330Ω.

Can I make the lights switch automatically without a delay?

Yes, you can use the millis() function to create non-blocking timing, which allows the Arduino to do other things while waiting. That’s more advanced but useful for larger projects.

How do I add a pedestrian crossing?

Add two extra LEDs (red and green) for pedestrians, connect them to two more pins, and modify the code to show a “walk” signal during the red phase.

Why does my LED not light up even though the code is correct?

Most likely a wiring issue: check that the anode is connected to the resistor, the cathode to ground, and that all jumper wires are firmly inserted. Also verify the pin number in the code matches the physical connection.