Internet Clock with ESP32 & LCD Display Using NTP Client and Arduino Code

The ESP32 is a powerful, feature-packed microcontroller that includes support for Wi-Fi, Bluetooth, and a variety of peripherals. One interesting application of the ESP32 is as an NTP (Network Time Protocol) clock, which can be used to display the current time on an LCD screen.

Setting up an ESP32 NTP clock is relatively straightforward, as the ESP32 has built-in support for NTP and there are many libraries available to assist with the LCD display. Once configured, the ESP32 NTP clock can be used to keep track of the time with high accuracy, even when disconnected from the internet.

Now we will design a NTP based clock using ESP32.

Components Required

To design a NTP internet-based clock using ESP32 following components are required:

  • ESP32 Board
  • 16X2 I2C LCD Display
  • Connecting Wires
  • Breadboard

Introduction to NTP (Network Time Protocol)

The Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems. It is used to ensure that the clocks on different devices are in sync with each other, even if they are in different parts of the world.

NTP works by using a hierarchy of time servers, with each server synchronizing its clock with a more accurate time source. This allows devices to synchronize their clocks with a high level of accuracy, typically within a few milliseconds.

NTP is an important protocol for many applications, including computer networks, financial transactions, and scientific research. It is also used to synchronize the time displayed on digital clocks and other devices.

How Does NTP Work?

The Network Time Protocol (NTP) works by sending and receiving timestamps between servers and clients, using a combination of the current time and the time taken for the message to be sent and received.

The NTP server maintains a high-precision reference clock and uses this clock to adjust the clocks on other devices. The NTP client sends a request to the server, and the server responds with the current time and other data, such as the round-trip time of the request and the server’s current time. The client then uses this information to adjust its own clock and maintain accurate time.

The NTP client adjusts its local clock with the online NTP server using the link delay and local offset defined inside the Arduino code.

Internet Clock with ESP32 & LCD Display Using NTP Client

Designing a real time NTP server-based clock using ESP32 has many benefits. As it is not dependent upon the internal RTC module so we can get exact time using the NTP server. To design this clock first we have to install some necessary libraries in the Arduino IDE.

Installing the Required Libraries

To make ESP32 internet clock using NTP server and display the time on LCD screen, we need to install following libraries:

Click the link to download the NTPClient library.

To download Time Library open the link and click Download Zip.

After downloading both libraries open IDE and go to: Sketch > Include Library > Add .ZIP Library.

Install both libraries one by one. Now to display time on the LCD screen, open the library manager and install the LiquidCrystal I2C library by Frank.

After installing required libraries now, we can integrate ESP32 with an LCD display.

Wiring the LCD to the ESP32

It is possible to connect an LCD display with an ESP32 via its I2C pins. SDA pin is at D21 and SCL/SCK is at D22. Connect ESP32 with LCD as shown in below image:

Following are the connections:


Getting the I2C LCD Address

After connecting the I2C LCD with ESP32 it’s important to check the I2C address. In case if one is using more than one device on the same I2C bus then ESP32 will be unable to communicate with both.

Always use devices with different I2C addresses. To get the I2C address we will use the Wire library. For more detailed-on Arduino code read the article Get I2C Address in ESP32 Using Arduino IDE.

Here the LCD we are using has an I2C address 0X27.

Code for ESP32 Internet Clock

Open IDE and upload code to connect to a NTP server. Once ESP32 is connected to NTP server using the WiFi connection defined inside the code, Arduino serial monitor, and I2C LCD will display real time.

#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>          
#include <TimeLib.h>                  
int lcd_Columns = 16;  /*define LCD size*/
int lcd_Rows = 2;
LiquidCrystal_I2C lcd(0x27, lcd_Columns, lcd_Rows);    /*0x27 I2C Address for LCD*/
const char *ssid     = "SSID";    /*Replace with your network SSID*/
const char *password = "Password";  /*Replace with network password*/
NTPClient timeClient(ntpUDP, "", 18000, 60000);
char Time[ ] = "TIME:00:00:00";
char Date[ ] = "DATE:00/00/2000";
byte last_second, second_, minute_, hour_, day_, month_;
int year_;
void setup(){
  lcd.init(); /*Initialize LCD display*/
  lcd.backlight();  /*ON LCD Backlight*/
  lcd.setCursor(0, 0);  /*Set cursor*/
  lcd.print("Time");  /*print time on LCD*/
  lcd.setCursor(0, 1);  /*Set LCD cursor*/
  lcd.print(Date);  /*Print date*/
  WiFi.begin(ssid, password);  /*begin WiFi*/
  while ( WiFi.status() != WL_CONNECTED ) {
  lcd.clear();  /*clear LCD display*/
void loop(){
  unsigned long unix_epoch = timeClient.getEpochTime();    // Get Unix epoch time from the NTP server
  second_ = second(unix_epoch);
  if (last_second != second_) {
    minute_ = minute(unix_epoch);
    hour_   = hour(unix_epoch);
    day_    = day(unix_epoch);
    month_  = month(unix_epoch);
    year_   = year(unix_epoch);
    Time[12] = second_ % 10 + 48;
    Time[11] = second_ / 10 + 48;
    Time[9]  = minute_ % 10 + 48;
    Time[8]  = minute_ / 10 + 48;
    Time[6]  = hour_   % 10 + 48;
    Time[5]  = hour_   / 10 + 48;
    Date[5]  = day_   / 10 + 48;
    Date[6]  = day_   % 10 + 48;
    Date[8]  = month_  / 10 + 48;
    Date[9]  = month_  % 10 + 48;
    Date[13] = (year_   / 10) % 10 + 48;
    Date[14] = year_   % 10 % 10 + 48;
    Serial.println(Time);  /*Prints time on serial monitor*/
    Serial.println(Date);  /*Print date on serial monitor*/
    lcd.setCursor(0, 0);  /*Set LCD cursor*/
    lcd.print(Time);  /*display time on LCD*/
    lcd.setCursor(0, 1); /*Set LCD cursor*/
    lcd.print(Date);  /*Display date on LCD*/
    last_second = second_;

Using the above code, we can get NTP Time from Server. To get the correct time on LCD you have to make changes according to your time zone.

NTPClient timeClient(ntpUDP, "", 18000, 60000);

Currently the country where I am living is 5 hours ahead of Coordinated Universal Time (UTC Time). So, I have to convert 5 hours to Seconds.

+5 hour = 5x60x60 = 18,000 seconds

Change this time zone according to your location. You can use google to check the GMT offset for your country.

Additionally change the network SSID and Password defined inside the code.

Code started by calling the installed libraries. I2C LCD, ESP32 WiFi, NTPClient and Time library.

NTPClient.h library will connect ESP32 with NTP server and WiFiUdp.h will send and receive UDP messages.

To communicate with NTP time server UDP protocol is used. To get time from NTP internet server there variables NTP server address, NTP Offset, and NTP interval need to be defined.

NTPClient timeClient(ntpUDP, "", 18000, 60000);

NTP server sends time information to ESP32. Time received is in Unix timestamp (Unix epoch) format. Time library will convert Unix epoch time to minutes, hours, and day format.

Next I2C address (0x27) of the LCD is defined. In addition, the size of the LCD 16×2 is also configured.

In loop function timeClient.update() function will get time from NTP server and store it inside the Time variable.


On the serial monitor, you will see the output below:

On the LCD display a clock with updated date and time can be seen.


ESP32 is a compact microcontroller based IoT board. This article covers all steps needed to design a real time NTP server-based clock. The output is displayed on the LCD screen using the Arduino code. By setting the correct NTP server any one can design a clock based on their time zone using the ESP32 and Arduino code.

About the author


I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.