To create a traffic light in Arduino Tinkercad, you need to connect three LEDs (Red, Yellow, Green) to digital pins on an Arduino Uno and write a loop that turns them on and off in sequence using the digitalWrite() and delay() functions. This project simulates a standard road signal, teaching the basics of circuit completion, current limiting with resistors, and timing logic in C++.

What You'll Need

  • Tinkercad Account (Free)
  • Arduino Uno R3 (Virtual)
  • Breadboard (Small or Full)
  • 1 Red LED
  • 1 Yellow LED
  • 1 Green LED
  • 3 Resistors (220 Ohm recommended)
  • Jumper Wires (Virtual)

Step-by-Step

  1. Initialize the Workspace: Log into Tinkercad, click on "Designs," and select "Create" then "Circuit." From the components panel on the right, drag an Arduino Uno R3 and a Breadboard into the center workspace.
  2. Place the LEDs: Drag three LEDs onto the breadboard. Place them in a vertical row. Change the colors of the LEDs to Red, Yellow, and Green using the dropdown menu in the component properties window that appears when an LED is clicked.
  3. Connect the Cathodes: Identify the cathode (the shorter, straight leg) of each LED. Connect each cathode to the negative (-) rail of the breadboard using a black jumper wire. Then, connect the negative rail of the breadboard to the GND (Ground) pin on the Arduino Uno.
  4. Install Resistors: To prevent the LEDs from burning out due to excessive current, place a 220 Ohm resistor between the anode (the longer, bent leg) of each LED and an empty row on the breadboard. Ensure the resistor is properly seated in the breadboard holes.
  5. Wire to Digital Pins: Use jumper wires to connect the other end of the resistors to the Arduino digital pins. Connect the Red LED resistor to Pin 13, the Yellow LED resistor to Pin 12, and the Green LED resistor to Pin 11.
  6. Open the Code Editor: Click the "Code" button in the top right corner. Change the dropdown menu from "Blocks" to "Text." A warning will appear asking if you want to switch; confirm this to enter the C++ editor.
  7. Define the Pins: At the very top of the code editor, before the setup function, define your pins to make the code easier to read. Type:
    int redPin = 13;
    int yellowPin = 12;
    int greenPin = 11;
  8. Configure Pin Modes: Inside the void setup() function, you must tell the Arduino that these pins are outputs. Add the following lines:
    pinMode(redPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
  9. Write the Logic Loop: Inside the void loop() function, create the sequence. First, turn the Red LED on and others off:
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, LOW);
    delay(5000); // Wait 5 seconds
  10. Add the Green Transition: Next, switch to Green. In a real traffic light, it usually goes Red to Green.
    digitalWrite(redPin, LOW);
    digitalWrite(yellowPin, LOW);
    digitalWrite(greenPin, HIGH);
    delay(5000); // Wait 5 seconds
  11. Add the Yellow Warning: Finally, switch to Yellow before returning to Red.
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, HIGH);
    delay(2000); // Wait 2 seconds
  12. Run the Simulation: Click the "Start Simulation" button. You should see the Red LED light up for 5 seconds, followed by the Green LED for 5 seconds, and the Yellow LED for 2 seconds, repeating indefinitely.

Common Mistakes

One of the most frequent errors is reversing the LED polarity. LEDs are diodes, meaning current only flows in one direction. If you connect the anode to ground and the cathode to the digital pin, the LED will not light up. Always ensure the shorter leg (cathode) goes to the ground rail.

Another common mistake is omitting the resistors. While Tinkercad might show a small warning symbol (an exclamation mark) over the LED instead of actually "blowing it up," in a real-world circuit, connecting an LED directly to 5V without a resistor would destroy the component instantly. Always use a resistor between 220 and 330 Ohms for standard LEDs.

Coding errors often involve forgetting the semicolon (;) at the end of a line. In C++, every statement must end with a semicolon. If you miss one, the compiler will throw an error, and the simulation will not start. Additionally, ensure that digitalWrite is spelled with a capital 'W'; Arduino code is case-sensitive, and digitalwrite will result in a compilation error.

Tips

To make your code more professional, use variables for your timing. Instead of typing delay(5000) multiple times, create variables at the top of your code like int redDuration = 5000;. This allows you to change the timing for the entire sequence by editing just one number at the top of the script.

If you want to expand this project, try adding a pedestrian button. You can connect a push-button to a different digital pin using a pull-down resistor. You can then modify the code so that the light only changes from Green to Yellow when the button is pressed, simulating a real-world crosswalk request.

For better organization in Tinkercad, use different colored wires. Use red wires for power/signal to the red LED, green for green, and yellow for yellow. Use black wires exclusively for ground (GND). This makes it much easier to spot wiring errors when your circuit becomes more complex.

Troubleshooting

If the simulation starts but no LEDs light up, first check the ground connection. Ensure there is a wire connecting the Arduino GND pin to the negative rail of the breadboard and that every LED cathode is connected to that same rail. A single missing ground wire will break the entire circuit.

If only one LED is working or they are lighting up in the wrong order, double-check your pin numbers. Verify that the wire from the Red LED goes to pin 13, Yellow to 12, and Green to 11. If your code says int redPin = 13; but the wire is plugged into pin 10, the wrong light will activate.

If the LEDs are very dim, check the resistor values. If you accidentally selected a 10k Ohm resistor instead of a 220 Ohm resistor, the resistance will be too high, and the current will be too low to brighten the LED. Click the resistor in Tinkercad and check the "Resistance" value in the properties box.

If the code won't compile, look at the error message at the bottom of the code editor. It usually tells you the exact line number where the error is located. Check for missing curly brackets { } or missing semicolons ; on that line and the line immediately preceding it.

Frequently Asked Questions

Why do I need resistors for the LEDs?

LEDs have very little internal resistance. Without a resistor to limit the current, the Arduino would push too much electricity through the LED, causing it to overheat and burn out.

Can I use different pins on the Arduino?

Yes, you can use any digital pin (0-13). Just make sure the pin number in your int variable matches the physical pin where the wire is plugged in.

What does digitalWrite(pin, HIGH) actually do?

It sends 5 volts of electricity to the specified pin, which completes the circuit and turns the LED on. LOW sets the voltage to 0, turning the LED off.

What is the difference between delay() and millis()?

The delay() function pauses the entire program, meaning the Arduino can't do anything else until the time is up. millis() tracks elapsed time without pausing the program, which is useful for more advanced projects.

Why does the LED have a bent leg?

The bent leg is the anode (positive), and the straight leg is the cathode (negative). This is a physical marker to help users orient the LED correctly in a circuit.

Can I add more lights to this circuit?

Yes, as long as you have available digital pins and enough resistors. You can add a second set of lights to create a four-way intersection simulation.