Arduino - Button Count - LCD
In this tutorial, we are going to use Arduino:
Count the number of times a button is pressed
Display the count number on LCD I2C display.
Auto vertical and horizontal center align the count number on LCD I2C display.
In this tutorial, the button also is debounced without using delay() function. See Why do we need debouncing?
Or you can buy the following sensor kits:
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.
If you do not know about LCD I2C and button (pinout, how it works, how to program ...), learn about them in the following tutorials:
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(13);
unsigned long lastCount = 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 (lastCount != count) {
Serial.println(count);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(count);
lastCount != count;
}
}
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.
Search “LiquidCrystal I2C”, then find the LiquidCrystal_I2C library by Frank de Brabander
Click Install button to install LiquidCrystal_I2C library.
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to Arduino
Press button several times
See the counting number changed on LCD
※ NOTE THAT:
The I2C address of LCD can vary according to the manufacturers. In the code, we used 0x27 that is specified by DIYables manufacturer
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
※ OUR MESSAGES
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!