Sub topic 12
Toggling an LED on Each Press
Assume you know the basic Arduino setup() and loop() structure, how to use a breadboard, and how digitalWrite() controls an LED. This chapter combines three ideas: active-low button wiring, software debouncing, and edge detection.
A toggle changes an output from one state to the other. Each complete button press changes the LED state once:
- LED off becomes LED on.
- LED on becomes LED off.
This differs from level-based code, where the LED stays on only while the button is held.
Use a stable button state
With the internal pull-up resistor, connect one side of the button to digital pin 2 and the other side to GND. Set the pin to INPUT_PULLUP.
The input has inverted logic:
| Physical condition | Pin reading | Meaning |
|---|---|---|
| Button released | `HIGH` | No connection to GND |
| Button pressed | `LOW` | Pin is connected to GND |
A mechanical button does not change cleanly. Its contacts can rapidly open and close for a few milliseconds. This is called contact bounce. Without debouncing, one physical press might look like several presses.
The program below accepts a new reading only after it has stayed unchanged for 25 milliseconds.
Detect the press transition
The program stores three important values:
lastReadingis the newest raw reading from the pin.stableStateis the last reading accepted as reliable.ledOnstores the desired LED state.
The code detects a press when stableState changes to LOW. This is a transition from released to pressed. Because the transition is accepted only after debouncing, the LED toggles once per press.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
const unsigned long debounceMs = 25;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChangeTime = 0;
bool ledOn = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastReading) {
lastChangeTime = millis();
lastReading = reading;
}
bool readingIsStable =
(millis() - lastChangeTime) >= debounceMs;
if (readingIsStable && reading != stableState) {
stableState = reading;
if (stableState == LOW) {
ledOn = !ledOn;
digitalWrite(ledPin, ledOn ? HIGH : LOW);
}
}
}The expression ledOn = !ledOn means “replace ledOn with its opposite.” If it is false, it becomes true. If it is true, it becomes false.
The conditional operator, written as condition ? valueIfTrue : valueIfFalse, selects the output level. Therefore, ledOn ? HIGH : LOW sends HIGH when the stored state is true and LOW when it is false.
Follow the state changes
The program does not toggle when the button remains held. After the first press, stableState is already LOW, so later loop iterations do nothing until the button is released and pressed again.
When interrupts are useful
An interrupt can notify the processor immediately when an input changes. For a simple button, polling with digitalRead() in loop() is usually easier to understand and debounce. Interrupt handlers have restrictions and can respond to bounce too, so they do not remove the need for careful button handling.
Exercise
Why does the example toggle the LED only when the stable state becomes LOW, rather than on every loop iteration where the button reads LOW?
Show answer
The transition to LOW happens once per press. Testing the level on every iteration would repeatedly change the LED while the button was held.