In this project, we will make an Internet Clock using ESP32 Board & 16X2 LCD Display using NTP Client, i.e. Network Time Protocol.
Overview
In this project we will design an Internet Clock using ESP32 Wifi Module. We will fetch the time and data from the internet using the ESP32 controller. The internet time clock has a precision of 0.02 to 0.10 seconds. In some of the previous projects, I used the RTC Module like DS1307, DS3231 or PCF8563 to get the time. But what if there is no availability of any RTC Module. The next disadvantage of RTC Module is the poor accuracy as it requires manual adjustments over and over again to keep time/date synchronized.
The solution here is to use the Network Time Protocol (NTP). If your ESP32 project has access to the Internet, you can get date and time (with precision within a few milliseconds of UTC) for FREE. You don’t need any additional hardware.
You can check one of my previous projects here: IoT Analog/Digital Clock as well as Arduino GPS Clock
Bill of Materials
Following are the components required for making this project. All the components can be purchased from Amazon. The components purchased link is given below.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | ESP32 Board | 1 | Amazon | AliExpress |
| 2 | 16x2 LCD Display | 1 | Amazon | AliExpress |
| 4 | Connecting Wires | 10 | Amazon | AliExpress |
| 5 | Breadboard | 1 | Amazon | AliExpress |
What is an NTP (Network Time Protocol)?
The Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks.
The protocol can be used to synchronize all networked devices to Coordinated Universal Time (UTC) within a few milliseconds, example 50 milliseconds over the public Internet.
How does NTP work?
The NTP client initiates a time-request exchange with the NTP server. As a result of this exchange, the client is able to calculate the link delay and its local offset and adjust its local clock to match the clock at the server’s computer.
Once synchronized, the client updates the clock about once every 10 minutes, usually requiring only a single message exchange. In addition to client-server synchronization. This transaction occurs via the User Datagram Protocol on port 123.
Internet Clock with ESP32 & LCD Display using NTP Client
Now let us interface 16×2 I2C LCD Display with ESP32 Board. Then we will connect the ESP32 to wifi network and fetch the time and date. Thus a simple internet clock using ESP32 can be made.
The connection between 16X2 I2C LCD and ESP32 is fairly simple. Connect the SDA & SCL pin of I2C LCD to GPIO21 & GPIO22 of ESP32 respectively. The LCD requires 5V VCC. So, connect its VCC pin to Vin pin of ESP32. Connect the GND to GND.
Libraries Requirement
To make an ESP32 Internet Clock with LCD Display, we need few libraries:
|
1 2 3 |
#include <NTPClient.h> // Include NTPClient library #include <TimeLib.h> // Include Arduino time library #include <LiquidCrystal_I2C.h> // Include LiquidCrystal_I2C library |
We will use NTPClient Library first. This library connects the ESP32 WiFi to a time server, the server sends time information to the module.
Then we have Time library which converts Unix timestamp (Unix epoch) into seconds, minutes, hours, day of the week, day, month and year.
The I2C LCD Library is used to interface 16×2 I2C LCD with ESP32 Board.
1. Download NTPClient Library
2. Download Time Library
3. Download I2C LCD Library
Source Code/Program for ESP32 Internet Clock
Here is a source code for getting NTP Time from Server. Before uploading the code to the ESP32 Board, you need to make changes in the line below to match your time Zone.
|
1 |
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 20700, 60000); |
Currently, I am staying in Nepal. Nepal is 5 hours and 45 minutes ahead of Coordinated Universal Time (UTC Time). So I converted +5 hr 45 Mins to Seconds.
So change this Timing according to your time Zone and Country in order to get correct time.
Also in the below code, make changes to the Wifi SSID & Password.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include <WiFi.h> #include <WiFiUdp.h> #include <NTPClient.h> #include <TimeLib.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); const char *ssid = "Alexahome"; const char *password = "12345678"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 20700, 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() { Serial.begin(115200); lcd.begin(21, 22); // Initialize I2C LCD module (SDA = GPIO21, SCL = GPIO22) lcd.backlight(); lcd.setCursor(0, 0); lcd.print(Time); lcd.setCursor(0, 1); lcd.print(Date); WiFi.begin(ssid, password); Serial.print("Connecting."); while ( WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); } Serial.println("connected"); timeClient.begin(); } void loop() { timeClient.update(); 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); Serial.println(Date); lcd.setCursor(0, 0); lcd.print(Time); lcd.setCursor(0, 1); lcd.print(Date); last_second = second_; } delay(500); } |
Once the code is uploaded, the ESP32 will try connecting to the Network. After it connects to Network, the LCD will start displaying the correct time and date.
Here is an application of NTP Clock in one of the project: ESP32 Weather Forecasting. You can follow this post as well to learn more about the Internet Clock.









1 Comment
Dear sir, Internet Clock with ESP32 & LCD Display using NTP Client but i want ESP8266 use what can I modified in circuit. Please sir haple.