Arduino NTP Clock using NodeMCU and DS3231

In the tutorial Arduino NTP Clock using NodeMCU and DS3231, you will learn to make an accurate clock using the RTC module and NTP server. This circuit automatically updates the time of the RTC module from the network time protocol server.

RTC module shows some time delay after some days/week, this is due to temperatures dependent oscillator used in this module. The external oscillator is more prone to temperature than an internal oscillator. DS3231 module has an internal oscillator that is less prone to temperature and humidity but still has some time delay. According to the datasheet of DS3231, the time variation is about 2min/year but is not true when you practically use it. Previously, we had made a clock using the DS3231 module but the result was wired because it shows a time delay of about 5 minutes in a day (24 hours) in serve climate and frequently ON/OFF the circuit. Whenever I see an advertising LED display board with date and time, I used to check time accuracy. Often, I found that there is either time is slow or fast by a few minutes because of the RTC module. To solve this time accuracy problem, we came up with the simple project Arduino NTP Clock using NodeMCU and DS3231 which update the time of the RTC module from the NTP server.

Advantage of Arduino NTP Clock using NodeMCU and DS3231 over normal RTC Clock

  1. When the internet is available RTC gets a time update from the NTP server. Thus, no error in time.
  2. Suitable for every country unlike other NTP projects available on the internet (GMT 5:45).
  3. Showtime when the internet is not available unlike normal NTP using NodeMCU because of the RTC module.
  4. Arduino UNO board is used in case you require an extra pin say if you want to display the date, time, and temperature in 16×2 LCD.

While making this project you must be familiar with the interfacing of the RTC module with Arduino and how to program NodeMCU using Arduino IDE. If you are not familiar with these things then check out previously posted projects

  1. Appliance Control using IoT and AI Chatbot (Program NodeMCU using Arduino IDE).
  2. Arduino Digital clock using DS3231 Pi Module (Interfacing RTC module with Arduino UNO board).

Components Required for Arduino NTP Clock using NodeMCU and DS3231

NodeMCU x 1

RTC DS3231 x1

Arduino UNO x 1

16×2 LCD x 1

10K variable resistor x 1

470-ohm resistor x 1

Circuit Description of Arduino NTP Clock using NodeMCU and DS3231

Figure 1 shows the circuit of Arduino NTP Clock using NodeMCU and DS3231, built around NodeMCU, RTC module DS3231, Arduino UNO, 16×2 LCD, and a few other electronic components like a resistor and variable resistor, etc.

Arduino NTP Clock using NodeMCU and DS3231 circuit

At first, let see the contribution of every component

NodeMCU: NodeMCU is the heart of this circuit because of the following reason.

  1. Communicate with the Network time provider server and get an accurate time.
  2. Convert that NTP time into the time of your region. (Say, convert the GMT +5 to GMT+5:45).
  3. Update this time to the RTC module regularly.

RTC Module DS3231: Real-Time Clock module keeps time running even you cut off the power supply. It contains a CMOS battery that supplies power to IC DS3231. It also has an internal oscillator to keep the time accurate. The main reason for using the RTC module is to keep time running even when the internet is not available.

Arduino UNO: Here, in this project, Arduino UNO is an optional component to get an accurate time. We are using it here just to control 16×2 LCD because nodeMCU has a smaller number of digital as well as an analog pin in comparison to Arduino UNO. You can add various other sensors like temperature and humidity sensors, gas sensors, etc. according to your requirements.

16×2 LCD: LCD is used here to display the date and time. Check out this video tutorial on interfacing 16×2 LCD with Arduino if you are not familiar with interfacing LCD video. Youtube Link

The connection of the entire component is shown in figure 1, RTC module is connected parallel to both the MCU board i.e. NodeMCU and Arduino UNO board. Power supply for the RTC module is given from NodeMCU where the ground of each component is connected (ground of NodeMCU, Arduino UNO board, RTC, and LCD must be connected to provide the same ground potential level). RTC module communicates with the board in I2C protocol thus we are using SDA and SCL pin of both of the boards. SDA and SCL pin of RTC module is connected to D2 and D1 of NodeMCU where these pins are connected to A4 and A5 of Arduino UNO board as shown in figure 1.

author prototype of rtc and NTP clock
Figure 2: Author Prototype for Arduino NTP Clock using NodeMCU and DS3231

Now Let’s see the software code part

Software Code

There is two code because we are using here two different development board i.e. NodeMCU and Arduino UNO. Both of these codes are written in Arduino programming language and compiled using Arduino IDE. You have to download a few library files like ESP8266. At first, let’s see the software code for NodeMCU.

Software Code for NodeMCU: Code for NodeMCU as shown below you can directly download and use it but before that, you have to change something like SSID, Password of your internet. Your Timer zone (say GMT+5:45 for Nepal).

Software Code for Arduino UNO: Code for Arduino UNO is written in Arduino programming language and compiled using Arduino IDE. You can directly download the code and use it in your project without modification.

Leave a Comment