ESP32 - RTC Module - LCD

In this tutorial, we will guide you through the process of creating an LCD clock using an ESP32 with the following steps:

You have the option to choose between two RTC modules: DS3231 and DS1307. To assist you in making an informed decision, refer to the comparison outlined in DS3231 vs DS1307.

This tutorial will provide a step-by-step guide on integrating ESP32 with either the DS3231 or DS1307 RTC module to display accurate date and time information on a 16x2 LCD I2C screen.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×LCD I2C
1×Real-Time Clock DS3231 Module
1×(Alternatively) Real-Time Clock DS1307 Module
1×CR2032 battery
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to LCD, DS3231 and DS1307 RTC module

Unfamiliar with LCD, DS3231 and DS1307, including their pinouts, functionality, and programming? Explore comprehensive tutorials on these topics below:

Install LCD and RTC Libraries

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “LiquidCrystal I2C”, then find the LiquidCrystal_I2C library by Frank de Brabander
  • Click Install button to install LiquidCrystal_I2C library.
ESP32 LiquidCrystal I2C library
  • Search “RTClib”, then find the RTC library by Adafruit
  • Click Install button to install RTC library.
ESP32 RTC library
  • You may be asked to install dependencies for the library
  • Install all dependencies for the library by clicking on Install All button.
ESP32 Adafruit BusIO library

Reading time from DS3231 RTC module and display it on LCD

Wiring Diagram

ESP32 DS3231 LCD Wiring Diagram

This image is created using Fritzing. Click to enlarge image

ESP32 Code - DS3231 and LCD

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rtc-module-lcd */ #include <LiquidCrystal_I2C.h> #include <RTClib.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows RTC_DS3231 rtc; void setup() { Serial.begin(9600); lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight // SETUP RTC MODULE if (!rtc.begin()) { Serial.println("RTC module is NOT found"); Serial.flush(); while (true) ; } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); int hour = now.hour(); int minute = now.minute(); int second = now.second(); lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Date: "); lcd.print(year); lcd.print("/"); lcd.print(month); lcd.print("/"); lcd.print(day); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Time: "); lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second); delay(1000); // Update every second }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • See the result on LCD

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

Reading time from DS1307 RTC module and display it on LCD

Wiring Diagram

ESP32 DS1307 LCD Wiring Diagram

This image is created using Fritzing. Click to enlarge image

ESP32 Code - DS1307 and LCD

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-rtc-module-lcd */ #include <LiquidCrystal_I2C.h> #include <RTClib.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows RTC_DS1307 rtc; void setup() { Serial.begin(9600); lcd.init(); // initialize the lcd lcd.backlight(); // open the backlight // SETUP RTC MODULE if (!rtc.begin()) { Serial.println("RTC module is NOT found"); Serial.flush(); while (true) ; } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); int hour = now.hour(); int minute = now.minute(); int second = now.second(); lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Date: "); lcd.print(year); lcd.print("/"); lcd.print(month); lcd.print("/"); lcd.print(day); lcd.setCursor(0, 1); // start to print at the second row lcd.print("Time: "); lcd.print(hour); lcd.print(":"); lcd.print(minute); lcd.print(":"); lcd.print(second); delay(1000); // Update every second }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • See the result on LCD

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES