Arduino Forum

Using Arduino => General Electronics => Topic started by: magnetman on Jun 07, 2014, 12:05 am

Title: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jun 07, 2014, 12:05 am
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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: jremington on Jun 07, 2014, 12:21 am
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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 07, 2014, 01:06 am
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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 07, 2014, 01:24 am

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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jun 07, 2014, 03:33 am
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?
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 07, 2014, 03:41 am

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 (http://playground.arduino.cc/Code/Time). 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 (https://github.com/JChristensen/DS3232RTC) that will help work with those.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: billhowl on Jun 07, 2014, 05:28 am
If your drift more or less constant daily, you may try the way on this link

DS1307 RTC adjust drift automatically (http://forum.arduino.cc/index.php/topic,119054)
Title: Re: My DS1302 RTC gets ahead of real time
Post by: liuzengqiang on Jun 07, 2014, 05:45 am
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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: jremington on Jun 07, 2014, 07:13 am
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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JimboZA on Jun 07, 2014, 08:31 am
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 (http://forum.arduino.cc/index.php?topic=165668.0)
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 07, 2014, 02:12 pm

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 (http://www.adafruit.com/products/264) 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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: jremington on Jun 07, 2014, 07:00 pm
@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?
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 07, 2014, 08:23 pm

@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 (http://www.mouser.com/Search/ProductDetail.aspx?R=CM200C-32.768KDZB-UTvirtualkey69500000virtualkey695-CM200C-327K-U) is the crystal I use with the Microchip RTCs. Any of these (http://www.mouser.com/Passive-Components/Frequency-Control-Timing-Devices/Crystals/_/N-6zu9fZscv7?P=1yzmo0zZ1yyimmpZ1z0wo7hZ1z0z7l5&Ns=Pricing|0) 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
Title: Re: My DS1302 RTC gets ahead of real time
Post by: liuzengqiang on Jun 08, 2014, 01:26 am
If you need accurate time, get network or gps.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 08, 2014, 01:41 am

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.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: liuzengqiang on Jun 08, 2014, 05:48 am
I meant "to get rid of drift, use gps or network". Right, more power consumption and more costly.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jun 15, 2014, 03:51 pm
I ordered a DS3231 from China via Ebay. We'll see how that goes.
Now I got 2 DS1302 modules and no use for them. Should I try to change the crystals to see how that goes? Or just scrap them?

Thank you all for the help in this thread.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JimboZA on Jun 15, 2014, 04:03 pm

Should I try to change the crystals to see how that goes? Or just scrap them?


That depends on the answer to my reply at #9.... Do you have crystals of not only the right frequency, but the right capacitance?
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jun 15, 2014, 05:04 pm
Well I was planning to go search them at the local electronics shops after you guys tell me what crystal should i get, cause on the one that's now on the board I can't see any markings, and my module didn't come with a datasheet. I guess all crystals for the ds1302 should be the same, right?
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jun 15, 2014, 05:21 pm

Well I was planning to go search them at the local electronics shops after you guys tell me what crystal should i get, cause on the one that's now on the board I can't see any markings, and my module didn't come with a datasheet. I guess all crystals for the ds1302 should be the same, right?


It needs to be a 32.768kHz crystal with a 6pF load capacitance. This is not the same as crystals for DS1307, which need a 12.5pF load capacitance. See the datasheet (p4, 5) for more info:
http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JimboZA on Jun 15, 2014, 05:38 pm

my module didn't come with a datasheet.


Nothing comes with a datasheet.... you can find them all on the 'net.

I don't see any markings on my crystal either, I just know it's the wrong one from the invoice. Your symptoms are consistent with the wrong capacitance.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jul 11, 2014, 10:51 am
I got my cheap ds3231, this one is loosing 1-2 seconds a day. I remember you guys telling me this one can be calibrated by code. Where should I research that. Any pointers?

As far as stuff from ebay goes I finally understood. They are good at cheap stuff but not precise stuff. I still can't understand how cheap electonics can have precise time chips, but this ones that are sold separately for arduino hobby are so bad at keeping time.
Thank you very much for the help.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jul 11, 2014, 03:03 pm

I got my cheap ds3231, this one is loosing 1-2 seconds a day. I remember you guys telling me this one can be calibrated by code. Where should I research that. Any pointers?


Everything is in the datasheet (http://datasheets.maximintegrated.com/en/ds/DS3231.pdf).

DS3231 has an aging offset register that allows the oscillator to be adjusted in approximate 0.1 ppm steps. It's an 8-bit register, so maximum adjustment is less than 13 ppm.
Stated accuracy for DS3231 is ±2ppm from 0°C to +40°C. This is about 0.17 seconds per day.
One second per day would be 11.6ppm, way out of spec.
The aging offset register can compensate for that, but not much more.
Good luck.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jul 11, 2014, 05:40 pm
Thank you very much.
After checking time once again, surprise, it's within 1 second of real time again. That led me to belive the temperature compensation does't work so great. I need to leave it running more before calculating the offset.
Now, another problem regarding the ds3231 breakboard I bought. It has a place on board for a rechargeable lithium ion battery that is hard to get around here. Is there any simple way to make it compatible to a regular battery or maybe use a bigger lithium ion battery soldered instead of the coin cell port?
This is the board model i have:
http://www.ebay.com/itm/DS3231-AT24C32-IIC-module-precision-Real-time-clock-module-memory-module-Arduino-/400503978923
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jul 11, 2014, 06:01 pm
Temperature compensation seems to work very well here, although I haven't done any extreme tests. I have a couple gadgets with DS3231s sitting on window sills. They catch direct sun and in the winter probably are cooler than the rest of the house. They've been operating continuously since last fall sometime and are both within 5-10 seconds. Like I said, that's not an extreme test, but I've tried non-temperature compensated RTCs in those same locations and they drift a lot farther than the DS3231s, and also a lot farther than non-temp compensated RTCs in other parts of the house where temperature is more constant.

I use regular lithium coin cells for all my RTCs, including DS3231 and DS3232. I use CR2016 mostly, but also some CR1220 (which may be a bit on the wimpy side, but should still last for years). I hardly see the point in a rechargeable cell, estimates I've seen indicate that the regular lithium coin cells should last 5-7 years.
Title: Re: My DS1302 RTC gets ahead of real time
Post by: magnetman on Jul 11, 2014, 06:14 pm
Could i use regular battery in my breakboard, which is made to use a rechargable coin cell? That is what i am asking, how to go about not using a rechargable coin cell battery.
Multimeter on battery pins on the board gives me 4.75V, so i guess i can't.
Or, could i use a 3.7 v li-ion 1090 mAh battery pack?
Title: Re: My DS1302 RTC gets ahead of real time
Post by: JChristensen on Jul 11, 2014, 07:45 pm

Could i use regular battery in my breakboard, which is made to use a rechargable coin cell? That is what i am asking, how to go about not using a rechargable coin cell battery.
Multimeter on battery pins on the board gives me 4.75V, so i guess i can't.
Or, could i use a 3.7 v li-ion 1090 mAh battery pack?


Depends on what else is in the circuit. Would really need to have a schematic for the board. DS3231 is fine with a 3V backup battery. See the circuit on the first page of the datasheet. Very simple, the battery just goes from the VBAT pin to ground. Again, not knowing the circuit for this particular board, but it may be possible to just remove whatever circuitry was involved with the rechargeable battery and connect a regular lithium coin cell directly to the DS3231 VBAT pin.

Datasheet says the VBAT voltage can be anywhere from 2.3V to 5.5V, with 3.0V being "typical". So a pair of AA or AAA cells would also work. But that would be gross overkill, as would a Li ion pack with 1090mAh capacity.