Computer program to set RTC to system time.

Hi,

I have been wondering if a computer program or sketch exists to make syncing an I2C RTC with the computer system time easy and precise? At the moment I either use

RTC.adjust(DateTime(__DATE__, __TIME__)

which is great but always out by the time it takes to compile etc.... or trying to set the time to account for these delays which is a bit hit and miss - but obviously not ideal.

I'm guessing that this nut has already been cracked so wondering if anyone can point me in the right direction - the RTC I am using is the great DS3231 but suspect code for other I2C RTCs wouldn't be too bad to recycle?

Thanks for any thoughts.

Here is the method I use to set my RTC, it's primitive for sure but simple and effective. I update the fixed values in the setup function for a time ahead maybe 3 mins. Then I upload the sketch to the board. Note that it waits for any character to be sent from the serial monitor before actually preceeding to set the RTC. Then I call up one of the many time reference web sites that have a running time clock being displayed. So when the web site time matches the time I preset into the sketch I hit a character on the serial monitor and the time gets updated in the RTC and the main loop just keeps printing the time data after that. So nothing fancy but I like being "part of the loop". :smiley:

Lefty

Note that this was written using IDE ver22 and might need tweaking for IDE 1.x ?

/*Reads the value from a Real Time Clock (RTC) DS1307 and displays it in the serial monitor
 *
 *Created by D. Sjunnesson 1scale1.com d.sjunnesson (at) 1scale1.com
 *
 *Created with combined information from 
 *http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1180908809
 *http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1191209057
 *
 *
 *Big credit to  mattt (please contact me for a more correct name...) from the Arduino forum 
 *which has written the main part of the library which I have modified
 *
 */

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson

void setup()
{
  Serial.begin(57600);
  Serial.println ("Hit a key to start");     // signal initalization done
  while(Serial.available() == 0){}
                              // Hit upload about 10 seconds before selected time
  RTC.stop();
  RTC.set(DS1307_SEC,0);        //set the seconds
  RTC.set(DS1307_MIN,28);       //set the minutes
  RTC.set(DS1307_HR,23);        //set the hours   note 24 hour clock
  RTC.set(DS1307_DOW,5);        //set the day of the week
  RTC.set(DS1307_DATE,13);      //set the date
  RTC.set(DS1307_MTH,1);        //set the month
  RTC.set(DS1307_YR,11);         //set the year   
  RTC.start();   
}

void loop()
{

  Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));//read seconds
  Serial.print("      ");                 // some space for a more happy life
  Serial.print(RTC.get(DS1307_DATE,false));//read date
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));//read month
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); //read year 
  Serial.println();

  delay(1000);

}

The Compile Time Error seems to be close to consistant for any particular PC so why could one not adjust the delayed time then add a correction factor - something like seconds = seconds + xxx then see how that goes? Don't know enought to write the code myself which may be obvious from my suggestion above.

Alternatively to get arround this I've used this VB / Arduino application to preload real time, that is real time until it drifts over time.
http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock#Examples

Cheers

I have been looking for examples just like those above! I am glad you guys posted this!

Hi, Yes - thanks for the input :slight_smile: I have since found an example of some nice code to do this via the serial window - I wasn't sure if I could post it here to start with but have since found it in the public domain so I guess its OK.

It is written for the DS3231 RTC but could be adapted for the DS1307 - the trick is that it is written for WaspMote which is Arduino-esq but needs a bit of translating as they have their own bespoke functions like USB.print which is Serial.print - I made a start but soon ran out of ability.... take a look here if you are interested - you basically use the serial window to predefine a time and date which is stored until a set command is sent to serial which sets the time specified in an instant.... so pretty accurate.

http://www.libelium.com/development/waspmote/example024

how to get wprogram.h and ds1307.h libraries

how to get wprogram.h

Install version 0023 of the Arduino IDE (or earlier). If you have a later version, change WProgram.h to Arduino.h.

and ds1307.h libraries

By mail order, from the dark side of the moon. Shipping might take a while.

While you wait, you could try google.