Go Down

Topic: My DS1302 RTC gets ahead of real time (Read 15715 times) previous topic - next topic

magnetman

Jun 07, 2014, 12:05 am Last Edit: Jun 07, 2014, 12:09 am by magnetman Reason: 1
Hello fellow forum members,
I just managed to connect my DS1302 RTC I bought from China to my Arduino UNO this week, but now I observed it drift in front of real time with about 19.89 seconds per day. I actually have 2 of them, and both do about the same drift.
More exacly:
From Date / Time: 2/6/2014 16:15:00 to Date / Time: 5/6/2014 20:23:56 the RTC drifted 63 seconds in front of real time.

I use the following code:
Code: [Select]
#include <virtuabotixRTC.h> //|

// Creation of the Real Time Clock Object
//SCLK -> 5, I/O -> 6, CE -> 7
virtuabotixRTC myRTC(5, 6, 7);

void setup() {
Serial.begin(9600);

// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year
/* SET TIME CODE:
myRTC.setDS1302Time(00, 15, 16, 7, 2, 6, 2014);
*/
}


void loop() {
// This allows for the update of variables for time or accessing the individual elements.
myRTC.updateTime();

// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);

// Delay so the program doesn't print non-stop
delay( 1000);
}

With the library: https://virtuabotix-virtuabotixllc.netdna-ssl.com/core/wp-content/uploads/2014/01/virtuabotixRTC.zip
And code from here: https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/

My questions are:
First one: Is there any way to use code to correct this drift daily? What can I do about it?
Second question: Which is the easiest way to use the data read from the rtc to time varios things, like relay boards? Do I assign the data for each set of data separately to integers (get one for seconds one for minutes and so on) or use it as a string? I have to mention that I'm a newbie, did research on this but found nothing helpful, if you guys could point me to a tutorial or book to learn working with this kind of data it would be great.

Thank you for reading.

**Think this thread may go better in other subforum, about coding. I was thinking since it's about the sensor it should be in general electronics, but I just realised this after posting. Please move it if that's the case.

jremington

Those RTCs aren't very accurate. 20 seconds/day drift is typical but the drift also depends fairly strongly on the temperature.

The DS3231 modules aren't much more expensive, but are temperature corrected and have far higher accuracy (drift of up to one minute per month). The DS3231 also  features a programmable output (SQW) that can be used to time events or interrupt the processor to create a completely separate timekeeping function.

JChristensen

Did these RTCs come from a reputable vendor? That accuracy is ten times worse that what would typically be expected. If they came from a reputable vendor, send them back. If not, then chalk it up to experience and get some more from someone that can be trusted.

If it were me, I'd use a DS1307 before a DS1302 because it has a standard I2C interface, or a DS3231 (also I2C) if I wanted more accuracy. These are probably the two most common RTCs used with Arduino. Accuracy should be about ±20ppm for the DS1307 or ±2ppm for the DS3231.

Check the link below for another alternative (shameless plug). These come calibrated to within a few ppm.

JChristensen


Those RTCs aren't very accurate. 20 seconds/day drift is typical but the drift also depends fairly strongly on the temperature.


Disagree. These probably have seriously out-of-spec crystals. The garden-variety 32.768kHz crystal has an accuracy spec of 20-30ppm, which equates to 2-3 seconds per day, a minute or so per month. Indeed this is not great accuracy, but is what I expect from typical inexpensive RTCs.

But a decent (non-temperature compensated) crystal will run very consistently if it does not undergo large temperature variations; room temperature as found in the typical house is usually consistent enough. I have a clock on my wall driven by an MCP79412 RTC with a 20ppm crystal, but calibrated closely, and it looks to be within a second after running for more than two months. If I put it on a window sill, where the sun heats it up during the day, and it gets colder at night, accuracy suffers. So my window-sill project has a DS3231 :D

Quote

The DS3231 modules aren't much more expensive, but are temperature corrected and have far higher accuracy (drift of up to one minute per month).


2ppm equates to about one minute per year.

magnetman

Thank you for all the responses. I will try my luck with a ds3231. As I am in the planning/prototyping part of my project I will try to get away with cheap products from ebay. Most of them work acceptably. This ones are not.
What about my second question, any tips or points on where to start researching it?

JChristensen


Thank you for all the responses. I will try my luck with a ds3231. As I am in the planning/prototyping part of my project I will try to get away with cheap products from ebay. Most of them work acceptably. This ones are not.
What about my second question, any tips or points on where to start researching it?


Good luck with eBay. It always seems to turn out to be false economy for me. Prices are reasonable from reputable dealers IMHO but everything is relative.

As for using the RTC, check out the Arduino Time library. There is also a TimeAlarms library that comes with it that may be useful. The other option is to do the time checks yourself. Get familiar with the time_t datatype and the tmElements_t structure that are defined in the Time library. The former is just a 32-bit integer that represents the number of seconds since 01Jan1970. The latter is a breakout of hours, minutes, seconds, etc. There are also functions to convert between the two. One more option is to use the alarms in the DS3231 RTC. I have a library that will help work with those.

billhowl

If your drift more or less constant daily, you may try the way on this link

DS1307 RTC adjust drift automatically

liuzengqiang

If your crystal is designed for wrist watch, and your operating temperature is quite different from wrist temperature, it will drift quite a bit. Your drift is typical considering temperature effect.

jremington

#8
Jun 07, 2014, 07:13 am Last Edit: Jun 07, 2014, 07:16 am by jremington Reason: 1
Talk of +/- 20 ppm or +/- 2 ppm is well and good, but I was speaking from experience. The first DS1307 module that I bought was from SparkFun and it lost about 30 minutes in 3 months, in my house at reasonably constant temperature. That is very much in line with the experience reported by the OP.  Perhaps SparkFun buys from less than reputable crystal manufacturers, but I didn't consider sending it back.

I had read that the watch crystals typically used in the DS1307  were sensitive to temperature swings, so I went ahead and bought the DS3231 module from JeeLabs. This module is currently being used as a timestamp source for a datalogger in a greenhouse, and it has lost about 5 minutes after four straight months of operation. That is OK for my purposes, and I acknowledge the caveat that temperature in the greenhouse ranges from 40 F to 95 F.

More recently I bought three DS3231 modules from dx.com for about $5 each, and I've experimented with two of them. On my workbench they seem to be more accurate than the JeeLabs module, but are still off by several seconds after a few days, compared to NTP time sources.

JimboZA

Have you got the right crystal? The 1302 datasheet specifies a crystal with a load capacitance of 6pF. I accidentally got one different, 12.5pF , and accuracy suffered a bit. (My crystal is of the correct frequency, but the wrong capacitance.)

See this thread
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

JChristensen


Talk of +/- 20 ppm or +/- 2 ppm is well and good, but I was speaking from experience. The first DS1307 module that I bought was from SparkFun and it lost about 30 minutes in 3 months, in my house at reasonably constant temperature. That is very much in line with the experience reported by the OP.  Perhaps SparkFun buys from less than reputable crystal manufacturers, but I didn't consider sending it back.


I was also speaking from experience. Stuff really does almost always work as well as the datasheets say. I haven't tried an RTC from SparkFun, but I do have several of AFI's DS1307s and they all run within 20-30ppm.

Quote

I had read that the watch crystals typically used in the DS1307  were sensitive to temperature swings, so I went ahead and bought the DS3231 module from JeeLabs. This module is currently being used as a timestamp source for a datalogger in a greenhouse, and it has lost about 5 minutes after four straight months of operation. That is OK for my purposes, and I acknowledge the caveat that temperature in the greenhouse ranges from 40 F to 95 F.


All crystals exhibit temperature effects. That DS3231 isn't performing to spec either, not by an order of magnitude. Something is fishy.

Quote

More recently I bought three DS3231 modules from dx.com for about $5 each


That could be what's fishy. Real DS3231s cost nearly twice that, those could well be counterfeits.

Some more experience, I sell MCP79411/12 RTCs on Tindie, for which I use a Citizen 20ppm crystal. I've built dozens of these, checked the calibration on each one and kept a log. None has ever exceeded the spec, and the average error is 8.2ppm, which is just about what I would expect.

jremington

@JC: It would be easy enough to replace the crystal on the DS1302 and DS1307 modules, so which crystal and trusted supplier would you suggest?

JChristensen


@JC: It would be easy enough to replace the crystal on the DS1302 and DS1307 modules, so which crystal and trusted supplier would you suggest?


Pretty much all parts used for anything that I sell come from Digi-Key or Mouser. Here is the crystal I use with the Microchip RTCs. Any of these would probably be fine if the typical through-hole cylindrical package is needed.

I have also used crystals from Dip Micro and Tayda for personal projects, both 32.768kHz and also 8 and 16MHz for system clock, and they seem fine, but I haven't done any real testing or even much measurement on them. I've ended up with some unbelievably bad junk from eBay vendors on the very few occasions that I've tried them. So I basically don't even think about using eBay, it's usually false economy for me.  I know a lot of folks here do a lot of business there, and if it works for them, then I can't argue. OTOH there are a lot of postings here from folks trying to get eBay merchandise to work, RTCs not being the least of these.

I make enough trouble for myself on the average day, so when I buy something I want it to work, that is not the time when I want to debug some other person's design. But if that's some folks' hobby, then what can I say.

</R&R> :D

liuzengqiang

If you need accurate time, get network or gps.

JChristensen


If you need accurate time, get network or gps.


Depends on the definition of "accurate". RTCs can easily do better than many watches or clocks bought in stores. NTP and GPS are indeed further improvements, but not without costs. Fit the solution to the requirements. If a $10 or $15 RTC module is good enough, then why spend $40 for a GPS module, $45 for an Ethernet shield, or even $20 on an Ethernet offload module. There are other costs, too. Code is more complex; can't run an Ethernet shield on a battery; etc., etc., etc.; compromises everywhere.

Go Up