Go back

Build a Traffic Light Simulator with Arduino on Velxio

Introduction

Ever wanted to build a traffic light but didn’t have the components handy? With Velxio, you can simulate an entire Arduino-based traffic light right in your browser. This project uses three LEDs (red, yellow, green) connected to an Arduino Uno to mimic the classic traffic light sequence: red → yellow → green → yellow → red. It’s a perfect beginner project to learn digital outputs, timing, and sequential logic.

Circuit Walkthrough

Let’s look at the circuit. The diagram below shows the complete wiring:

Circuit diagram of traffic-light on the Velxio simulator canvas

Components

Wiring Details

Each LED has two legs: anode (longer leg, marked as pin “A”) and cathode (shorter leg, marked as pin “C”). The wiring is straightforward:

All three LEDs share the same GND connection on the Arduino. This is a common ground configuration.

Code Walkthrough

Now let’s examine the code that makes the lights change. Open the code editor in Velxio:

Source code for traffic-light in the Velxio Monaco editor

// Traffic Light Simulator
// Red -> Yellow -> Green -> Yellow -> Red

const int RED_PIN = 13;
const int YELLOW_PIN = 12;
const int GREEN_PIN = 11;

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
}

void loop() {
  // Red light
  digitalWrite(RED_PIN, HIGH);
  delay(3000);
  digitalWrite(RED_PIN, LOW);

  // Yellow light
  digitalWrite(YELLOW_PIN, HIGH);
  delay(1000);
  digitalWrite(YELLOW_PIN, LOW);

  // Green light
  digitalWrite(GREEN_PIN, HIGH);
  delay(3000);
  digitalWrite(GREEN_PIN, LOW);

  // Yellow light again
  digitalWrite(YELLOW_PIN, HIGH);
  delay(1000);
  digitalWrite(YELLOW_PIN, LOW);
}

How It Works

  1. Pin Definitions: We define constants for the three pins: RED_PIN = 13, YELLOW_PIN = 12, GREEN_PIN = 11. These match the wiring.
  2. Setup: In setup(), we set all three pins as OUTPUT using pinMode(). This tells the Arduino to send voltage out through these pins.
  3. Loop: The loop() function runs forever. It follows this sequence:
    • Turn red LED on (HIGH), wait 3 seconds, turn it off (LOW).
    • Turn yellow LED on, wait 1 second, turn it off.
    • Turn green LED on, wait 3 seconds, turn it off.
    • Turn yellow LED on again, wait 1 second, turn it off.
    • Then the loop repeats from the red light.

This creates the familiar traffic light pattern.

Key Concepts for Students

Common Pitfalls and Debugging Tips

Suggested Extensions

Once you have the basic traffic light working, try these enhancements:

Try It Yourself

Ready to build your own traffic light? Open the example in Velxio and start experimenting:

Open Traffic Light Example on Velxio

You can edit the code, change the timing, or add more LEDs. The simulator runs entirely in your browser – no downloads, no hardware. Use the toolbar to compile and run your code:

Velxio editor toolbar with compile and run controls

Velxio canvas toolbar above the simulation viewport

If you need to add new components, open the component picker:

Velxio component picker modal browsing parts by category

And if you want to use libraries, the Library Manager is just a click away:

Velxio Library Manager modal listing installed Arduino libraries

Happy simulating!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Button-Controlled LED: Your First Arduino Input Project
Next Post
Blink an LED with Arduino: Your First Velxio Simulation