Count one Arduino button event per press
Predict events from a stable active-LOW button trace and build an Arduino Uno edge detector with a checked code-order exercise.
0 of 3 complete · Progress saved on this device
Predict edges from stable samples
A pull-up gives D2 a stable HIGH when the switch is open. Closing the switch connects D2 to ground. A press is the transition from HIGH to LOW. Repeated LOW samples show that the same press is still held.
Put the loop in order
Compare the current sample with the previous sample before updating the saved value. Updating the saved value first would compare the input with itself.
Try a held press and a second press
In the modeled circuit, hold the button for several iterations. Serial should print only once for that falling edge. Release and press again for the next count. Physical contacts may bounce between levels during a single press, so use a debounced state as the input to this edge detector if hardware counts extra presses.
Open the exact Uno example
bool previous;
unsigned int count = 0;
void setup() {
pinMode(2, INPUT_PULLUP);
previous = digitalRead(2) == LOW;
Serial.begin(9600);
}
void loop() {
const bool pressed = digitalRead(2) == LOW;
if (pressed && !previous) Serial.println(++count);
previous = pressed;
}Verified with [email protected] on Uno R3 (2026-12-05 UTC). The matching circuit passed its Uno model checks. This check did not use physical hardware.