ESP8266 - Button Count - LCD
This tutorial instructs you how to use ESP8266 to count the button's press and display the counted number on LCD display. It is possible to modify this for use with different sensors instead of the button.
In this tutorial, we will debounce the button without using the delay() function. For more information, please take a look at 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.
Additionally, some of these links are for products from our own brand, DIYables.
If you are unfamiliar with LCD I2C and button (pinout, how it works, how to program ...), the following tutorials can help you learn:
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
ezButton button(13);
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;
}
}
To get started with ESP8266 on Arduino IDE, follow these steps:
Wire the components as shown in the diagram.
Connect the ESP8266 board to your computer using a USB cable.
Open Arduino IDE on your computer.
Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
Click to the Libraries icon on the left bar of the Arduino IDE.
Look for “ezButton” and locate the button library from ArduinoGetStarted.
Press the Install button to install the ezButton library.
Search for “LiquidCrystal I2C” and locate the LiquidCrystal_I2C library created by Frank de Brabander.
Then, click the Install button to add the library.
Copy the code and open it in the Arduino IDE.
Click the Upload button to send the code to the ESP8266.
Push the button multiple times.
Check out the count on the LCD changing.
※ NOTE THAT:
The address of the LCD may differ depending on the manufacturer. We used 0x27 in our code, as specified by DIYables.