Using a Real-Time Clock (RTC) – NXP PCF8563P – with an Arduino

In this post, I will explain what a Real-Time Clock (RTC) is about and how to use it in your circuits. I will be using the NXP PCF8563P chip for the examples. You can find the data sheet for that chip here.

A RTC is used to keep track of date & time in your circuit and make it available to other components. You first configure it at to what you consider being the current date/time and it will use a crystal to keep track of time from that moment. Later, you can ask the RTC about the current date/time when you need it for something. For instance, it could be that you want to include date/time information in a log file. A RTC can also be used as a timer or as an alarm you can use to trigger some other actions when the corresponding event occurs.

We will first see how to wire this RTC and an Arduino. Then we will have the Arduino set the RTC current time. Once done, the Arduino will write the current date/time to the console to show that everything works properly. Finally, I will show how you can enhance the circuit to include a RTC backup battery that will keep powering it when the Arduino is turned off so it doesn’t lose the current date/time.

NXP PCF8563P Pinout

pcf8563P-pinout

Power:

Pin 8 -> V+ source (1V to 5.5V)
Pin 4 -> GND

Crystal (32.768kHz):

Pin 1 -> XTAL
Pin 2 -> XTAL

I2C Communication:

Pin 5 -> SDA (Data) (Pin A4 on Arduino)
Pin 6 -> SCL (Clock) (Pin A5 on Arduino)

Clock Output (Pin 7):

You can use this chip as a clock for other components of your circuits by using the output on that pin. Look at section 8.1 of the datasheet for more details.

INT Pin = Alarm/Timer indicator (Pin 3):

You can program an alarm (at a given moment) or use the chip as a timer. The INT pin will be high when an alarm occurs or the timer ends. Look at section 8.3.2.1 of the datasheet for more details on those 2 modes.

Wiring

The RTC is very easy to wire with your Arduino (or any other micro-controller).

arduino-rtc-8563-wiring

As mentioned previously, the crystal must be a 32.768kHz one.
And, there are 2 2.2KOhms pull-up resistors on the I2C channels.

Programming your Arduino

Installing Arduino library for the PCF8563 chip

There’s an Arduino library, that was created by Joe Robertson, that can be used to interface with the PCF8563P chip. It can be downloaded from here. The official library page can also be found here.
Once you downloaded the .zip file, you have to decompress it and put its content in the libraries folder, which is C:\Program Files (x86)\Arduino\libraries in my case. I also renamed the library folder to rtc_pcf8563 because its original name is way too long.

Make sure you restart your Arduino IDE and you will now have access both to add the rtc_pcf8563 library to a project or load one of its 4 examples.

Example program

You can use the following code, that will set the RTC to the 14th of March 2010 at 1:15:00AM. It is a modified version of the code in the function_test example program. You can load the original version using the following Arduino IDE menu option: File | Examples | rtc_pcf8563 | function_test. Note that there is a small bug in that example since it sets the year t0 1910 instead of 2010. I fixed that bug in the code that follows.

#include < Wire.h >
#include < Rtc_Pcf8563.h >

//init the real time clock
Rtc_Pcf8563 rtc;

void setup()
{
  Serial.begin(9600);
  Serial.println("Date and Time function test.");
  //clear out the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century(1=1900, 0=2000), year(0-99)
  rtc.setDate(14, 6, 3, 0, 10);
  //hr, min, sec
  rtc.setTime(1, 15, 0);
  
  Serial.println("Date set to: Sunday(6) March(3) 14, 2010.  Time set to:  1:15:00 am.");
  Serial.println("Now reading back from chip.");
}

void loop()
{
  char* timeStr = rtc.formatTime(RTCC_TIME_HMS);
  char* dateStr = rtc.formatDate(RTCC_DATE_US);

  Serial.print(dateStr);
  Serial.print(" ");
  Serial.println(timeStr);

  delay(1000);
}

Once you uploaded the program to your Arduino, open a terminal (Tools | Serial Monitor ) and make sure it is configured to talk at 9600 bauds since this is what we configured in the program. You should see an output similar to this:

Date and Time function test.
Date set to: Sunday(6) March(3) 14, 2010.  Time set to:  1:15:00 am.
Now reading back from chip.
03/14/2010 01:15:00
03/14/2010 01:15:01
03/14/2010 01:15:02
03/14/2010 01:15:03
03/14/2010 01:15:04
03/14/2010 01:15:05
03/14/2010 01:15:06
...

Other examples

If you are looking at using this chip for its alarm feature, you are not out of luck! The examples provided by Joe Robertson also show how to use that feature!

Adding a back-up power supply

Very often, when using a RTC, you will want to add a backup battery to your circuit, that will power the RTC while the rest of the circuit is not powered (turned off).
arduino-rtc-8563-wiring-with-backup-battery

This can simply be done by adding a coin cell with a voltage lower than what you use to power the rest of your circuit. That way, by using 2 signal diodes, you will be able to make sure that:
– The coin cell doesn’t power anything when your Arduino powers the circuit.
– Your Arduino doesn’t try to recharge the coin cell… which is important when it’s not a rechargeable battery.
– The coin cell can only power your RTC (not your Arduino).

In this example, I am using a CR2032 (3.3V) non-rechargeable coin cell.

Conclusion

A RTC is a very low power consumption component (0.25uA at 3V) that can be used to keep track of date/time. But it can also be used to trigger other actions based on an alarm (you say the alarm should occur on which day at which time) or based on a timer. This can certainly be useful in many situations!

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Plugin