Learn Arduino Tutorials Open the 3D lab

Beginner · 5 min read

Traffic light LED module with Arduino: wiring, pins and code

Three LEDs, three resistors and a ground pin on one little board. Wire four jumpers and your Arduino can say go, slow down and stop — no extra parts needed.

A small traffic-light module with a red, a yellow and a green LED and four pins, shown in the 3D lab
Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →

What you need

  • Arduino UNO R3 (or a compatible board)
  • USB cable (A to B)
  • Half-size breadboard
  • Jumper wires (male–male)
  • Traffic-light LED module

Don't have the parts? Build it in 3D first.

What it is

A traffic light module is a small board with a red, a yellow and a green LED, each with its own 220 Ω resistor already soldered on. It has four pins: R, Y, G and GND. Wire each colour to an Arduino pin, GND to GND, and you are done — no loose resistors, no wondering which leg is long.

It is perfect for traffic lights (of course), but also for any three-level signal: battery full / half / low, distance far / near / stop, temperature OK / warm / hot. See it in 3D.

How it works

Each colour is just an LED and a resistor in a row, from its pin to the shared GND. When the Arduino sets a pin HIGH (5 V), current flows through that resistor and LED and the colour lights. LOW (0 V) and it goes dark.

The resistor sets the current. Each colour needs a slightly different voltage to light (its forward voltage), and the rest of the 5 V falls across the 220 Ω resistor:

ColourLED voltageLeft for the resistorCurrent (÷ 220 Ω)
Red1.9 V3.1 V≈ 14 mA
Yellow2.0 V3.0 V≈ 13.6 mA
Green2.1 V2.9 V≈ 13.2 mA

All three are well below a pin's 20 mA, so you can light all three at once safely. If you are new to this sum, read how an LED works and what a resistor does.

Pins

PinGoes toWhat it does
RA digital pin (D4 in the parking sensor)Red LED, HIGH = on
YA digital pin (D3)Yellow LED, HIGH = on
GA digital pin (D2)Green LED, HIGH = on
GNDGNDThe shared return for all three

How to wire it to an Arduino

In the parking-sensor project the module stands in the breadboard with its four legs in four strips:

  1. R strip → pin 4.
  2. Y strip → pin 3.
  3. G strip → pin 2.
  4. GND strip → the UNO's GND.

Any digital pins work; you just change the numbers in the code. No extra resistors — they are on the board already.

Drag the obstacle closer: green, then yellow, then red
Drag the obstacle closer: green, then yellow, then red · open it full screen

Example code

This is the parking sensor sketch. An HC-SR04 measures the distance, and the module shows how close you are:

const int TRIG = 9, ECHO = 10;
const int RED = 4, YELLOW = 3, GREEN = 2;

long distanceCm() {
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  return pulseIn(ECHO, HIGH) / 58;
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(GREEN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long cm = distanceCm();
  digitalWrite(GREEN, cm > 30);
  digitalWrite(YELLOW, cm > 15 && cm <= 30);
  digitalWrite(RED, cm <= 15);
  Serial.print(cm);
  Serial.println(" cm");
  delay(80);
}
  • const int RED = 4, YELLOW = 3, GREEN = 2; names the three pins, so the rest of the code reads like a sentence.
  • pinMode(..., OUTPUT) for each colour — the Arduino drives them.
  • digitalWrite(GREEN, cm > 30); is a neat trick: a comparison is either true (1, HIGH) or false (0, LOW), so the light simply follows the answer.
  • cm > 15 && cm <= 30 — && means "and": yellow only between 16 and 30 cm.
  • Exactly one of the three conditions is true for any distance, so exactly one light is on.

Common mistakes

  • Nothing lights. GND is not connected. All three colours return through that one pin.
  • The wrong colour lights. Two signal wires are swapped. Compare the pin numbers in the code with the R / Y / G labels.
  • Adding a second resistor "to be safe". It works, but the LEDs get dimmer. The 220 Ω is already on the board.
  • Pins left as inputs. Forget pinMode(..., OUTPUT) and the LED glows only faintly, if at all.

Check yourself

  1. Do you need extra resistors with this module?

  2. Roughly how much current does the red LED take from a 5 V pin?

  3. What does digitalWrite(GREEN, cm > 30) do when cm is 50?

Questions people ask

Can I dim the traffic light LEDs with PWM?

Yes. Wire a colour to a ~ pin (3, 5, 6, 9, 10 or 11) and use analogWrite(pin, 0–255). Pin 3 is a PWM pin, so in the parking-sensor wiring yellow can already fade.

Does the traffic light module work with 3.3 V boards?

Usually yes, but dimmer: with 3.3 V only about 1.2–1.4 V is left for the resistor, so each LED gets roughly 6 mA.

Can I build the same thing from loose LEDs?

Of course: three LEDs, three 220 Ω resistors and a breadboard. The module just saves six legs and three resistors.

Where it is used

The module is the display of the parking sensor. To learn the sensor half of that project, read the HC-SR04 pinout guide.

Build it yourself in 3DFree, in your browser — no board, no wires, nothing to buy.Open the 3D lab →
Next: DC motor explained: how it spins and how to drive it with Arduino →

Your progress stays on this device — no account, nothing sent anywhere.

Last updated 2026-10-29

Read next