PCF8563 is a Real Time Clock IC that provides accurate real time. In the market one can buy the PCF8563 module which has necessary pins for interfacing with microcontroller boards like Arduino or ESP-32 CAM module. Here we can want to show how to build real time clock displaying time and date on LCD using Arduino and PCF8563 RTC module with battery backup.
Below is the circuit diagram.
In the diagram, the PCF8563 RTC module is connected to two things: first the Arduino Uno, where the I2C pins SDA and SCL is connected between them, power is applied from the +5V pin of Arduino through a switch and a diode to provide power to the RTC module. The second thing is the that we have 3V coin battery connected also to the power pin VDD of the module. When the power is switched on then the Arduino will provide the power and the switch is disconnected the 3V battery will provide the power so that the clock remains powered on. This is a simple battery backup method for powering real time clock continuously. This is also the method used in PC to keep the clock going even if it powered off.
The program code for Arduino is below.
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#include <LiquidCrystal.h>
//init the real time clock
Rtc_Pcf8563 rtc;
/* initialize the library objects */
/* LiquidCrystal lcd(rs, en, d4, d5, d6, d7); */
LiquidCrystal lcd(7 ,6 ,5 ,4 ,3 ,2);
void setup(){
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
lcd.clear(); // Clear the LCD screen
//clear out all the registers
rtc.initClock();
//set a time to start with.
rtc.setDate(24, 4, 1, 20, 25); // day, weekday, month, century, year
//hr, min, sec
rtc.setTime(10, 55, 40);
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
lcd.print("Real Time Clock");
delay(500);
lcd.clear(); // Clear the LCD screen
}
void loop(){
// Get current time and date
String currentTime = rtc.formatTime();
String currentDate = rtc.formatDate();
// Print time and date to serial monitor
Serial.print("Time: ");
Serial.print(currentTime);
Serial.print("\t Date: ");
Serial.println(currentDate);
// Display time on LCD
lcd.setCursor(0, 0); // Set cursor to the top-left (first row)
lcd.print("Time: ");
lcd.print(currentTime);
// Display date on LCD
lcd.setCursor(0, 1); // Set cursor to the second row
lcd.print("Date: ");
lcd.print(currentDate);
delay(100); // Update every second
}
This code demonstrates how to use an RTC (Real-Time Clock) module (specifically the PCF8563) to display the current time and date on an LCD screen (16x2) using an Arduino.
Key Components:
RTC (PCF8563): This is a real-time clock module that keeps track of time and date even when the Arduino is powered off, thanks to an internal battery. It communicates with the Arduino via I2C (a common two-wire communication protocol).
16x2 LCD: This is a small display that can show 16 characters on each of its two rows. It’s used here to display the current time and date.
Setup Process:
- Initializing the Components:
- The RTC is initialized so that the Arduino can communicate with it and read the time and date.
- The LCD is initialized so it can show text. It’s set up with the correct number of rows and columns (16 characters by 2 rows).
- Setting Time and Date:
- When the program starts, it sets an initial date and time for the RTC module, which is used as the starting point.
- The time and date are manually set in the code (for example, 10:55 AM on the 24th of April, 2025). This step is essential if the RTC hasn’t been set previously.
Main Functionality:
Reading the Time and Date:
- The program continuously retrieves the current time and date from the RTC module.
- The RTC keeps track of time (hours, minutes, seconds) and the date (day, month, year) internally. This information is then extracted by the Arduino.
Displaying on LCD:
- The program then takes the current time and date and displays them on the LCD.
- The time is shown on the top row, and the date is shown on the bottom row.
- Every second, the displayed information is updated to reflect the current time and date.
Serial Monitor:
- In addition to displaying the time and date on the LCD, the program also prints this information to the Serial Monitor for debugging purposes. This allows you to view the time and date directly on your computer screen, which can be useful for troubleshooting or monitoring the RTC’s output.
The Process:
- The program starts by initializing both the RTC and the LCD.
- It sets an initial date and time for the RTC.
- It continually reads the time and date from the RTC every second.
- The current time is displayed on the first row of the LCD, and the current date is displayed on the second row.
- The time and date are also printed to the Serial Monitor for observation.
- This process repeats indefinitely, updating the display every second.
Hardware Connections:
- The RTC module is connected to the Arduino using I2C. Two wires (SDA and SCL) handle the communication between the Arduino and the RTC.
- The LCD is connected to six digital pins on the Arduino (for controlling the display: sending data and enabling the screen to show it). You can learn more in the guide how to interface 16x2 LCD with Arduino.
Key Points:
- I2C Communication: The RTC uses I2C to send time and date data to the Arduino. The Arduino uses the
Wire
library to communicate over this protocol. - LCD Display: The 16x2 LCD is controlled using a parallel interface with the Arduino. The
LiquidCrystal
library makes it easy to send text to the display. - Real-Time Clock: The RTC chip keeps time independently of the Arduino, even when the Arduino is powered down. This allows the clock to continue ticking.
What You’ll See:
- On the LCD screen, you’ll see the time (hours, minutes, seconds) and the date (day, month, year) displayed. The time and date update every second.
- On the Serial Monitor, the same information is printed, which you can use for verification.
In summary, the program is continuously reading the current time and date from the RTC, displaying it on an LCD, and also printing it to the Serial Monitor for verification. The RTC module ensures the time keeps ticking accurately, even when the Arduino is powered off.