A lightweight Arduino library for handling push buttons with software debouncing.
- Software debouncing
- Button press detection (
click()) - Button release detection (
unclick()) - Double-click detection (
doubleClick()) - Hold-duration detection (
press()) - Current button state detection (
pressed()) - Configurable debounce and double-click intervals
- Support for
INPUTandINPUT_PULLUP - Non-blocking operation using
millis()
In the Arduino IDE, open Tools > Manage Libraries... (or click the Library Manager icon), search for AButton, and click Install.
Then include the library in your sketch:
#include <AButton.h>The library supports two input configurations:
- External pull-up or pull-down resistor: create the
AButtonobject withfalse(the default). Use an external resistor to ensure the input has a defined logic level when the button is not pressed. - Internal pull-up resistor: create the object with
true. Connect one side of the button to the pin and the other side to GND.
When using INPUT_PULLUP, the library automatically inverts the pin reading. A pressed button is represented internally as true.
#include <AButton.h>
AButton button(2, true);
void setup() {
Serial.begin(9600);
button.begin();
}
void loop() {
button.update();
if (button.click()) {
Serial.println("Button pressed");
}
if (button.unclick()) {
Serial.println("Button released");
}
if (button.doubleClick()) {
Serial.println("Double click detected");
}
if (button.press(1000)) {
Serial.println("Button held for at least 1 second");
}
}Important: Call update() on every iteration of loop(). The event methods depend on it.
| Method | Description |
|---|---|
AButton(pin, pullup) |
Creates a button instance. pullup defaults to false. |
begin() |
Configures the pin and initializes the button state. |
update() |
Reads the input and updates the debounced state. Call it repeatedly in loop(). |
click() |
Returns true during the update cycle in which the button becomes pressed. |
unclick() |
Returns true during the update cycle in which the button becomes released. |
doubleClick() |
Returns true during the update cycle in which a second press is detected within the configured interval after the first release. |
press(duration) |
Returns true only once when the button has been held for the specified duration. It will not trigger again until the button is released and pressed again. |
pressed() |
Returns the current debounced button state. |
setDebounce(ms) |
Sets the debounce interval in milliseconds. The default is 50 ms. |
setDoubleClickInterval(ms) |
Sets the maximum time, in milliseconds, between the first release and the second press. The default is 400 ms. |
A double click is detected when the second press begins within the configured interval after the first click has been released. The doubleClick() event is reported on that second press.
The existing click() event is still reported immediately on every press. Therefore, click() does not wait to determine whether the press will become part of a double click.
press(duration)is a level-style check, not a one-time event. It remainstrueon every call while the button stays pressed after the duration has elapsed.- The default debounce interval is 50 ms.
- The default double-click interval is 400 ms.
- When using
INPUT, provide an external resistor so the input does not float. - The library uses
millis()and does not use blocking delays.