10

I want my real-time clock to set its time as the time on my PC. However, when I run the following sketch, the real-time clock reports the time as being 32-33 seconds earlier than my PC says the time is.

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// uncomment it & upload to set the time, date and start run the RTC!
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}

I have also tried manually setting the time on the RTC, but I end up with the same problem: the RTC is always 32-33 seconds behind what I set it to. The lag happens as soon as I run the sketch. It seems very odd to me that no matter how I try to set the time, I end up with exactly the same error. I can tell the Arduino to report the time as being 33 seconds after what the RTC says it is, but this solution seems kind of sketchy, and I am worried that something is fundamentally wrong with my RTC or the way I am using it.

I am using an Arduino Uno with an Assembled Data Logging Shield from Adafruit. The data logging shield uses a DS1307 RTC. Has anyone had this problem before, or have any ideas about what could be causing it? Any help would be much appreciated.

  • Ive taken Hugo Bertini and Oli explanation to use in my projects and it works ! Thank you (: but it goes faster by 26 seconds.... – Ahmad Zaki Aiman Abdul Rashid Jul 27 '18 at 13:55
17

The __DATE__ and __TIME__ are set when the code is compiling so they will naturally be behind since the code still needs to finish compiling and then be flashed to the chip.

See the Arduino Playground for an example of how to sync it to your computer over serial.

TimeSerial.pde shows Arduino as a clock without external hardware.

It is synchronized by time messages sent over the serial port. A companion Processing sketch will automatically provide these messages if it is running and connected to the Arduino serial port.

0

You can fix the offset by calling once the following code in the setup() function:

RTC_DS3231 rtc;

DateTime now = rtc.now();
rtc.adjust(DateTime(now.unixtime() + 10)); // add 10s to current time for fixing the offset
0

I am experiencing a similar problem with an Arduino UNO and a Nano. Both from the same PC. Indeed it looks like the time it takes from compile time to upload + MCU start is reflected.

Assuming the RTC has a good battery and the compilation + upload times are consistent, then adding a drift compensation to the code seems to do the trick. Here's how I did it (in my case 7 seconds did the "trick" - sorry for the hardocode, but that's for mere description purposes):

RTC.adjust(DateTime(__DATE__, __TIME__));
DateTime t = DateTime(RTC.now().unixtime()+7);
RTC.adjust(t);

Regards, Hugo Bertini

-1

The Arduino takes the computer time and sends it to the DS1307. The time it takes to copy is the one you are seeing wrong. I would solve the problem by advancing the computer time the necessary seconds that you say is delayed and when programming you should have the exact time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.