Skip to content

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, version 2.1.4 is used in this project.
  • Additional library: Adafruit BusIO, version 1.17.4 is used in this project.
  • WiFi.h and time.h are included with the ESP32 3.3.3 board package and do not require separate installation.

How to add the library files:

3. Arduino IDE Instructions

  1. Open Lesson07_WiFi_RTC_Sync.ino in the Arduino IDE. lesson_1

  2. Update kWifiSsid and kWifiPassword to match the available network. lesson_2

  3. Confirm that kTimezone is set to EST5EDT,M3.2.0,M11.1.0, which represents U.S. Eastern Time. lesson_3

  4. Confirm that the board settings match those used in Lesson01. lesson_4

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

  6. Open the Serial Monitor and set the baud rate to 115200. lesson_6

4. Hardware Instructions

  1. Connect the USB data cable and place the device within range of a 2.4 GHz WiFi network with internet access.

lesson_7

  1. Confirm that the WiFi SSID and password in the code match the available network.

  2. After flashing, keep the device connected to the network and wait for it to obtain the NTP time.

  3. Observe the RTC time before synchronization, the network time, and the RTC time after synchronization.

  4. 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.

Download the Lesson 07 code