Using a real time clock with a microcontroller

I have been working on a little LED display unit, which I am hoping to produce in kit form. This is designed to be used as a programmable LED display for numerous applications. I also wanted to include some additional features so that the display can be used instantly.

The display uses a sure 8 x 32 LED dot matrix. This is interfaced to a Xino (or Arduino). The display works really nicely but I wanted a few more features for this project.

So I added 2 x buttons for any user input, a temperature sensor, a buzzer, a wireless link and a real time clock. This is a post about using real time clocks.

Real time clocks are designed to very accurately measure time. They use an external oscillator (in this case a watch crystal running at 32.768kHz). While micro-controllers are very good at performing many functions, unless very careful programming is used then it is difficult to accurately keep track of time. Lots of different commands use varying lengths of time to complete hence using the micro-controller instructions to keep track of time is quite difficult to implement. Certainly it is very difficult with the Arduino, as it is not designed to easily use the interrupts (which is how you can get very accurate timing). It is very easy to add a commercial real time clock chip onto a project and that will ensure very accurate timings are performed.

I used the PCF8583 from Dallas Semiconductor. This is a relatively cheap (around £2) real time clock which uses the I2C serial data transfer method. It also has an additional 240 8-bit ram locations, which could be useful.

The data from the PCF8583 is read using I2C, which is implemented using the wire library, which must be included.

As usual in these interconnected days, some one has already released the code required to easily talk to the PCF8583. It is available here. It was developed by Joe Robertson.

The chip is connected to the arduino via pins AN4 and AN5, which the code uses for the I2C communication.

There are a few things to note about using the PCF8583.

  • A 100nf decoupling capacitor should be used on the power lines, close to the RTC IC.
  • Connect AN5 to SCK – pin 6 (the I2C clock pin).
  • Connect AN4 to SDA – pin 5 (the I2C data pin).
  • Use a 32.768kHz crystal on the OSC pins 1&2.
  • A pull-up resistor (10k) on the /int line.
  • To keep the time when switched off, a back-up battery must be used. This can be any battery from 1V to 6V. I used a 3V CR2032 coin cell. You must be careful to ensure that the battery does not feed back to the arduino, nor that the ardunio power supply feeds back to the battery. To stop this you can use two general purpose diodes (I used 1N4001 as I have them around) , as shown in the circuit diagram here: (Note: this circuit diagram is for an arduino shield I am making, hence it shows loads of other stuff)

alt

The circuit diagram is available for download here.

The library includes commands to set and read the time and date. It also includes commands to set an alarm, although I have not implemented this into my device.

Download the library and include it into your arduino libraries. Use the examples to see how to set and read the time.

A few code notes:

You must include

You must initialise a real time clock variable and give it a name (in this case called, imaginatively, rtc):

You can use the various functions which include setDate, getWeekday etc etc (check the .cpp file in the rtc_ptc8583 library folder). When using the, write the name of the real time clock (in my case this was ‘rtc’) then add .command. For example:

It was pretty easy, but I found a few problems (especially with another rtc library for the Ardunio which I could not get working correctly). This work has been done to be included as code on the arduino based LED dot matrix display hardware which I am planning on releasing as a kit for people to easily make their own LED displays. Please contact me to register your interest in one of these units.

Hope it is of help to someone else. I will clean up my sketch and upload it soon.

Update 23/10/12:

Following on from implementing this design within a data acquisition unit I have a bit more detail to include.

I found that with the PCF8563 I was using was loosing around 30 seconds per day. This was obviously unacceptable. The data sheet explains that an accuracy of 300 seconds per year is obtainable, whihc is less than one second per day.

Peter had commented on adding the trimmer capacitor (see comments below) which is mentioned in the PCF8564 data sheet. I used a 8-30pF trimmer capacitor between OCS1 (pin 1) and ground. I then left the RTC running for a day or two. After testing I was now running 10 seconds per day fast!

I found this application note (AN10652) from NXP who manufacture the RTC. Section 4 mentions about calibrating the accuracy of the RTC using an 8 digit frequency counter with an accuracy of 1ppm (part per million). To do this I first had to set the output clock (pin 7) to give an output the same as the oscillator frequency. I was using 32.768kHz crystal oscillator. I bought one of these cheap but definitely not the best frequency counters from Farnell (part number: 1270137). It is definitely not 1ppm accurate, but the cost of those counters was too high for this project.

Leaving the frequency counter to warm up and stabilise for 20 minutes, I then checked the output frequency. This unit can give a resolution of XX.XXXX kHz, which is 0.1Hz. I adjusted the trimmer capacitor until I was spot on 32.7680 kHz.

I left the unit for 4 days to run. At the end of the 4 days it was around 1 second slow. This was MUCH better and within the accuracy that I required. One day I hope to check this against a better frequency meter to get even better resolution.

The application note above (AN10652) gives much more detail on temperature compensation and getting the RTC even more accurate.

2 responses to “Using a real time clock with a microcontroller

  1. Interested to see your schematic doesn’t show a (trimmer) capacitor connected between OSCI on the PCF8563 and ground as suggested by the Dallas datasheet. When I set up a breadboard without an external capacitor I was unable to get the oscillator running properly and as a consequence the rtc lost time.

    Putting a 5 – 22pF trimmer between OSCI (pin 1) of the PCF8653 and ground solved the problem. (With a bit of judicious adjustment.)

  2. Thanks for your comment Pete,
    That is a very important point – according to the data sheet with no crystal frequency adjustment there is the potential for a deviation of +/- 5 minutes per year.
    I’ve got to admit in one application I was seeing maybe 1 or 2 seconds a day being missed, which corresponds to around 364 seconds per year, which is around 6 mins.
    Thanks for the advice – I will start using adjustment trimmer capacitors. If you have any advice on how to calibrate them (apart from leaving a year and checking it out, then please post a comment. I see the data sheet says to output an alarm, which gives a higher outpt frequency hence ajustment can be made.
    Regards,

    Matt

    Quoting Pete_T:Interested to see your schematic doesn’t show a (trimmer) capacitor connected between OSCI on the PCF8563 and ground as suggested by the Dallas datasheet. When I set up a breadboard without an external capacitor I was unable to get the oscillator running properly and as a consequence the rtc lost time.

    Putting a 5 – 22pF trimmer between OSCI (pin 1) of the PCF8653 and ground solved the problem. (With a bit of judicious adjustment.)

Leave a Reply

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