top of page
Search

Arduino Projects for Kids: Build a Traffic Light with a Button (Full Code)

2 minutes ago
5 min read

One of the best Arduino projects for kids is a traffic light: three LEDs, one push button and about 40 lines of code. It takes an afternoon, needs no soldering, and teaches the ideas every robot uses: outputs, inputs, timing and making decisions in code. You can even build it free in your browser with Tinkercad Circuits before buying any parts. Below is the full parts list, wiring and tested code.

Why a traffic light is a great first Arduino project

Kids already know how a traffic light behaves, so they can tell right away when their program is wrong. That makes debugging feel like a puzzle, not a mystery. The project also grows nicely:

  • Step 1: make one LED blink (outputs and delays).

  • Step 2: run green, yellow, red in order (sequences).

  • Step 3: add a pedestrian button that shortens the green light (inputs and if-statements).

  • Step 4: change the timing or add a buzzer (their own ideas).

It suits kids roughly ages 9 and up who have done a little block coding. Younger kids can do it with a parent reading the code aloud.

What you need

  • An Arduino Uno (or compatible board) and USB cable

  • A small breadboard and a few jumper wires

  • One red, one yellow and one green LED

  • Three 220-ohm resistors (one per LED)

  • One push button

  • A computer with the free Arduino IDE

Most Arduino starter kits include all of this. If you are choosing a kit, see our coaches' robotics kit guide. No hardware yet? Build the same circuit in Tinkercad Circuits and run the code in the simulator.

Wiring the circuit

Wire each LED the same way. The long leg of an LED is the positive side.

  1. Pin 12 → 220-ohm resistor → long leg of the red LED. Short leg → GND.

  2. Pin 11 → 220-ohm resistor → long leg of the yellow LED. Short leg → GND.

  3. Pin 10 → 220-ohm resistor → long leg of the green LED. Short leg → GND.

  4. Push button: one side to pin 2, the other side to GND. No resistor is needed because the code turns on the Arduino's built-in pull-up resistor.

Tip: run the Arduino's GND pin to the breadboard's blue rail, then plug every short LED leg and the button into that rail. It keeps the wiring tidy and easy to check.

The full code

Copy this into the Arduino IDE, choose your board and port, and click Upload. Open the Serial Monitor at 9600 baud to watch the light change.

// Arduino Traffic Light with a Pedestrian Button
const int RED_PIN = 12;
const int YELLOW_PIN = 11;
const int GREEN_PIN = 10;
const int BUTTON_PIN = 2;   // button between pin 2 and GND

const unsigned long GREEN_TIME = 8000;   // milliseconds
const unsigned long YELLOW_TIME = 2000;
const unsigned long RED_TIME = 6000;

void setLights(int red, int yellow, int green) {
  digitalWrite(RED_PIN, red);
  digitalWrite(YELLOW_PIN, yellow);
  digitalWrite(GREEN_PIN, green);
}

// Wait up to ms milliseconds, but stop early if the button is pressed.
// Returns true if the button cut the wait short.
bool waitOrButton(unsigned long ms) {
  unsigned long start = millis();
  while (millis() - start < ms) {
    if (digitalRead(BUTTON_PIN) == LOW) {   // LOW = pressed (pull-up)
      return true;
    }
  }
  return false;
}

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  setLights(LOW, LOW, HIGH);               // green
  Serial.println("GREEN");
  if (waitOrButton(GREEN_TIME)) {
    Serial.println("Button pressed - changing soon");
    delay(1000);                           // short pause, like a real crossing
  }

  setLights(LOW, HIGH, LOW);               // yellow
  Serial.println("YELLOW");
  delay(YELLOW_TIME);

  setLights(HIGH, LOW, LOW);               // red
  Serial.println("RED - safe to cross");
  delay(RED_TIME);
}

We tested this logic before publishing by compiling it with stand-in hardware functions and simulating a button press. The light went green, cut over to yellow one second after the press, then red, then back to green on its normal timing.

How the code works

  • Constants at the top name each pin and timing, so kids change numbers in one place instead of hunting through the code.

  • setLights() is a helper function. Instead of three digitalWrite lines every time, one call sets all three LEDs. This is a good moment to talk about why programmers write functions.

  • INPUT_PULLUP keeps pin 2 at HIGH until the button connects it to GND. So in this code, LOW means pressed. Kids often find this backwards at first, and that is a useful lesson.

  • waitOrButton() waits for the green time but keeps checking the button using millis(), the Arduino's built-in clock. A plain delay() would ignore the button, because the board can't do anything else while it sleeps.

  • loop() runs green, yellow, red forever. Each color is one step, which is the same idea behind the state machines we use in robot code.

Challenges to try next

  • Add a fourth LED for a "walk" signal that only lights during red.

  • Make the yellow light blink three times before turning red.

  • Add a buzzer that beeps while it is safe to cross.

  • Ignore button presses in the first two seconds of green, like a real crossing does.

  • Swap the LEDs for a servo gate: our Arduino servo project shows how to wire one.

Common questions

What age is this Arduino project for?

Most kids around 9 to 14 can do it with a little help. Kids who have used Scratch or block coding pick it up fastest, since the logic (sequence, wait, if) is the same.

Why does my LED not light up?

The usual cause is an LED plugged in backwards. Flip it so the long leg faces the resistor. Next, check that the resistor and LED are in the same breadboard row, and that the short leg reaches GND.

Do I need to buy an Arduino to start?

No. Tinkercad Circuits is free and lets kids build and run this exact project in the browser. When they are ready for real parts, the code works unchanged on an Arduino Uno.

Keep learning with Fusion Robotics Education

If your child enjoyed this, our Arduino Basics for Beginners course walks through LEDs, buttons, sensors and motors step by step. You can start with our free self-paced courses in the course catalog, try All-Access for $39/month with a 7-day free trial, or book Remote 1-on-1 live lessons ($149/month) by texting (619) 786-5731.

Live in San Diego? Our weekend group sessions in Scripps Ranch are 1.5 hours a week for ages 6 to 18, with no more than 10 kids, and every child works on their own project at their own pace. The first session is free: text (619) 786-5731 to book a spot.

 
 
 

Comments


bottom of page