Traffic lights on pins 3, 4 and 5, on an Arduino Uno
Green on pin 3, yellow on 4 and red on 5, each through 220 ohm: the lights run green, yellow, red and round again, with the times a real crossing might use. The whole build, an Arduino Uno and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
What you'll learn
- Driving several outputs
- Writing a helper function
- Timing a sequence with
delay - Sharing one ground across LEDs
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 8
How it works
Three LEDs make a traffic light: green on pin 3, yellow on pin 4 and red on pin 5. Each has its own 220 ohm resistor, and all three share a ground.
The light runs a cycle: green for 5 seconds, yellow for 2 seconds, then red for 5 seconds, and starts again. Each step also prints its color on the serial monitor.
A small function named lights sets all three LEDs in one call, so each step in the cycle is a single readable line.
Parts list
8 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED, red pin C | Arduino Uno R3 pin GND |
| LED, yellow pin C | Arduino Uno R3 pin GND |
| LED, green pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 3 |
| LED, red pin A | Resistor, 220 Ω (1) pin 2 |
| LED, yellow pin A | Resistor, 220 Ω (2) pin 2 |
| LED, green pin A | Resistor, 220 Ω (3) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Traffic lights: green, yellow and red on pins 3, 4 and 5.
//
// pin 3 -> 220 ohm -> green LED -> GND
// pin 4 -> 220 ohm -> yellow LED -> GND
// pin 5 -> 220 ohm -> red LED -> GND
//
// One function sets all three lights at once, so each step of the cycle is
// one line: which lamps are on, and for how long.
const int GREEN = 3;
const int YELLOW = 4;
const int RED = 5;
void lights(int green, int yellow, int red) {
digitalWrite(GREEN, green);
digitalWrite(YELLOW, yellow);
digitalWrite(RED, red);
}
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: traffic lights on pins 3, 4 and 5");
}
void loop() {
Serial.println("green");
lights(HIGH, LOW, LOW);
delay(5000);
Serial.println("yellow");
lights(LOW, HIGH, LOW);
delay(2000);
Serial.println("red");
lights(LOW, LOW, HIGH);
delay(5000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
lights sketch.ino
Takes three values and writes them to the green, yellow and red pins. Calling lights(HIGH, LOW, LOW) means green on, the others off. One function keeps loop short.
void lights(int green, int yellow, int red) {
digitalWrite(GREEN, green);
digitalWrite(YELLOW, yellow);
digitalWrite(RED, red);
}setup sketch.ino
Makes the three pins outputs and starts serial with a short message.
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: traffic lights on pins 3, 4 and 5");
}loop sketch.ino
Prints a color name, calls lights, and waits: 5000 ms for green, 2000 for yellow, 5000 for red.
void loop() {
Serial.println("green");
lights(HIGH, LOW, LOW);
delay(5000);
Serial.println("yellow");
lights(LOW, HIGH, LOW);
delay(2000);
Serial.println("red");
lights(LOW, LOW, HIGH);
delay(5000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors light in the wrong order This project
Green is pin 3, yellow is pin 4 and red is pin 5. Check which LED is on which pin. - An LED does not light This project
The long leg (anode) goes toward the resistor and pin, the short leg to ground. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.