Go Down

Topic: DS1307 RTC adjust drift automatically (Read 17935 times) previous topic - next topic

PriSim

Code posted here about drifting of ds1307 is dead ! anybody have that code please share

http://limchonghan.wordpress.com/driftcorrectedds1307/

gion86

I saved it few years ago... and still have it  ;D

Here 
Project https://github.com/gion86/PlantLight
Library https://github.com/gion86/DS1307_RTC
I made a little project with that code, slightly modified: it's working pretty well once you get the drift correction for you clock/oscillator...

PriSim

@gion86

Thanks for sharing the code !

Can you please explain these 2 lines of code!

Code: [Select]

di.DriftDays = 1000;     // valid value 0 to 65,535
di.DriftSeconds = 2725;  // fine tune this until your RTC sync with your reference time, valid value -32,768 to 32,767


also how to calculate these values , if my clock is 6 sec/day fast.

Please explain any other code lines related to correction !

gion86

The variable DriftSeconds holds the "quantity" of second your clock is going to drift in DriftDays days. So if you clock is 6 sec/day fast you have to set:

Code: [Select]

di.DriftDays = 1;
di.DriftSeconds = 6;  


If you clock was slow by 6 it should be (signed):

Code: [Select]

di.DriftDays = 1;
di.DriftSeconds = -6;  


If you a have a not integer number of seconds you can multiply both variable by the same factor. So for example if you have 25.67 sec/day:

Code: [Select]

di.DriftDays = 100;
di.DriftSeconds = 2567;  


In the internal of the library those 2 variables will be divided by each other:

Code: [Select]
 
float driftCorrection = float(rtcTime - driftInfo.DriftStart);
driftCorrection /= float(SECS_PER_DAY * driftInfo.DriftDays);
driftCorrection *= float(driftInfo.DriftSeconds);


In driftCorrection you will have the floating point number of seconds per day of you drift, which is then used to calculate the current time:

Code: [Select]
 
// due to conversion of driftCorrection to long, accuracy of time returned is +/- 0.5sec
time_t curTime = rtcTime - long(driftCorrection);


I have to make clear that this is not my code, but I took it from the blog post above
http://limchonghan.wordpress.com/driftcorrectedds1307/. But I have tested and I'm using in a project so I can say that is working. ;)

marthinius

I'm trying to run the above code, but unsuccessfully.

Code: [Select]


sketch_jul28a:34: error: 'tmDriftInfo' does not name a type

 tmDriftInfo di;


sketch_jul28a.ino: In function 'void setup()':

sketch_jul28a:48: error: 'di' was not declared in this scope

   di = RTC.read_DriftInfo();


sketch_jul28a:48: error: 'class DS1307RTC' has no member named 'read_DriftInfo'

   di = RTC.read_DriftInfo();

sketch_jul28a:51: error: 'class DS1307RTC' has no member named 'write_DriftInfo'

   RTC.write_DriftInfo(di); // once you're happy with the drift setting you only have to read the DriftInfo and pass it to now2() or now3() function to get your drift corrected time

sketch_jul28a.ino: In function 'void loop()':

sketch_jul28a:59: error: 'di' was not declared in this scope

   LcdDisplayDateTime(now3(di));

sketch_jul28a:59: error: 'now3' was not declared in this scope

   LcdDisplayDateTime(now3(di));

sketch_jul28a.ino: In function 'void processSerialCommand()':

sketch_jul28a:142: error: 'tmDriftInfo' was not declared in this scope

         tmDriftInfo diUpdate = RTC.read_DriftInfo();  // update DriftInfo in RTC

sketch_jul28a:143: error: 'diUpdate' was not declared in this scope

         diUpdate.DriftStart = newTime;

sketch_jul28a:144: error: 'class DS1307RTC' has no member named 'write_DriftInfo'

         RTC.write_DriftInfo(diUpdate);

sketch_jul28a:151: error: 'tmDriftInfo' was not declared in this scope

       tmDriftInfo diRead = RTC.read_DriftInfo();

sketch_jul28a:152: error: 'diRead' was not declared in this scope

       if (diRead.DriftStart == 0 | diRead.DriftDays == 0 | diRead.DriftSeconds == 0) {

sketch_jul28a:158: error: 'diRead' was not declared in this scope

       SerialDisplayDateTime(diRead.DriftStart); Serial.println();

exit status 1
'tmDriftInfo' does not name a type



gion86

You have to include the library in your project.
How are you compiling? With the Arduino IDE?

Nick_Pyner

I'm trying to run the above code, but unsuccessfully.
You will note that this thread is pretty old, and I suspect rather pointless even when it was current. Inaccuracies in the DS1307 were down to lack of temperature compensation as much as hardware drift. If your fix does not provide temperature compensation, you are probably on a futile venture, which may explain why the original code is hard to find. In the meantime the price of the DS3231 has come down to much the same as the DS1307, and doesn't need all this stuff. If you need to be serious about clock accuracy, you might look at that.

gion86

Yes Nick_Pyner is right.
Newer models offer a clock with temperature compensated oscillator and that makes the difference.
I have switched to a Maxim DS3232 clock, and used this library which is very usefull:

https://github.com/JChristensen/DS3232RTC

Maybe we should close this thread...

Nick_Pyner

#23
Aug 05, 2020, 02:50 am Last Edit: Aug 05, 2020, 09:21 am by Nick_Pyner
Most definitely. It was a dumb thread in 2012.

I might point out that, if you just want to tell the time, the 3231 will run with DS1307 code. No changes required. I don't use any library, but this probably applies with libraries as well.

Go Up