To create a traffic light with Arduino, you need to connect three LEDs (Red, Yellow, Green) to digital pins and use the digitalWrite() and delay() functions to cycle through them in a specific sequence. This project achieves a basic automated timing loop that simulates a real-world intersection, teaching the fundamentals of GPIO (General Purpose Input/Output) control and timing logic in C++.

What You'll Need

  • Arduino Uno (or any compatible board like Nano or Mega)
  • USB cable for connecting the Arduino to your computer
  • One Red LED
  • One Yellow LED
  • One Green LED
  • Three 220-ohm resistors (to prevent the LEDs from burning out)
  • Breadboard
  • Jumper wires (Male-to-Male)
  • A computer with the Arduino IDE installed

Step-by-Step

  1. Prepare the Hardware: Place your Arduino board on a flat surface. Insert the three LEDs into the breadboard. Ensure they are spaced apart so the wiring remains clean. Remember that LEDs have polarity: the longer leg is the positive anode, and the shorter leg is the negative cathode.
  2. Connect the Cathodes: Use jumper wires to connect the shorter leg (cathode) of each LED to the common ground rail on the breadboard. Then, connect the ground rail of the breadboard to the GND pin on the Arduino board. This completes the electrical circuit.
  3. Install the Resistors: Place a 220-ohm resistor in series with each LED. Connect one end of the resistor to the longer leg (anode) of the LED and the other end to an empty row on the breadboard. Resistors are critical because LEDs cannot handle the full current provided by the Arduino pins; without them, the LED may pop or damage the Arduino pin.
  4. Connect to Digital Pins: Use jumper wires to connect the resistors to specific digital pins on the Arduino. For this guide, connect the Red LED resistor to Pin 12, the Yellow LED resistor to Pin 11, and the Green LED resistor to Pin 10.
  5. Open the Arduino IDE: Launch the Arduino IDE on your computer. Go to the Tools menu and select the correct board (e.g., Arduino Uno) and the correct Port (usually labeled COM followed by a number on Windows or /dev/cu.usb... on macOS).
  6. Write the Setup Code: In the IDE, create a new sketch. In the void setup() function, you must define the pins as outputs. Use the following code:
    void setup() {
          pinMode(12, OUTPUT); // Red LED
          pinMode(11, OUTPUT); // Yellow LED
          pinMode(10, OUTPUT); // Green LED
        }
  7. Write the Loop Logic: In the void loop() function, you will program the sequence. First, turn the Red LED on and the others off, then wait. Next, turn the Green LED on and the others off, then wait. Finally, turn the Yellow LED on briefly before returning to Red. Use this structure:
    void loop() {
          // Red Light
          digitalWrite(12, HIGH);
          digitalWrite(11, LOW);
          digitalWrite(10, LOW);
          delay(5000); // Wait 5 seconds
    
          // Green Light
          digitalWrite(12, LOW);
          digitalWrite(11, LOW);
          digitalWrite(10, HIGH);
          delay(5000); // Wait 5 seconds
    
          // Yellow Light
          digitalWrite(12, LOW);
          digitalWrite(11, HIGH);
          digitalWrite(10, LOW);
          delay(2000); // Wait 2 seconds
        }
  8. Upload the Code: Click the Upload button (the right-pointing arrow) in the IDE. The software will compile the code and send it to the Arduino via USB. Once the status bar says "Done uploading," the LEDs should begin cycling automatically.

Common Mistakes

One of the most frequent errors is inserting the LEDs backward. Since LEDs are diodes, they only allow current to flow in one direction. If the long leg is connected to ground and the short leg to the digital pin, the LED will not light up regardless of whether the code is correct. Always double-check the polarity before powering the board.

Another common mistake is forgetting the resistors. While an LED might light up for a few seconds without a resistor, the excessive current will eventually burn out the LED or, worse, damage the internal circuitry of the Arduino's digital pin. Always use a resistor between 220 and 330 ohms for standard 5V Arduino pins.

Beginners often forget to define the pinMode in the setup() function. If you try to use digitalWrite() on a pin that has not been set to OUTPUT, the LED will either not light up or will glow very dimly because the pin is still in its default high-impedance input state.

Lastly, syntax errors in the Arduino IDE are common. Missing a semicolon (;) at the end of a line or forgetting a closing curly bracket (}) will cause the compiler to throw an error. Read the error message at the bottom of the IDE; it usually tells you exactly which line is causing the problem.

Tips

To make your code cleaner and easier to modify, use variables instead of hard-coding pin numbers. For example, declare int redPin = 12; at the top of your sketch. If you decide to move the wire to Pin 8 later, you only have to change the number in one place rather than searching through the entire loop for every instance of "12".

If you want to simulate a more realistic traffic light, you can implement a "Yellow-to-Red" transition or a "Red-and-Yellow" combination (common in some European countries) by turning on two pins simultaneously before the light changes. This adds a layer of complexity to your timing logic.

For those looking to expand the project, consider adding a push-button to simulate a pedestrian crossing. This would require changing the code from a simple linear loop to a conditional structure using if statements, where the light only changes when the button is pressed.

Organize your breadboard by using different colored jumper wires. Use a red wire for the red LED, a yellow/orange wire for the yellow LED, and a green wire for the green LED. This makes it significantly easier to debug your circuit if a light fails to trigger.

Troubleshooting

If the LEDs do not light up at all, first check the USB connection. Ensure the Arduino board's power LED (usually labeled ON) is glowing. If it is not, try a different USB cable or port on your computer. Next, check the ground connection. A loose wire connecting the breadboard rail to the Arduino GND pin will break the circuit for all three LEDs.

If only one or two LEDs are working, check the specific wiring for the non-functional LED. Ensure the resistor is firmly seated in the breadboard and that the LED legs are not touching each other, which could cause a short circuit. Try swapping the non-working LED with one that is working to determine if the LED itself is burnt out.

If the lights are blinking in the wrong order or the timing feels off, review your digitalWrite sequence in the loop() function. Ensure that for every state, you are explicitly turning the other two LEDs LOW. If you forget to set a pin to LOW, multiple lights will stay on at the same time as the loop progresses.

If the Arduino IDE fails to upload the code, check the Tools menu again. The most common cause is selecting the wrong COM port. If you see multiple ports, unplug the Arduino and see which one disappears; the one that returns when you plug it back in is the correct port. If the port doesn't appear at all, you may need to install the drivers for your specific Arduino clone (such as the CH340 driver).

Frequently Asked Questions

Can I use different resistors?

Yes, any resistor between 220 and 1k ohms will work. A higher resistance (like 1k) will make the LED dimmer, while a lower resistance (below 220) risks damaging the LED.

Do I have to use the Arduino Uno?

No, this code and circuit work on almost any Arduino board, including the Nano, Mega, and Leonardo, as they all use the same digitalWrite logic.

What does delay(5000) actually do?

The delay() function pauses the program execution for a specified number of milliseconds. Therefore, delay(5000) tells the Arduino to do nothing for exactly 5 seconds.

Why is my LED very dim?

This usually happens if the resistor value is too high (e.g., using 10k instead of 220 ohms) or if the LED is inserted backward and is leaking a tiny amount of current.

Can I add more lights to this circuit?

Yes, you can add as many LEDs as you have available digital pins. You would simply define new pins in setup() and add them to the sequence in loop().