Accurate timekeeping is the backbone of any data logging, scheduling, or time-stamping application — and with ESP32 PCF8563 RTC low power integration, you can maintain precise time even when your microcontroller is in deep sleep, the Wi-Fi is off, or the mains power is interrupted. The PCF8563 is a battery-backed I2C real-time clock IC from NXP Semiconductors, consuming just 250 nA from its backup coin cell, making it ideal for ESP32 projects where power efficiency is critical.
In this tutorial we’ll cover PCF8563 hardware wiring to the ESP32, initializing the RTC with the correct time, reading back time and date, configuring alarms to wake the ESP32 from deep sleep, and best practices for drift compensation in a long-running device. All examples use Arduino IDE and the popular RTClib library.
Why PCF8563 with ESP32?
The ESP32 has a built-in RTC that keeps time during deep sleep — but it has two limitations. First, it resets to zero every time power is removed (e.g., during a blackout in Indian homes with frequent load shedding). Second, its drift can be significant: 30–200 ppm depending on temperature, translating to 2–15 minutes of error per month.
The PCF8563 solves both problems. Its external 32.768 kHz crystal has a typical accuracy of ±20 ppm (about 1 minute per month at room temperature), and a tiny CR2032 coin cell keeps it ticking for 10+ years with no main power. For Indian agricultural IoT, industrial monitoring, and home automation projects where accurate time-stamped logs are essential, the PCF8563 is an indispensable addition.
PCF8563 vs DS1307 vs DS3231
| RTC | Accuracy | Backup Current | Interface | Price (India) |
|---|---|---|---|---|
| PCF8563 | ±20 ppm | 250 nA | I2C | ₹80–150 |
| DS1307 | ±100 ppm | 300 nA | I2C | ₹50–100 |
| DS3231 | ±2 ppm | 840 nA | I2C | ₹150–250 |
The PCF8563 hits the sweet spot for most projects: significantly better accuracy than DS1307, at a much lower backup current than DS3231, and at an affordable price point readily available from Indian component suppliers.
Hardware Wiring and Circuit Design
The PCF8563 communicates over I2C at a fixed address of 0x51. The ESP32’s default I2C pins are GPIO 21 (SDA) and GPIO 22 (SCL).
Minimal Connection
PCF8563 VDD → ESP32 3.3V
PCF8563 GND → ESP32 GND
PCF8563 SDA → ESP32 GPIO21 (+ 4.7kΩ pull-up to 3.3V)
PCF8563 SCL → ESP32 GPIO22 (+ 4.7kΩ pull-up to 3.3V)
PCF8563 VBAT → CR2032 + terminal (CR2032 – → GND)
PCF8563 INT → ESP32 GPIO36 or any RTC wakeup capable pin (optional, for alarm)
Important for Indian hobbyists: Many PCF8563 breakout modules sold locally already include the 32.768 kHz crystal and pull-up resistors on the board. If you’re using a bare IC on a custom PCB, add the crystal between OSCI and OSCO pins and 4.7 kΩ pull-ups on SDA/SCL.
Backup Battery Circuit
A CR2032 (3V, 220 mAh) connected to the VBAT pin keeps the RTC running indefinitely when main power is off. Add a Schottky diode (e.g., BAT54) between VDD and VBAT if you want to prevent the coin cell from partially powering your main circuit when 3.3V is removed.
Library Setup in Arduino IDE
Two popular libraries support PCF8563 on the ESP32:
- RTClib by Adafruit: Supports DS1307, DS3231, PCF8563, and more. Most widely documented. Install via Library Manager: search “RTClib” by Adafruit.
- PCF8563 by Bill Greiman: Lightweight, PCF8563-only library with full alarm support.
# platformio.ini
lib_deps =
adafruit/RTClib @ ^2.1.1
Setting and Reading Time
#include <Wire.h>
#include <RTClib.h>
RTC_PCF8563 rtc;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA=21, SCL=22
if (!rtc.begin()) {
Serial.println("PCF8563 not found! Check wiring.");
while (1);
}
// Set time if RTC lost power (battery was dead or first use)
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting time...");
// Sets to compile time; or set manually:
// rtc.adjust(DateTime(2025, 11, 15, 9, 30, 0)); // 9:30 AM Nov 15 2025
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
// Print in IST format (no timezone offset — pre-set to IST when setting)
Serial.printf("%04d-%02d-%02d %02d:%02d:%02dn",
now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
// Day-of-week string (useful for scheduling)
const char* days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Serial.printf("Day: %sn", days[now.dayOfTheWeek()]);
delay(1000);
}
Indian Standard Time (IST) note: The PCF8563 stores only UTC — it has no timezone awareness. The simplest approach for Indian projects is to set the RTC to IST (UTC+5:30) directly. For projects that need proper UTC storage with IST display, add 5.5 hours during display formatting.
Alarm Configuration for Deep Sleep Wake
One of the most powerful features of the PCF8563 for ESP32 projects is its alarm output on the INT pin. When an alarm fires, INT goes LOW, which can be connected to ESP32’s EXT0/EXT1 wakeup GPIO to trigger a deep-sleep wakeup with extremely low power consumption.
#include <Wire.h>
#include <RTClib.h>
RTC_PCF8563 rtc;
#define RTC_INT_PIN GPIO_NUM_36 // Connect PCF8563 INT to GPIO36
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
rtc.begin();
// Check if we woke from RTC alarm
esp_sleep_wakeup_cause_t wakeup = esp_sleep_get_wakeup_cause();
if (wakeup == ESP_SLEEP_WAKEUP_EXT0) {
Serial.println("Woke from RTC alarm!");
// Do your scheduled work here:
// - Read sensors
// - Log to SD card
// - Send MQTT message
rtc.clearAlarm(); // Clear the alarm flag
}
// Set alarm to fire every hour at :00
DateTime now = rtc.now();
DateTime nextHour(now.year(), now.month(), now.day(),
now.hour() + 1, 0, 0);
rtc.enableAlarm();
rtc.setAlarm(nextHour);
// Configure EXT0 wakeup on LOW level
esp_sleep_enable_ext0_wakeup(RTC_INT_PIN, 0);
Serial.println("Going to deep sleep until next alarm...");
esp_deep_sleep_start();
}
void loop() {}
In this pattern the ESP32 runs for only 100–500 ms per cycle (sensor read + transmission), then sleeps for up to an hour. Total average current drops to under 50 µA — running for months on a single 18650 battery pack.
NTP Time Sync and RTC Calibration
For maximum accuracy, sync your PCF8563 to NTP time whenever the ESP32 connects to Wi-Fi. This corrects accumulated drift and ensures your timestamps are always accurate:
#include <WiFi.h>
#include <time.h>
#include <RTClib.h>
RTC_PCF8563 rtc;
void syncRTCwithNTP() {
// Indian NTP server for best latency
configTime(19800, 0, "time.google.com", "in.pool.ntp.org");
// IST = UTC + 5h30m = 19800 seconds
struct tm timeinfo;
if (!getLocalTime(&timeinfo, 10000)) {
Serial.println("NTP sync failed");
return;
}
// Write NTP time into PCF8563
rtc.adjust(DateTime(
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec
));
Serial.println("RTC synced with NTP");
}
For an agricultural monitoring station in rural India with unreliable internet, syncing once a week over a mobile hotspot is typically sufficient to keep the RTC within 30 seconds of real time.
Complete Low-Power Data Logger Pattern
Combining the PCF8563, ESP32 deep sleep, a DHT20 temperature/humidity sensor, and an SD card module gives you a standalone environmental data logger that runs for months on batteries — ideal for greenhouse monitoring, soil moisture tracking, or cold chain temperature logging in Indian agricultural supply chains.
The complete flow is:
- ESP32 wakes from deep sleep triggered by PCF8563 alarm (e.g., every 15 minutes)
- Read DHT20 or BMP280 sensor via I2C (shared bus with PCF8563)
- Read current timestamp from PCF8563
- Append CSV line to SD card:
2025-11-15 09:15:00,32.4,65,1012.3 - If Wi-Fi credentials available and signal present: connect, POST data to server, sync NTP
- Set next PCF8563 alarm, clear current alarm flag
- Enter deep sleep
This design achieves an average current of 30–80 µA, giving 6–18 months of operation from two 18650 cells (5200 mAh combined).
Recommended Components from Zbotic
DHT20 SIP Packaged Temperature and Humidity Sensor
Pair the DHT20 with the PCF8563 on the same I2C bus for a compact, two-sensor node that timestamps every reading with battery-backed accuracy.
2 x 18650 Lithium Battery Shield for ESP32/ESP8266
Power your PCF8563 + ESP32 deep-sleep data logger with this dual 18650 shield — months of battery life at typical 15-minute logging intervals.
BMP280 Barometric Pressure and Altitude Sensor I2C Module
Add barometric pressure to your PCF8563-timed data logger. All three sensors (PCF8563, DHT20, BMP280) share the same I2C bus for minimal wiring complexity.
30Pin ESP32 Expansion Board with Type-C USB
Prototype your PCF8563 + ESP32 RTC data logger easily on this expansion board — exposed I2C pins, USB-C for programming, and breadboard-friendly layout.
Frequently Asked Questions
Does PCF8563 automatically handle leap years and month lengths?
Yes. The PCF8563 handles leap years and varying month lengths automatically in hardware. You don’t need to write any correction logic — set the date and time once and the IC updates correctly through February 29th and month-end boundaries.
How long will the CR2032 coin cell last in the PCF8563?
At the PCF8563’s backup current of 250 nA and a typical CR2032 capacity of 220 mAh, theoretical backup life is over 100 years. In practice, coin cell self-discharge limits life to 8–12 years. Use a reputable brand (Panasonic, Renata) for reliable backup in critical applications.
Can I use PCF8563 with Arduino Uno or Nano?
Yes. The PCF8563 works with any I2C-capable microcontroller including Arduino Uno/Nano (5V — add a 5V-compatible breakout or a level shifter) and ESP8266. The same RTClib code works across all platforms.
Why does my time drift significantly after power restore?
If time resets to zero after power restore, the VBAT pin is not connected to a coin cell, or the coin cell is dead. Check the backup battery connection. If time drifts even when running on main power, verify your 32.768 kHz crystal is correctly placed and not loading-capacitor limited.
Can PCF8563 replace the ESP32’s internal RTC?
For most applications, yes. The PCF8563 is more accurate (±20 ppm vs ±200 ppm for ESP32 internal), survives power cuts, and uses far less current than keeping the ESP32 awake to maintain time. Sync the internal ESP32 RTC from PCF8563 at startup for the best of both worlds.
Build Your ESP32 Data Logger with Zbotic.in
Shop ESP32 boards, I2C sensors, battery shields, and accessories — everything for a low-power PCF8563 timekeeping project, delivered fast across India.
Add comment