Example Code for ESP32-Get NTP Time
Last revision 2025/12/14
This article offers a comprehensive guide on using an ESP32 with NTP to retrieve accurate time, covering hardware requirements, software setup, and providing sample code to ensure precise time synchronization with the DS3231M RTC module.
Hardware Preparation
- DFR0654 FireBeetle 2 ESP32-E x 1
- DFR0641 DS3231M MEMS Precise RTC Module x 1
- FIT0916-FF DuPont Wires x5
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download the DFRobot_DS3231M library: DFRobot DS3231M library
- About how to install the library?
Wiring Diagram

Other Preparation Work
- Install the DS3231M Library in Arduino IDE.
- Connect the DS3231M module to the FireBeetle-ESP32 development board using jumper wires as per the Wiring Diagram.
- Configure the ESP32 compilation environment in Arduino IDE.
- Select the port of the FireBeetle-ESP32 development board in Arduino IDE.
Sample Code
/*!
* @file getTimefromNTP.ino
* @brief Get time from ntpServer and show current time
* @n Experiment phenomenon: read data every second and print it on serial port.
* @n This demo only works on mpython
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng]([email protected])
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
#include <WiFi.h>
#include "time.h"
DFRobot_DS3231M rtc;
const char* ssid = "dfrobotYanfa";//wlan information
const char* password = "hidfrobot";
const char* ntpServer = "ntp.ntsc.ac.cn";//local ntp server
const long gmtOffset_sec = 8*3600; //GMT+08:00
const int daylightOffset_sec = 0;
void getTimeFromNTP()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
rtc.setYear(timeinfo.tm_year - 100);
rtc.setMonth(timeinfo.tm_mon + 1);
rtc.setDate(timeinfo.tm_mday);
rtc.setHour(timeinfo.tm_hour, e24hours);
rtc.setMinute(timeinfo.tm_min);
rtc.setSecond(timeinfo.tm_sec);
rtc.adjust();
}
void setup()
{
Serial.begin(9600);
while(rtc.begin() != true){
Serial.println("Failed to init chip, please check if the chip connection is fine. ");
delay(1000);
}
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
getTimeFromNTP();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void loop()
{
rtc.getNowTime();
Serial.print(rtc.year(), DEC);//year
Serial.print('/');
Serial.print(rtc.month(), DEC);//month
Serial.print('/');
Serial.print(rtc.day(), DEC);//date
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());//day of week
Serial.print(") ");
Serial.print(rtc.hour(), DEC);//hour
Serial.print(':');
Serial.print(rtc.minute(), DEC);//minute
Serial.print(':');
Serial.print(rtc.second(), DEC);//second
Serial.println();
delay(1000);
}
Result
The ESP32 acquires real-time time through the NTP service and prints it via the serial port.

Additional Information
NOTE: This example requires network connection, so the ESP32 series main control board is necessary here.
If there is no enviroment, please configure the ESP32 compilation environment first.
After the ESP32 compilation environment is configured, please select the port of FireBeetle-ESP32 development board.
Was this article helpful?
