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 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.
Additionally, some of these links are for products from our own brand,
DIYables .
Buy Note: Alternatively, you can assemble the LCD I2C display using LCD 1602 Display and PCF8574 I2C Adapter Module.
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
You can also build this circuit without a breadboard. Here is the same wiring:

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
#include <DIYables_LCD_I2C.h>
#include <ezButton.h>
DIYables_LCD_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.