Lesson05: Basic PCF8563 RTC Reading¶
1. Objectives¶
In this lesson, you will learn how to read the PCF8563 real-time clock via I2C to obtain the year, month, day, hour, minute, second, day of the week, and Unix timestamp, as well as read the voltage-low flag in the PCF8563 seconds register.
2. Prerequisites¶
- The ESP32-S3 environment setup in Lesson01 has been completed.
- Additional library:
RTClib; this project uses version2.1.4. - Additional library:
Adafruit BusIO; this project uses version1.17.4. RTClib depends on this library.
How to add the library files:
- RTClib: 2.1.4
- Adafruit BusIO: 1.17.4
- 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¶
-
Confirm that
RTClibandAdafruit BusIOare installed or that the library files for this lesson are present.
4. Hardware Instructions¶
- Connect the development board to the computer using a USB data cable and ensure that the device remains properly powered.
-
After opening the Serial Monitor, wait for the RTC time to refresh every second.
-
Observe the date, time, day of the week, Unix timestamp, and VL flag.
-
If the VL flag is 1, the RTC has previously lost power or experienced insufficient voltage, and its time will need to be recalibrated.
5. Key Code Explanation¶
constexpr uint8_t kI2cSdaPin = 4;
constexpr uint8_t kI2cSclPin = 3;
Wire.begin(kI2cSdaPin, kI2cSclPin);
The ESP32-S3 can map I2C to different GPIO pins. On this watch hardware, I2C SDA is GPIO4 and SCL is GPIO3, so Wire.begin(4, 3) must be called explicitly instead of relying on the Arduino default I2C pins.
RTC_PCF8563 rtc;
if (!rtc.begin(&Wire)) {
Serial.println("ERROR: PCF8563 was not detected at address 0x51");
while (true) {
delay(1000);
}
}
RTC_PCF8563 is the PCF8563 driver class in RTClib. rtc.begin(&Wire) uses the previously initialized I2C bus to locate the RTC. If the RTC is not detected, the program enters an infinite loop, allowing students to first troubleshoot the I2C address, power supply, and solder connections.
Wire.beginTransmission(kRtcAddress);
Wire.write(kRtcSecondsRegister);
Wire.endTransmission(false);
Wire.requestFrom(kRtcAddress, static_cast<uint8_t>(1));
voltageLow = (Wire.read() & 0x80U) != 0;
RTClib can read the date and time, but the voltage-low flag must be read directly from the PCF8563 seconds register. The code first selects register 0x02 and then reads 1 byte. The most significant bit, 0x80, is the VL flag. If this bit is 1, the RTC has previously experienced insufficient power, and the time may be unreliable.





