RRADIUS TUTOR
Robotics Lab / Experiment 02

Arduino Traffic Light Simulator

Set the red, green and amber light durations, assign Arduino pins and watch the simulated traffic lights change. Compare breadboard wiring with a circuit schematic.

Find a Tutor

01   Experiment parameters

Yellow code values follow your settings

02   Live circuit view

ARDUINO UNOATmega328PControllerUSBD8D9D10GND +− REDGREENAMBER220 Ω220 Ω220 ΩEducational wiring illustration • 3 separate output channels
Stopped · all LEDs OFF
Full cycle: 12 s

03   Arduino code

C++ / Arduino IDE
Yellow values are controlled by the parameters above. Each LED has its own 220 Ω series resistor.
// Experiment 02: Traffic lights
const int redPin = 8;
const int greenPin = 9;
const int amberPin = 10;

const unsigned long redTime = 5000;
const unsigned long greenTime = 5000;
const unsigned long amberTime = 2000;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(amberPin, OUTPUT);
}

void loop() {
  digitalWrite(redPin, HIGH);
  delay(redTime);
  digitalWrite(redPin, LOW);

  digitalWrite(greenPin, HIGH);
  delay(greenTime);
  digitalWrite(greenPin, LOW);

  digitalWrite(amberPin, HIGH);
  delay(amberTime);
  digitalWrite(amberPin, LOW);
}
Yellow = user-controlled program value.
Understand the logic

04   How does the traffic light work?

The Arduino activates one output at a time. When the programmed delay finishes, it switches that LED off and activates the next. The loop() function repeats the sequence.

1

Configure three outputs

pinMode() configures pins 8, 9 and 10 to drive three LEDs through separate resistors.

2

Red: stop

Only the red LED is ON for 5 seconds, then it switches OFF.

3

Green: go

Only the green LED is ON for 5 seconds, then it switches OFF.

4

Amber: prepare to stop

The amber LED stays ON for 2 seconds, then the sequence restarts.

Engineering connection: A traffic controller is a finite-state machine: RED → GREEN → AMBER → RED. One cycle takes 12 seconds. This beginner example uses blocking delay() calls; more advanced projects use millis() so the Arduino can respond to pedestrian buttons while timing the lights. Actual traffic signal sequences and safety interlocks vary by jurisdiction.