If you want to control three-way traffic lights—red-yellow-green for one direction and red-only for the other—Arduino makes it simple. Instead of buying a complex system, you can code a reliable solution that fits your budget and adapts to your needs. Here’s how to do it right, with exact wiring, timing logic, and safety precautions.

1. Logic Circuit Design: How to Wire Two Independent Traffic Light Sequences

The core challenge is managing two traffic light loops: one for the main direction (red → yellow → green) and another for the opposing side, which remains red. You can wire this using two Arduino pins—each controlling the reverse direction’s red LED. For example, if the main direction’s green is active, the opposite side’s red must stay on. This avoids interference. A simple approach uses a single Arduino pin to toggle both loops’ red LEDs: when the main direction turns green, the Arduino sends a signal to keep the opposite side red. This is cleaner than using relays or transistors, which add complexity. The wiring runs like this:

  • Attach the main direction’s red LED to pin 2, green to pin 3, yellow to pin 4.
  • Attach the opposite side’s red LED to a separate pin (e.g., pin 5) with a pull-down resistor if needed.
  • Use an inverter (e.g., a NAND gate or a transistor like a 2N2222) to flip signals if your Arduino lacks direct LED control.

This setup ensures no cross-talk between the two loops. If you’re using a microcontroller like the Arduino Uno, you’ll need external resistors (e.g., 220Ω) for the LEDs to avoid damaging the pins.

2. Code Structure: Timing and Synchronization with Exact Durations

The code must enforce strict timing for each phase: 30 seconds red, 2 seconds yellow, 30 seconds green, repeating. You’ll use `millis()` for precise timing (avoid `delay()` to prevent blocking the rest of the program). The structure looks like this:

const int redPin = 2;
const int greenPin = 3;
const int yellowPin = 4;
const int oppositeRedPin = 5;

unsigned long lastChange = 0;
const unsigned long redDuration = 30000; // 30 sec
const unsigned long yellowDuration = 2000; // 2 sec
const unsigned long greenDuration = 30000; // 30 sec

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(oppositeRedPin, OUTPUT);
  digitalWrite(redPin, LOW); // Start with main red off
  digitalWrite(greenPin, LOW);
  digitalWrite(yellowPin, LOW);
  digitalWrite(oppositeRedPin, HIGH); // Opposite side red on
}

void loop() {
  unsigned long currentTime = millis();

  if ((currentTime - lastChange) >= redDuration) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(yellowPin, LOW);
    lastChange = currentTime;
  }
  else if ((currentTime - lastChange) >= (redDuration + yellowDuration)) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, HIGH);
    lastChange = currentTime;
  }
  else if ((currentTime - lastChange) >= (redDuration + yellowDuration + greenDuration)) {
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(oppositeRedPin, HIGH); // Opposite side red on
    lastChange = currentTime;
  }
}

Key notes: `lastChange` tracks when the current phase starts. The opposite side’s red LED stays on while the main direction is green. This avoids logic errors like forgetting to reset the opposite side.

3. Safety Checks: Preventing Faulty Timings and Overcurrent Risks

Arduino’s timers can drift with battery changes or noise. To fix this, add a small delay after phase changes to stabilize the LEDs. For power, use a 9V battery in series with a 100Ω current limiter—never connect LEDs directly to the Arduino’s 5V pin, as it can draw too much current (max 20mA per pin). If you’re using a breadboard, check that all connections are clean and insulated. A common failure is a loose wire causing intermittent shorts; test with a multimeter. Also, if the lights flicker, adjust the pull-down resistors on the opposite red LED to ~1kΩ.

For real-world use, consider adding a button to manually override the sequence (e.g., for emergency stops). Use a push-button with a pull-up resistor (e.g., 10kΩ) and a debounce circuit (e.g., a 10µF capacitor + transistor) to avoid false triggers.

How to Choose

When picking an Arduino for this project, focus on three things: pin count, power supply, and reliability. The Uno (8 pins) works fine, but if you need more (e.g., sensors), use a Mega (54 pins). For power, a 9V battery with a current limiter is safe; avoid USB power unless you add a dedicated power supply. If you’re testing, use a breadboard with a 9V adapter—never plug it directly into the Arduino’s USB port. For wiring, ensure all connections are short and isolated to avoid shorts. If you’re in a humid area, add a small desiccant to prolong battery life.

Frequently Asked Questions

What if my Arduino’s pins draw too much current?

Use external resistors (e.g., 220Ω) for LEDs and limit the Arduino’s current with a 100Ω series resistor between the battery and the board. Never exceed 20mA per pin.

How do I adjust the timing for my street?

Modify the constants in the code (e.g., `30000` = 30 sec). Test with `Serial.print()` to log phase changes before deploying. If using a real-world system, monitor traffic density and tweak timings incrementally.

Can I add motion sensors to this setup?

Yes, but integrate them carefully. Use a PIR sensor (e.g., MPU-111) to trigger a shorter green phase (e.g., 10 sec) when movement is detected. Add a debounce delay (e.g., 2 sec) to prevent false triggers.

What happens if the battery dies mid-sequence?

The Arduino will reset, restarting the full sequence. To prevent this, add a small capacitor (e.g., 100µF) near the power input to smooth voltage drops.

Do I need a relay for this?

No, but if you want to control multiple lights or motors (e.g., a traffic signal actuator), use a relay module (e.g., 5V relay board) with a transistor to switch higher currents safely.

How do I debug flickering lights?

Check for loose wires or short circuits. Use a multimeter to verify resistor values (e.g., 220Ω LEDs, 1kΩ pull-downs). If the issue persists, add a small LED (e.g., 100Ω) in series with each output to test continuity.

Final Thoughts

A three-way traffic light on Arduino is a great project for learning logic, timing, and basic electronics. The key is keeping things simple—two loops, precise timers, and safe wiring. For permanent installations, consider adding a power switch or timer module to avoid manual resets. Start small, test often, and prioritize safety above all. If you’re ready to scale up, explore adding sensors or wireless control, but for now, this setup offers a reliable, cost-effective solution.