A lightweight, non-blocking Arduino library for physical push buttons.
ARDUniaButton provides a simple polling API and optional callbacks for debounced press/release events, single/double/multi-click detection, long presses, and repeat-while-held behavior. It uses millis(), requires no third-party dependencies, and avoids dynamic memory allocation.
- Non-blocking debounce using
millis() - Default wiring: digital pin → button → GND, using
INPUT_PULLUP - Active-LOW and Active-HIGH logic
INPUT,INPUT_PULLUP, andINPUT_PULLDOWNwhen the selected Arduino core defines it- Press and release events
- Single, double, and multi-click detection
- Long-press detection and held-state reporting
- Configurable repeat delay and interval while held
- Polling and callback APIs
- Independent one-shot event flags
- Safe elapsed-time comparisons across
millis()rollover - No third-party dependencies or dynamic allocation
Once ARDUniaButton is accepted into the Arduino Library Manager index, open Tools → Manage Libraries… (or Sketch → Include Library → Manage Libraries… in Arduino IDE 1.x), search for ARDUniaButton, and install it.
Until the Library Manager listing is available, download the ZIP from the GitHub repository, then select Sketch → Include Library → Add .ZIP Library… in Arduino IDE.
The default configuration expects a button connected between a digital GPIO pin and GND:
Arduino GPIO ----[ BUTTON ]---- GND
The internal pull-up is enabled by default, so no external pull-up resistor is required for this wiring.
#include <ARDUniaButton.h> ARDUniaButton button(7); // INPUT_PULLUP, active LOW void setup() { Serial.begin(115200); if (!button.begin()) { Serial.println("Invalid button configuration"); } } void loop() { button.update(); if (button.wasPressed()) Serial.println("Pressed"); if (button.wasReleased()) Serial.println("Released"); if (button.wasClicked()) Serial.println("Click"); if (button.wasDoubleClicked()) Serial.println("Double click"); if (button.wasMultiClicked()) { Serial.print("Multi click: "); Serial.println(button.clickCount()); } if (button.wasLongPressed()) Serial.println("Long press"); if (button.wasRepeated()) Serial.println("Repeat"); }
Call update() regularly from loop(). The library does not create its own timer or interrupt.
| Setting | Default |
|---|---|
| Debounce | 30 ms |
| Long press | 1000 ms |
| Double-click window | 400 ms |
| Repeat initial delay | 500 ms |
| Repeat interval | 100 ms |
Configure timings with:
button.setDebounce(30); button.setLongPressTime(1000); button.setDoubleClickTime(400); button.setRepeat(500, 100);
The setters return true when a value is accepted and false when it is outside the supported range.
wasPressed()andwasReleased()report debounced transitions.wasLongPressed()fires once when the long-press threshold is reached.wasRepeated()fires at the configured repeat interval after the repeat delay.- A long press is not counted as a click.
- Click events are delayed until the double-click window expires so a second press can be detected.
- The
wasXxx()event flags are independent and remain available until the next call toupdate(). event()returns the last event generated by the most recentupdate(). If several events occur during one update, use the individualwasXxx()flags to check them all.clickCount()returns the count for a multi-click event; it is meaningful whenwasMultiClicked()is true.
Register callbacks before or after begin():
button.onClick([]() {
Serial.println("Click");
});
button.onLongPress([]() {
Serial.println("Long press");
});
button.onRepeat([](uint8_t count) {
Serial.println(count);
});Supported callbacks are onPress(), onRelease(), onClick(), onDoubleClick(), onMultiClick(), onLongPress(), and onRepeat().
Active-HIGH input can be configured when the hardware provides an appropriate pull-down or external resistor:
ARDUniaButton button(7, INPUT, false);
The classic Arduino AVR core used by Arduino UNO does not define INPUT_PULLDOWN. ARDUniaButton only references that symbol when the selected core defines it.
- BasicUsage — polling API and the main events
- Callbacks — callback-based event handling
- VolumeControl — repeat behavior for up/down controls
The library uses standard Arduino APIs and has no third-party dependencies. It is intended for Arduino-compatible cores that provide Arduino.h, millis(), pinMode(), and digitalRead(). Compatibility claims should be verified against the specific board core and version in use.
The host-side test suite simulates Arduino GPIO and time, including basic click, double-click, long-press, and repeat behavior. Run it from the repository root:
sh test/host/run_test.sh
GitHub Actions runs the host tests and checks library metadata with Arduino Lint.
ARDUniaButton is an open-source project from ARDUnia, created by Hamidreza Milaninia.
- ARDUnia: https://ardunia.ir
- Author: https://hrmnia.com
- Repository: https://github.com/ARDUnia/ARDUniaButton
MIT. See LICENSE.