You can code a traffic light in about ten minutes with an Arduino, three LEDs (red, yellow, green), three resistors, and a breadboard. The code cycles the LEDs through the standard red–green–yellow sequence with timed delays. No mains wiring is involved, so it is a safe first electronics project. If you want to control a real household lamp, stop and call a licensed electrician—mains voltage is not a DIY step.
What You'll Need
- Arduino board (Uno, Nano, or similar) and a USB cable
- Three LEDs: red, yellow, green
- Three resistors, 220 Ω to 330 Ω (one per LED)
- Breadboard and jumper wires
- A computer with the Arduino IDE installed from the official Arduino website
Step-by-Step
- Wire the LEDs. Place the red, yellow, and green LEDs on the breadboard. Connect each LED's longer leg (anode) through a resistor to a separate Arduino digital pin—use pins 8, 9, and 10 for red, yellow, and green. Connect each LED's shorter leg (cathode) to the Arduino's GND pin. The resistor must be in series with each LED; without it the LED can burn out.
- Open the Arduino IDE. Launch the IDE and select your board under Tools > Board and the correct port under Tools > Port. Menu labels can differ slightly by version and operating system.
- Write the code. Paste this sketch into the editor:
The delays are in milliseconds: 5000 ms = 5 seconds. Adjust them to change the timing.const int redPin = 8; const int yellowPin = 9; const int greenPin = 10; void setup() { pinMode(redPin, OUTPUT); pinMode(yellowPin, OUTPUT); pinMode(greenPin, OUTPUT); } void loop() { digitalWrite(redPin, HIGH); digitalWrite(yellowPin, LOW); digitalWrite(greenPin, LOW); delay(5000); digitalWrite(redPin, LOW); digitalWrite(greenPin, HIGH); delay(5000); digitalWrite(greenPin, LOW); digitalWrite(yellowPin, HIGH); delay(2000); digitalWrite(yellowPin, LOW); } - Upload the sketch. Click the Upload button (right-arrow icon). The IDE compiles and sends the code to the board. If you see an error, check that the correct board and port are selected.
- Test the sequence. After upload, the red LED should light for 5 seconds, then green for 5 seconds, then yellow for 2 seconds, then repeat. If an LED does not light, check its orientation and resistor connection.
Common Mistakes
- Forgetting the resistors—each LED needs one in series to limit current.
- Putting the LED in backwards: the longer leg is the anode and goes toward the Arduino pin (through the resistor).
- Using the same pin for two LEDs, or wiring to a pin that is not declared in the code.
- Uploading without selecting the correct board and port, which causes a “programmer not responding” error.
- Assuming the code controls mains voltage—it does not. Keep the project on low-voltage DC only.
Tips
- Use a breadboard power rail for GND so you do not need multiple ground wires.
- Label your jumper wires if you plan to expand to a pedestrian button later.
- Use
#defineorconst intfor pin numbers so the code is easy to change. - If the LEDs are too bright, increase the resistor value (e.g., 1 kΩ) to reduce current.
Troubleshooting
- Nothing lights up: Check USB connection, board selection, and that the sketch uploaded without errors. Try a different USB cable or port.
- One LED never lights: Swap the LED with a known-good one, check the resistor, and confirm the pin number in the code matches the wiring.
- Sequence is wrong: Re-read the
loop()section; eachdigitalWritepair must turn the previous LED off before turning the next on. - Upload fails: Close the Serial Monitor, press the reset button on the board, and try again. On some boards you must select the correct processor under Tools.
- LED flickers or is dim: A loose breadboard connection or a resistor value that is too high. Reseat the components.
Frequently Asked Questions
Can I use a Raspberry Pi instead of an Arduino?
Yes. The same three-LED circuit works on a Raspberry Pi's GPIO pins, but the code is written in Python and you must use the correct pin numbering library. The wiring and resistor values are similar.
Do I need a breadboard?
No. You can solder the LEDs and resistors to a prototyping board or use alligator clips, but a breadboard is the easiest way to test without soldering.
How do I change the timing?
Change the numbers inside the delay() calls. The value is in milliseconds, so 1000 = 1 second. Keep the total cycle time reasonable for your demonstration.
Can I add a pedestrian button?
Yes. Wire a pushbutton between a digital pin and GND, then read it with digitalRead() and change the sequence when pressed. Use a pull-up resistor or the internal pull-up to avoid floating reads.
Is it safe to leave this running unattended?
The low-voltage Arduino circuit is safe, but do not connect it to mains-powered lamps or fixtures. For any mains wiring, hire a licensed electrician.
Why does my LED stay on all the time?
Likely the pin is set to HIGH and never set back to LOW, or the LED is wired directly to 5V without a resistor. Check the code and wiring.