Mission 06 · Stage 1

Alternate LEDs

alternating output states

Turn two LEDs on and off in opposite states.

Alternate LEDs circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 12

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 13

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

See it

Let's make two lights take turns!

When the red light is on, the green one is off — they swap back and forth like a seesaw.

You see this in car turn signals and railway crossing lights!

The story

The problem

We want two lights that are never on at the same time — they should take turns.

Think of it like

It's like a seesaw — when one side goes up, the other side goes down.

Meet the parts

It thinks and tells the two lights when to swap.

Arduino

The brain

Loading part…

Glows red when it is its turn.

Red LED

The first light

Loading part…

Glows green when it is its turn.

Green LED

The second light

Loading part…

Keeps the red light safe from too much power.

Red resistor

The protector

Loading part…

Keeps the green light safe from too much power.

Green resistor

The protector

Loading part…

How it works

1

First light on

The first light (pin 12) turns on while the second light (pin 13) stays off.

digitalWrite(LED_A, HIGH);
digitalWrite(LED_B, LOW);
2

Wait a little

Both lights hold still for a moment so you can see which one is glowing.

delay(300);
3

Swap to the other

Now they trade places — the first light goes off and the second one glows. Just like a seesaw!

digitalWrite(LED_A, LOW);
digitalWrite(LED_B, HIGH);
4

Wait, then repeat

Another short wait, then loop() jumps back and swaps the lights again!

delay(300);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed — ready to build!

    Loading part…
  2. 2

    Place Red LED

    Place the Red LED (ledA) on the breadboard.

    Loading part…
  3. 3

    Place Green LED

    Place the Green LED (ledB) on the breadboard.

    Loading part…
  4. 4

    Place Red resistor

    Place the Red resistor (rA) on the breadboard.

    Loading part…
  5. 5

    Place Green resistor

    Place the Green resistor (rB) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 12 to Red resistor (rA) 2

    Connect Arduino pin 12 to Red resistor (rA) 2.

  7. 7

    Connect Red resistor (rA) 1 to Red LED (ledA) anode (+)

    Connect Red resistor (rA) 1 to Red LED (ledA) anode (+).

  8. 8

    Connect Red LED (ledA) cathode (-) to Arduino GND

    Connect Red LED (ledA) cathode (-) to Arduino GND.

  9. 9

    Connect Arduino pin 13 to Green resistor (rB) 2

    Connect Arduino pin 13 to Green resistor (rB) 2.

  10. 10

    Connect Green resistor (rB) 1 to Green LED (ledB) anode (+)

    Connect Green resistor (rB) 1 to Green LED (ledB) anode (+).

  11. 11

    Connect Green LED (ledB) cathode (-) to Arduino GND

    Connect Green LED (ledB) cathode (-) to Arduino GND.

Try it

  • Press the green Run button — red and green should never glow brightly at the same time.
  • Watch them take turns, back and forth, like a blinker!

Peek at code

The two light nicknames

const int LED_A = 12;
const int LED_B = 13;

LED_A is pin 12 and LED_B is pin 13. These are the two lights that take turns.

Get both lights ready

void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}

setup() gets both pins ready so we can turn each light on and off.

The swapping loop

void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(300);
}

Each half of loop() turns one light on and the other off, with a short wait between swaps.

Show full sketch (alternate-leds.ino)
const int LED_A = 12;
const int LED_B = 13;
void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}
void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(300);
}

Quick quiz

Q1. Which part runs again and again?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Yes! loop() runs again and again, forever.

Q2. When the first light is ON, the second light should be…

  • A. OFF (the opposite)
  • B. ON (the same)
  • C. Unplugged
Why: Yes! Taking turns means when one is on, the other is off.

Code lab — try on your own

  1. Make the lights swap faster — change both delay(300) to delay(150).

    Hint: There are two matching delay lines inside loop().

  2. Add a little note (comment) on the line that turns the first light on.

    Hint: Type // and your words at the end of line 8.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

Two lights take turns — one glows while the other is dark, again and again.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

const int LED_A = 12;
const int LED_B = 13;

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets pin 12 and pin 13 ready for our two lights.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the two lights swap turns over and over.

Why here

Things that repeat go inside loop().

void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(300);
}

Try this: Change a delay number inside loop(), then press Run to make the lights swap faster.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It makes pin 12 ready to push power to the first light.

Why here

It goes in setup() because we only set it once.

  pinMode(LED_A, OUTPUT);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights up a light, OFF turns it dark. Here it turns the first light on.

Why here

It goes in loop() so the lights can keep changing.

  digitalWrite(LED_A, HIGH);

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It holds the lights still long enough for you to see who is on.

Why here

Right after we set the two lights.

  delay(300);