ESP32 PCF8563 Real-Time Clock (RTC)
The PCF8563 is a low-power real-time clock/calendar with I2C interface, offering timekeeping functions, programmable clock output, alarm and timer features, and operating over a wide voltage range, making it ideal for portable and battery-powered applications.
Jump to Code Examples
Quick Links
PCF8563 Price
About PCF8563 Real-Time Clock (RTC)
The PCF8563 is a CMOS Real-Time Clock (RTC) and calendar optimized for low power consumption. It provides year, month, day, weekday, hours, minutes, and seconds information, with a century flag. Compared to the DS1307, the PCF8563 operates over a wider voltage range (1.0V to 5.5V) and consumes less power, making it suitable for battery-powered applications. Additionally, it offers a programmable clock output, alarm and timer functions, and an integrated oscillator capacitor, features that enhance its versatility in various applications.PCF8563 Sensor Technical Specifications
Below you can see the PCF8563 Real-Time Clock (RTC) Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C
- Timekeeping Range: Seconds to Years (with Century Flag)
- Operating Voltage: 1.0V to 5.5V
- Backup Current: 0.25 µA typical at 3.0V
- Interface: I2C (up to 400 kHz)
- Clock Output Frequencies: 32.768 kHz, 1.024 kHz, 32 Hz, 1 Hz
- Alarm Function: Yes
- Timer Function: Yes
- Operating Temperature: -40°C to +85°C
PCF8563 Sensor Pinout
Below you can see the pinout for the PCF8563 Real-Time Clock (RTC). The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The PCF8563 pinout is as follows:
- OSCI: Oscillator input; connect to a 32.768 kHz crystal.
- OSCO: Oscillator output; connect to a 32.768 kHz crystal.
- INT: Interrupt output; open-drain, active low.
- SCL: Serial Clock Line for I2C communication.
- VSS: Ground connection.
- SDA: Serial Data Line for I2C communication.
- CLKOUT: Clock output; programmable frequency.
- VDD: Power supply input (1.0V to 5.5V).
PCF8563 Wiring with ESP32
Below you can see the wiring for the PCF8563 Real-Time Clock (RTC) with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
VDD
to a 3.3V or 5V power supply, VSS
to ground, SDA
to the microcontroller's I2C data pin, and SCL
to the I2C clock pin. Attach a 32.768 kHz crystal between OSCI
and OSCO
. The INT
pin can be connected to a digital input on the microcontroller if interrupt functionality is required. Optionally, the CLKOUT
pin can be used to provide a programmable clock signal to other components.Code Examples
Below you can find code examples of PCF8563 Real-Time Clock (RTC) with ESP32 in several frameworks:
If you encounter issues while using the PCF8563 Real-Time Clock (RTC), check the Common Issues Troubleshooting Guide.
ESP32 PCF8563 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the PCF8563 Real-Time Clock (RTC):
#include <Wire.h>
#include <Rtc_Pcf8563.h>
Rtc_Pcf8563 rtc;
void setup() {
Serial.begin(9600);
Wire.begin(21, 22); // SDA: GPIO21, SCL: GPIO22
rtc.initClock();
rtc.setDate(4, 12, 2023); // DD, MM, YYYY
rtc.setTime(14, 30, 0); // HH, MM, SS
}
void loop() {
Serial.print("Time: ");
Serial.print(rtc.getHour());
Serial.print(":");
Serial.print(rtc.getMinute());
Serial.print(":");
Serial.println(rtc.getSecond());
Serial.print("Date: ");
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.println(rtc.getYear());
delay(1000);
}
This Arduino sketch interfaces with the PCF8563 RTC module using the Rtc_Pcf8563
library and the specified pin configuration (SDA: GPIO21, SCL: GPIO22). In the setup()
, the RTC is initialized and the date and time are set. The loop()
retrieves and prints the current time and date to the Serial Monitor every second.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 PCF8563 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the PCF8563 Real-Time Clock (RTC), here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/i2c.h"
#include "pcf8563.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
void app_main(void) {
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000
};
i2c_param_config(I2C_NUM_0, &i2c_config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
pcf8563_init(I2C_NUM_0);
pcf8563_set_datetime(2023, 12, 4, 14, 30, 0);
while (1) {
pcf8563_datetime_t now;
pcf8563_get_datetime(&now);
printf("Time: %02d:%02d:%02d\n", now.hour, now.minute, now.second);
printf("Date: %04d/%02d/%02d\n", now.year, now.month, now.day);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This ESP-IDF example interfaces with the PCF8563 RTC over I2C, with SDA connected to GPIO21 and SCL to GPIO22. The RTC is initialized, and the date and time are set using pcf8563_set_datetime()
. The current time and date are fetched using pcf8563_get_datetime()
and printed to the console every second.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 PCF8563 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the PCF8563 Real-Time Clock (RTC)
i2c:
sda: GPIO21
scl: GPIO22
time:
- platform: pcf8563
id: pcf8563_time
update_interval: 1s
sensor:
- platform: custom
lambda: |-
auto my_sensor = new PCF8563Sensor(id(pcf8563_time));
return {my_sensor};
sensors:
- name: "PCF8563 Date and Time"
The ESPHome configuration sets up I2C communication with the PCF8563 RTC using SDA on GPIO21 and SCL on GPIO22. The time
platform fetches time and date information at 1-second intervals, and a custom sensor displays the data in a human-readable format.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 PCF8563 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
ESP32 PCF8563 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the PCF8563 Real-Time Clock (RTC):
#include <Wire.h>
#include <Rtc_Pcf8563.h>
Rtc_Pcf8563 rtc;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA: GPIO21, SCL: GPIO22
rtc.initClock();
rtc.setDate(4, 12, 2023); // DD, MM, YYYY
rtc.setTime(14, 30, 0); // HH, MM, SS
}
void loop() {
Serial.print("Time: ");
Serial.print(rtc.getHour());
Serial.print(":");
Serial.print(rtc.getMinute());
Serial.print(":");
Serial.println(rtc.getSecond());
Serial.print("Date: ");
Serial.print(rtc.getDay());
Serial.print("/");
Serial.print(rtc.getMonth());
Serial.print("/");
Serial.println(rtc.getYear());
delay(1000);
}
This PlatformIO code interfaces with the PCF8563 RTC using the specified pin configuration (SDA: GPIO21, SCL: GPIO22). The RTC is initialized and configured in the setup()
, while the loop()
fetches and prints the current time and date every second.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 PCF8563 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the PCF8563 Real-Time Clock (RTC) with your ESP32.
from machine import I2C, Pin
import time
# PCF8563 I2C address
PCF8563_ADDRESS = 0x51
def bcd_to_decimal(bcd):
return (bcd >> 4) * 10 + (bcd & 0x0F)
def decimal_to_bcd(decimal):
return ((decimal // 10) << 4) | (decimal % 10)
def set_time(i2c, year, month, day, hour, minute, second):
data = [decimal_to_bcd(second), decimal_to_bcd(minute), decimal_to_bcd(hour),
0, decimal_to_bcd(day), decimal_to_bcd(month), decimal_to_bcd(year - 2000)]
i2c.writeto_mem(PCF8563_ADDRESS, 0x02, bytes(data))
def get_time(i2c):
data = i2c.readfrom_mem(PCF8563_ADDRESS, 0x02, 7)
second = bcd_to_decimal(data[0] & 0x7F)
minute = bcd_to_decimal(data[1] & 0x7F)
hour = bcd_to_decimal(data[2] & 0x3F)
day = bcd_to_decimal(data[3] & 0x3F)
month = bcd_to_decimal(data[5] & 0x1F)
year = bcd_to_decimal(data[6]) + 2000
return year, month, day, hour, minute, second
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Set initial time
set_time(i2c, 2023, 12, 4, 14, 30, 0)
# Loop to read time
while True:
year, month, day, hour, minute, second = get_time(i2c)
print(f"Time: {hour:02}:{minute:02}:{second:02}, Date: {year:04}/{month:02}/{day:02}")
time.sleep(1)
This MicroPython script communicates with the PCF8563 RTC over I2C using SDA (GPIO21) and SCL (GPIO22). The set_time()
function initializes the RTC with the specified time and date, while the get_time()
function reads and decodes the current time and date. In the loop, the script continuously fetches and prints the current time and date every second.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
PCF8563 Real-Time Clock (RTC) Troubleshooting
This guide outlines a systematic approach to troubleshoot and resolve common problems with the . Start by confirming that the hardware connections are correct, as wiring mistakes are the most frequent cause of issues. If you are sure the connections are correct, follow the below steps to debug common issues.
RTC Not Advancing Time Correctly
Issue: The PCF8563 RTC module displays a constant time or advances time incorrectly.
Possible causes include insufficient power supply, incorrect wiring, or a defective module.
Solution: Ensure that the module is connected to a stable power source, with VCC connected to 3.3V or 5V (depending on your module) and GND to ground. Verify that the SDA and SCL pins are correctly connected to the appropriate I2C pins on the microcontroller. If the problem persists, consider replacing the PCF8563 module, as some units may be faulty.
Incorrect or Corrupted Date and Time Display
Issue: The PCF8563 module displays incorrect or corrupted date and time information.
Possible causes include improper initialization, incorrect data retrieval methods, or communication errors.
Solution: Ensure that the RTC is properly initialized in your code, setting the correct time and date during setup. Use reliable libraries and functions to set and retrieve time data. Verify that the communication between the microcontroller and the RTC is functioning correctly, and consider implementing error-checking mechanisms to detect and handle communication issues.
RTC Module Overheating
Issue: The PCF8563 module becomes excessively hot during operation.
Possible causes include incorrect power connections, short circuits, or defective components.
Solution: Double-check all power connections to ensure they are correct, with VCC connected to the appropriate voltage and GND to ground. Inspect the module and wiring for any signs of short circuits or solder bridges. If the module continues to overheat, it may be defective and should be replaced.
Time Resets After Power Loss
Issue: The PCF8563 RTC loses track of time after a power cycle.
Possible causes include a missing or depleted backup battery, or incorrect wiring of the backup power supply.
Solution: Install a backup battery (e.g., a CR2032 coin cell) to the appropriate pins to maintain timekeeping during power loss. Ensure that the battery is fresh and properly connected. Verify that the module is configured to switch to the backup battery when the main power is unavailable.
Conclusion
We went through technical specifications of PCF8563 Real-Time Clock (RTC), its pinout, connection with ESP32 and PCF8563 Real-Time Clock (RTC) code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.