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.
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)
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):
//initialise the real time clock
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.
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.