It remembers the light and flips it on each tap.
Arduino
The brain
edge detection and toggle latch
Each button press toggles an LED on or off using edge detection.

Pin connections
| Part 1 | Part 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 |
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 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.
It remembers the light and flips it on each tap.
Arduino
The brain
Each tap flips the light to the other state.
Button
The button
Slows the power down so the light does not get hurt.
Resistor
The protector
It stays on until your next tap.
LED
The light
Remember last reading
We compare this new reading with lastReading to see if something changed.
bool reading = digitalRead(BUTTON_PIN);
Catch the press moment
Going from not-pressed to pressed means you just tapped — that is one flip.
if (lastReading == HIGH && reading == LOW)
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
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed — ready to build!
Place Button
Place the Button (btn1) on the breadboard.
Place Resistor
Place the Resistor (r1) on the breadboard.
Place LED
Place the LED (led1) on the breadboard.
Connect Arduino pin 2 to Button (btn1) 1.l
Connect Arduino pin 2 to Button (btn1) 1.l.
Connect Button (btn1) 2.l to Arduino GND
Connect Button (btn1) 2.l to Arduino GND.
Connect Arduino pin 13 to Resistor (r1) 1
Connect Arduino pin 13 to Resistor (r1) 1.
Connect Resistor (r1) 2 to LED (led1) anode (+)
Connect Resistor (r1) 2 to LED (led1) anode (+).
Connect LED (led1) cathode (-) to Arduino GND
Connect LED (led1) cathode (-) to Arduino GND.
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.
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);
}
Q1. Where does work that repeats go?
Q2. Why do we check lastReading == HIGH && reading == LOW?
Make button checks snappier — change delay(10) to delay(5).
Hint: It's the last line inside loop().
Add a note (comment) on the line ledOn = !ledOn that says "flip the light".
Hint: That's line 13.
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);