Lesson07: WiFi NTP Synchronization with the PCF8563 RTC¶
1. Learning Objectives¶
This lesson combines WiFi, NTP network time, and the PCF8563 RTC. After connecting to WiFi, the program obtains the current time from an NTP server and writes it to the external RTC. The RTC then continues keeping time even after WiFi is turned off.
2. Prerequisites¶
- Complete the ESP32-S3 environment setup in Lesson01.
- Additional library:
RTClib, version2.1.4is used in this project. - Additional library:
Adafruit BusIO, version1.17.4is used in this project. WiFi.handtime.hare included with the ESP32 3.3.3 board package and do not require separate installation.
How to add the library files:
- RTClib: 2.1.4
- Adafruit BusIO: 1.17.4
- WiFi/time: Included with the ESP32 3.3.3 board package
- Library download link: https://github.com/Elecrow-RD/CrowPanel_2.01_inch-HMI_ESP32_Watch_Display_240_296/tree/master/example/V1.0/Arduino/Lesson04_Rotary_Encoder
3. Arduino IDE Instructions¶
-
Update
kWifiSsidandkWifiPasswordto match the available network.
-
Confirm that
kTimezoneis set toEST5EDT,M3.2.0,M11.1.0, which represents U.S. Eastern Time.
-
Confirm that the board settings match those used in Lesson01.

-
Click Verify to compile the program, then click Upload to flash it to the board.

4. Hardware Instructions¶
- Connect the USB data cable and place the device within range of a 2.4 GHz WiFi network with internet access.
-
Confirm that the WiFi SSID and password in the code match the available network.
-
After flashing, keep the device connected to the network and wait for it to obtain the NTP time.
-
Observe the RTC time before synchronization, the network time, and the RTC time after synchronization.
-
After synchronization is complete, the program turns off WiFi. Continue observing whether the RTC advances once per second.
5. Key Code Explanation¶
configTzTime(kTimezone, kNtpServer1, kNtpServer2);
struct tm timeInfo = {};
if (!getLocalTime(&timeInfo, kNtpTimeoutMs)) {
Serial.println("ERROR: NTP time was not received");
return false;
}
configTzTime() configures the time zone and NTP servers. getLocalTime() waits for the system to synchronize the time over the network and stores the result in struct tm. A 15-second timeout is configured here to prevent the program from waiting indefinitely if a network error occurs.
networkTime = DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec);
In the C standard library, tm_year counts from 1900, while tm_mon counts from 0. Therefore, 1900 and 1 must be added respectively. The converted result is a DateTime object used by RTClib, which can then be written directly to the RTC.
rtc.adjust(networkTime);
delay(100);
printDateTime("RTC after sync", rtc.now());
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
rtc.adjust() writes the network time to the PCF8563. After a short delay, the program reads the RTC again to confirm the time after it has been written. WiFi is turned off after synchronization to reduce power consumption and demonstrate that the RTC operates independently as an external clock chip.


