Sub topic 11
Detecting a Press Instead of a Held Level
Assume you know the basic Arduino sketch structure, pinMode, digitalRead, digitalWrite, and button wiring. This chapter uses the internal pull-up resistor, so the button is connected between the input pin and GND.
A level is not an event
digitalRead reports the button's current level:
HIGHmeans the input voltage is near the board's logic-high voltage.LOWmeans the input voltage is near 0 V.
With INPUT_PULLUP, the input is normally HIGH. Pressing the button connects it to GND, so it becomes LOW. This is called active-low logic because the pressed state is represented by LOW.
Suppose your loop checks the button many thousands of times per second:
if (digitalRead(buttonPin) == LOW) {
// The button is currently pressed.
}That code detects a held level. The body runs repeatedly for as long as the button remains pressed. This is correct for an action such as keeping a motor running while a button is held. It is wrong when one press should cause one action, such as toggling an LED.
A press is a state change, or edge. For an active-low button, the press edge is the transition from HIGH to LOW.
| Button state | Input level | Meaning |
|---|---|---|
| Released | `HIGH` | No press |
| Pressed | `LOW` | Press is active |
Compare the current and previous states
To detect an edge, store the previous reading and compare it with the new reading. A press occurs only when:
previous state = HIGH
current state = LOWAfter processing the reading, save it as the previous state. The next loop can then compare against it.
flowchart TD
A[Read button] --> B{Previous HIGH and current LOW?}
B -- Yes --> C[Handle one press]
B -- No --> D[Do nothing]
C --> E[Save current state]
D --> E
E --> AThis simple method assumes that the input changes cleanly. Real mechanical buttons do not always do that.
Debounce before detecting the edge
Button contacts can briefly alternate between open and closed when they touch. This behavior is called contact bounce. Without debouncing, one physical press may look like several HIGH-to-LOW transitions.
The program below first waits until the raw reading has stayed unchanged for 25 milliseconds. It then treats that reading as stable. Only a stable transition to LOW toggles the LED.
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);
}
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);
}
}
}lastReading tracks the latest raw value. stableState tracks the last accepted value. The LED changes only when the accepted state becomes LOW, so holding the button does not repeatedly toggle it.
When to use level detection
Use a level test when the action should continue while the button is held:
if (digitalRead(buttonPin) == LOW) {
// Keep performing a held-button action.
}Use debounced edge detection when the action should happen once per press. For simple projects, polling in loop is usually easier to understand than an interrupt. Interrupts can react quickly to a pin change, but they still need careful debouncing and should not perform lengthy work inside the interrupt handler.
Exercise
In the program above, why does the LED toggle only once while the button remains pressed?
Show answer
The program toggles the LED only when stableState changes to LOW. While the button is held, stableState remains LOW, so no new press transition is detected.