Real Time Clock (RTC) Adafruit PCF 8523 with maximized accuracy

Introduction

Having available the accurate time can be done in several ways:

1) Use a quartz watch (±15 s/month usual error, ±2 s/month for COSC certified quartz watches), but this one cannot be connected with other devices, e.g. for reading temperature, humidity, etc.

2) Read atomic clock servers (e.g. Time.is) using an internet browser. Same limitations on other uses.

3) If you have an accurate RTC, and you choose to make your own clock, using components from the Arduino World, it is worth trying to read the date and time, accurate to several seconds a month!

Many projects for RTC exist on the net, so why posting another one? Because here we show you how to can make an excellent time keeping device from a below average RTC module!

Among the first components bought a year ago, was a "Adafruit Feather" with RTC. It is easy to connect this module to the Adafruit Feather 32u4 Basic Proto micro-controller, but not so easy it was to connect all this to a small TFT. The initial result was a total failure: a clock with +8s/day error!! What a waste of time and money!

It took a while to understand all the RTC PCF8523 commands in detail. We found how and what to write to the RTC registers and get the best accuracy it can offer. We reached -3.5s/month! Not bad! Compare with your wrist watch accuracy! Even better: read the Project Update and the software can bring the accuracy of the same RTC to 6 seconds/YEAR!

The Purpose

  • Help the Arduino community with some ideas for another project, by improving the performance of an existing RTC.
  • Use commercially available components. Cheaper ones can be found easily!
  • Get the maximum accuracy from these components, using an efficient software.
  • Use a LiPo battery for reading time on a TFT display. Battery level indicator is shown on the TFT screen.
  • A daily alarm can be set in the program, to do the same task for the same duration.

Components

  1. Adafruit Feather 32u4 Basic Proto (20 USD). Details here: https://www.adafruit.com/product/2771
  2. Adafruit Adalogger Featherwing (9 USD). Details: https://www.adafruit.com/product/2922
  3. Adafruit 1.44" Color TFT with Micro SD Socket (15 USD). See: https://www.adafruit.com/product/2088
  4. Li-Polymer battery 800 ... 1200 mAh (12 USD)
  5. A plastic box (3 USD). Cut a window and two holes in it. A rectangular hole in front of the USB port will allow time adjustment (once a month, to keep the error below 3.5s) without opening the box.

1. The display turned on (button on right side) 2. The back-cover off. 3. Battery out.

Schematic

Putting the Featherwing over the Feather is easy: just choose which is on top! Our choice is shown on Figs. 2 and 3. The connection to the TFT is more difficult. We decided to make everything modular, so the display could be disconnected and mounted to another 32U4 board (E.g. Pololu A-Star Micro 32u4) in a few seconds. We have replicated the Pololu A-Star Micro ISP 6 pin connector on the free holes of the Feather 32u4 Basic Proto. This was made possible by the tolerance of the mentioned Color TFT to 5V and PIN voltage. The Fritzing schematics is given below. The connection to the 6 pin connector are also shown, but these are optional.

General view
Bottom view

4. Schematic for the TFT connections and power button.

On the Fritzing schematic are indicated by dotted lines, the implicit connections between the Feather and Fether Wing. The TFT connections are shown, as well as the on/off button. This button connects the Enable pin to Ground to disable the 3.3V on-board regulator. The Li-Po battery can last several weeks/months in this way, depending on its capacity. When you turn on the device, a battery level indicator will tell if recharge is needed. If you have cut the box in front of the USB port, you can charge the LiPo by simply plugging the USB to a 5V socket. The same USB connector can be used to communicate with your PC for programming and time adjusting (once a month, to keep the error below 3.5s!) or once a year if you use the latest software version!

Arduino Code

There are in fact, two Arduino codes:

- Feather32u4_Adalogger-time_setting.ino : This one is setting the RTC to the compile time given by your PC, which is only close to the exact time. After loading this code, you can see on the Serial Monitor the time set in your RTC. Open a browser and read the exact time from Time.is. Compare the seconds. If your RTC time is lagging, by e.g. 5s, write 5 in the top part of the Serial Monitor and press Enter on your PC keyboard. Use -3 for a watch advancing by 3s. You will see on the Serial Monitor the updated time. Repeat the procedure until the "visual error" is less than 1s. Corrections less than 1s are not possible (yet!). You can set also an alarm (start and duration) in the code. This is only useful for daily alarms, of a given duration. A pin is set HIGH for the duration of the alarm, if the RTC clock is not sleeping. This means, you should power on the clock by USB continuously (The LiPo battery can be disconnected in this case). The external LED is set for testing this feature. Other uses of this feature, such as wake up on music, are up to you! Now the exact time is set in the RTC.

Then, load the permanent code:

- Feather32u4_Adalogger-TFT-time_reading-ring5.ino: This is version 5, an improved one! If powered by USB, connect a USB cable to PC or USB power adapter. Otherwise, by simply pressing the side button, if you are using a LiPo battery, you could read on the TFT display, the date, day, time, alarm (start and duration) and the battery level (as image). It is possible that the indicated seconds are not exactly as you have set them in the first step, for an unknown reason, but not always! In this case, mark the difference and repeat the first step.

A screen-off option exists (just un-comment some lines). Useful if you power the clock by USB and want to turn off the screen after a while. You will need to change the button connections to give an impulse waking-up the screen, if you chose so!

We draw attention to the following part, which is setting the PCF8523 accuracy:

// For my Adalogger there is a +8s/day error, so it is needed an: offset = 15; //HEX value of Two's complement on first 7 bytes //>>> Here write an offset value to a PCF8523 register through I2C: Wire.beginTransmission(PCF8523_ADDRESS); Wire._I2C_WRITE((byte)14); // start at location 0F Wire._I2C_WRITE((byte)0x15); // write offset value: 15..16 for +8s/day Wire.endTransmission(); // >>> set to battery switchover mode <<< Is it necessary?? Wire.beginTransmission(PCF8523_ADDRESS); Wire._I2C_WRITE((byte)PCF8523_CONTROL_3); Wire._I2C_WRITE((byte)0x00); Wire.endTransmission();

Correction for your PCF8523-based RTC can be obtained from our file OFFSET-pcf8523.pdf which can be downloaded from the Files page. Just measure your RTC drift in 24 hours, compared with Time.is. In our file, you will find the offset value in the required form (e.g. 0x15 in our case).

The full Arduino codes are given in the next page (Arduino code).

User guide

1) Once a month, or for the first time: connect the USB cable between Feather 32u4 Basic Proto.

Load the first Arduino code discussed above and set the clock, the best you can (better than 1 s error).

2) Load the second Arduino code. Disconnect the USB.

3) If powered by a LiPo battery, press the On/Off button and you will to see on the display:

  • YYYY/MM/DD
  • Week day
  • HH:MM
  • SS
  • Alarm HH:MM:SS (duration in s) Battery level indicator for the Li-Po.

(If not powered by a battery, connect a micro-USB charger and the display will turn on, showing all these data items.)

Project New Contributions

The contributions of this project to the Arduino community are:

- Software with better than ±3.5s/month RTC accuracy, by correction in the registers of PCF8523.

- Power off by disabling the on-board regulator. Of course, a latching button on the battery wires is just as good!

Project Updates

For an improved version of the software, continue with the page Project updates (below).

First improvement is a monthly correction, bringing the annual error to less than 6 seconds. The adjustment is stored in the RTC registers, so no need to keep powered the Adafruit Feather 32u4 Basic Proto. This new version checks also the status of the backup battery of the RTC, which should be normally replaced after several years. This is described on the dedicated page.

Second improvement is ANOTHER RTC! Incredible as it may seem, a relatively cheap (1.8 €) RTC proved to be of an remarkable accuracy (could not measure 1 s error after a week!). So, all our efforts were just for the sake of improving programming? Maybe someone will need it if only an average RTC module is available!

Morale: not necessarily expensive stuff (like those used above) are of good quality, but with some programming skills, these hardware components can be considerably improved!