Arduino library · Input

ezButton: a debounced button in four lines

ezButton, from ArduinoGetStarted.com, is the button library many beginner tutorials start with. You make one object per button, set a debounce time, call loop() on it every pass, and ask isPressed() or isReleased(). Both are events: they are true once, on the pass where the press or release happened, so a held button does not keep firing.

Built in on Mokxi: #include <ezButton.h> and it builds, with nothing to install. Free, and no account needed.

What ezButton does

By default the pin uses the board’s internal pull-up, so the button goes between the pin and GND with no resistor, and pressed reads as LOW. ezButton also counts presses for you with getCount(), and setCountMode() decides whether it counts presses, releases or both. getState() gives the steady, debounced level when you need to know whether the button is down right now.

One detail matters: the debounce time starts at 0, which means no debouncing at all. A real button bounces for a few milliseconds and would be counted several times, so always call setDebounceTime(50) in setup(), as the runnable example does. The simulated button bounces like a typical one, so you can see the difference by taking that line out.

Library Manager nameezButton
AuthorArduinoGetStarted.com
Include#include <ezButton.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesPushbutton

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <ezButton.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ezButton" by ArduinoGetStarted.com from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ezButton to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.6, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

ezButton button(2)

A button on pin 2 with the internal pull-up on. Wire it between the pin and GND.

button.setDebounceTime(50)

A level must hold for 50 ms before it counts. The default is 0, so set it.

button.loop()

Reads and debounces the pin. Call it first thing in loop(), every pass; the other calls report what it saw.

button.isPressed()

True on the one pass where a press was confirmed.

button.loop();
if (button.isPressed()) {
  ledOn = !ledOn;
  digitalWrite(13, ledOn);
}

button.isReleased()

True on the one pass where the button was let go.

button.getState()

The steady level: LOW while pressed with the default pull-up, HIGH while released.

button.getCount()

How many presses so far. resetCount() sets it back to zero, and setCountMode(COUNT_BOTH) counts releases too.

Example: Pressed and released, debounced

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <ezButton.h>

ezButton button(2);  // to GND, with the internal pull-up

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50);
}

void loop() {
  button.loop();
  if (button.isPressed()) Serial.println("The button is pressed");
  if (button.isReleased()) Serial.println("The button is released");
}

Wiring

  • Arduino Uno R3 2 to Pushbutton 1a
  • Arduino Uno R3 GND to Pushbutton 2a

Open it in the editor, wired and ready to run

Common errors and how to fix them

One press is counted two or three times

The debounce time is still 0, its default. Add button.setDebounceTime(50) in setup(); raise it to 80 or 100 for a particularly noisy switch.

isPressed() never becomes true

button.loop() is not being called, or is called only now and then. It must run on every pass of loop(), before isPressed(), and loop() must not sit in long delays.

The button reads pressed all the time

It is wired to 5 V while the default pull-up mode expects GND. Move the wire to GND, or create the button with ezButton(2, INTERNAL_PULLDOWN) on a board that has pull-downs.

Try ezButton without installing anything

Open the example, change the sketch, and keep your version in a free account.