Arduino - Button Library

When using buttons, beginners usually run into the following troubles:

The ezButton (easy button) library is designed to solve all of the above problems and make it easy to use for not only beginners but also experienced users. It is created by ArduioGetStarted.com.

The library can be used for push button, momentary switches, toggle switch, magnetic contact switch (door sensor)..​.

Arduino button library feature

Library Features

  • Uses the internal pull-up resistor to avoid the floating value
  • Supports debounce to eliminate the chattering phenomenon
  • Supports the pressed and released events
  • Supports the counting (for FALLING, RISING and BOTH)
  • Easy to use with multiple buttons
  • All functions are non-blocking

※ NOTE THAT:

If this library is useful for your work, you can say thanks to us by buying us a coffee: PayPal.Me/arduinogetstarted

How To Install Library

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “ezButton”, then find the button library by ArduinoGetStarted
  • Click Install button to install ezButton library.
Arduino button library

Or you can download it on Github

Functions

  • ezButton()
  • setDebounceTime()
  • getState()
  • getStateRaw()
  • isPressed()
  • isReleased()
  • setCountMode()
  • getCount()
  • resetCount()
  • loop()

References

ezButton()

Description

This function creates a new instance of the ezButton class that represents a particular button attached to your Arduino board.

Syntax

ezButton(pin)

Parameters

pin: int - Arduino's pin that connects to the button.

Return

A new instance of the ezButton class.

Example

ezButton button(7);

setDebounceTime()

Description

This function is used to set the debounce time. The debounce time takes effect on getState(), isPressed(), isReleased() and getCount().

Syntax

button.setDebounceTime(time);

Parameters

time: unsigned long - debounce time in milliseconds

Return

None

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { button.setDebounceTime(100); // set debounce time to 100 milliseconds } void loop() {}

getState()

Description

This function returns the state of the button. If the debounce time is set, the returned state is the state after debouncing. We MUST call button.loop() function before using this function.

Syntax

button.getState();

Parameters

None

Return

The state of button. Since the ezButton library used the internal pull-up resistor, the state will be HIGH (1) when no press, and LOW (0) when pressed. If the debounce time is set, the returned state is the state after debouncing.

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { } void loop() { button.loop(); // MUST call the loop() function first int btnState = button.getState(); }

getStateRaw()

This function returns the current state of the button without debouncing. It is equivalent to digitalRead().

Description

Syntax

button.getStateRaw()

Parameters

None

Return

The current state of button without debouncing.

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { } void loop() { int btnState = button.getStateRaw(); }

isPressed()

Description

This function check whether a button is pressed or not. We MUST call button.loop() function before using this function.

Syntax

button.isPressed()

Parameters

None

Return

true if the button is pressed, false otherwise

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) Serial.println("The button is pressed"); }

isReleased()

Description

This function check whether a button is released or not. We MUST call button.loop() function before using this function.

Syntax

button.isReleased()

Parameters

None

Return

true if the button is released, false otherwise

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); } void loop() { button.loop(); // MUST call the loop() function first if(button.isReleased()) Serial.println("The button is released"); }

setCountMode()

Description

This function sets the count mode.

Syntax

button.setCountMode(mode)

Parameters

mode: int - count mode. The available count modes include: COUNT_FALLING, COUNT_RISING and COUNT_BOTH.

Return

None

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); Serial.println(count); }

getCount()

Description

This function returns the count value. We MUST call button.loop() function before using this function.

Syntax

button.getCount()

Parameters

None.

Return

The count value. If the debounce time is set, the returned value is the value after debouncing.

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); Serial.println(count); }

resetCount()

Description

This function set the counter value to 0.

Syntax

button.resetCount()

Parameters

None.

Return

None.

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setCountMode(COUNT_FALLING); } void loop() { button.loop(); // MUST call the loop() function first unsigned long count = button.getCount(); if(count >= 100) button.resetCount(); }

loop()

loop()

Description

This function does debounce and updates the state of the button. It MUST be called before using getState(), isPressed(), isReleased() and getCount() functions.

Syntax

button.loop();

Parameters

None

Return

None

Example

#include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(100); // set debounce time to 100 milliseconds } void loop() { button.loop(); // MUST call the loop() function first int btnState = button.getState(); Serial.println(btnState); if(button.isPressed()) Serial.println("The button is pressed"); if(button.isReleased()) Serial.println("The button is released"); }

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES