Mission 14 · Stage 2

Toggle Button

edge detection and toggle latch

Each button press toggles an LED on or off using edge detection.

Toggle Button circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

Let's make a tap turn it on and off!

Tap once and the light glows. Tap again and it goes dark — like a real power button.

The power buttons on TVs and game consoles flip on and off just like this.

The story

The problem

Holding a button keeps the light on only while you hold. We want one tap to flip it and keep it.

Think of it like

Like flipping a coin once each time you tap — not while your finger rests on it.

Meet the parts

It remembers the light and flips it on each tap.

Arduino

The brain

Loading part…

Each tap flips the light to the other state.

Button

The button

Loading part…

Slows the power down so the light does not get hurt.

Resistor

The protector

Loading part…

It stays on until your next tap.

LED

The light

Loading part…

How it works

1

Remember last reading

We compare this new reading with lastReading to see if something changed.

bool reading = digitalRead(BUTTON_PIN);
2

Catch the press moment

Going from not-pressed to pressed means you just tapped — that is one flip.

if (lastReading == HIGH && reading == LOW)
3

Flip and remember

ledOn remembers the light, so it stays on or off between taps.

ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);

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 Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  6. 6

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

    Connect Resistor (r1) 2 to LED (led1) anode (+)

    Connect Resistor (r1) 2 to LED (led1) anode (+).

  9. 9

    Connect LED (led1) cathode (-) to Arduino GND

    Connect LED (led1) cathode (-) to Arduino GND.

Try it

  • Tap the button — the light flips with each tap.
  • Hold it down — it should NOT flicker on and off fast.

Peek at code

Memory boxes

bool ledOn = false;
bool lastReading = HIGH;

ledOn remembers the light; lastReading remembers what the button looked like before.

Catching the press

void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

Only a fresh press flips the light — holding the button does nothing extra.

Show full sketch (toggle-button.ino)
const int BUTTON_PIN = 2;
const int LED_PIN = 13;
bool ledOn = false;
bool lastReading = HIGH;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}
void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

Quick quiz

Q1. Where does work that repeats go?

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

Q2. Why do we check lastReading == HIGH && reading == LOW?

  • A. To catch the exact moment of a press
  • B. To make the light brighter
  • C. To run setup() again
Why: Yes! It fires once per press, not the whole time you hold.

Code lab — try on your own

  1. Make button checks snappier — change delay(10) to delay(5).

    Hint: It's the last line inside loop().

  2. Add a note (comment) on the line ledOn = !ledOn that says "flip the light".

    Hint: That's line 13.

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

One tap turns the light on. The next tap turns it off.

Tip

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

const int BUTTON_PIN = 2;
const int LED_PIN = 13;
bool ledOn = false;
bool lastReading = HIGH;

setup()

Big idea

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

In this project

It gets the button ready to listen, pin 13 ready for the light, and starts the light off.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

loop()

Big idea

loop() runs again and again, forever.

In this project

It watches for the moment you press, then flips the light.

Why here

Things that repeat go inside loop().

void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

Try this: Change a number inside loop(), then press Run to see what happens.

pinMode

Big idea

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

In this project

It makes pin 2 listen to the button and pin 13 push power to the light.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

It starts the light off so we begin in a known state.

Why here

This one is in setup() so the light begins dark.

  digitalWrite(LED_PIN, LOW);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It reads the button each loop to spot a fresh press.

Why here

It goes in loop() so we can catch the moment you press.

  bool reading = digitalRead(BUTTON_PIN);

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

A tiny wait keeps button reads steady and clean.

Why here

Right after we check the button.

  delay(10);