Arduino Nano - Button Count - LCD
This tutorial instructs you how to use Arduino Nano to count the button's press and display the value on LCD display. It is possible to adapt this for alternative sensors, rather than just the button.
In this tutorial, we will be debouncing the button without using the delay() function. For further information on why debouncing is necessary, please refer to Why do we need debouncing?.
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.
If you are unfamiliar with LCD I2C and button (pinout, how it works, how to program ...), the following tutorials can help you learn:
This image is created using Fritzing. Click to enlarge image
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
ezButton button(4);
unsigned long prev_count = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
button.setDebounceTime(50);
button.setCountMode(COUNT_FALLING);
}
void loop() {
button.loop();
unsigned long count = button.getCount();
if (prev_count != count) {
Serial.println(count);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(count);
prev_count != count;
}
}
Click to the Libraries icon on the left bar of the Arduino IDE.
Search for “ezButton” and locate the button library by ArduinoGetStarted.
Press the Install button to install the ezButton library.
Copy the code and open it in the Arduino IDE.
Hit the Upload button to transfer it to the Arduino Nano.
Press the button a few times.
Check out the count changing on the LCD.
※ NOTE THAT:
The I2C address of LCD can differ depending on the manufacturer. In the code, we have used 0x27 which is specified by DIYables manufacturer.