RTC module loses seconds respect to the computer clock

Hi everyone, I have a problem with the RTC module. I'm using a DS1307 rtc and an Arduino Mega. When I upload the sketch the rtc should take the hour of the computer, but actually it results to be on late of about 5-6 seconds respect to the clock of the computer.
I use this "classic" sketch:

// Date and time functions using a DS1307 RTC connected via I2C
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

  Serial.begin(9600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
    DateTime now = rtc.now();  //recupera data e ora corrente

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  delay(10000);
}

I also tried to use a DS 3231, witch is more accurate than the other one, but the result is the same: the rtc module loses some seconds respect to the clock of the computer.

You may have a fake, counterfeit or reject DS3231.

but actually it results to be on late of about 5-6 seconds respect to the clock of the computer.

There is some lag in uploading and starting the code. This symbol is the time of compilation:

__TIME__

Battery clocks with their own time keeping crystals drift. Its a fact of life. Code an easy way to reset the time. This happens to all battery clocks to more or less extent (unless sync'd to WWV or WWVB or satellite). I have four clocks I built with RTC's and they all drift at different rates, mostly fast.

https://create.arduino.cc/projecthub/madmark2150/talking-alarm-clock-with-8x8x4-matrix-display-2ae9f6

Using compilation time is a rather crude way of updating the RTC.
But once the RTC has been set with a time, does the time shift from the PC time say constant. That is, is it always say 5 seconds slow or does it continue to get worse .

Your clock is set when the Mega restarts, with the compile time.

Even if the drift is inevitable, it's strange for me that the rtc module is already on late of many seconds suddenly after uploading the sketch

The clock is set to compile time, there is a few seconds lag while the program is transferred, hence the initial offset.

I wrote a time set function that prompts for the time and waits for a button press to sync. Perhaps you should too.

Okay, I understood. And how did you wrote this function? Would you like to share your code? I tried to search this part in the project you linked before, but I didn't found it...sorry I'm not very skilled with Arduino yet

He wrote DS1307

@ruilviana the OP also wrote:

Please pay attention.

Hi,
The DS1307 is a low-precision RTC. The DS3231 has a higher precision, but still needs an adjustment for each case.
I have one here at home that advances about 1 second every 2 months in relation to the NTC time.
But it seems that this is not the problem you are having.
If I'm wrong, correct me.
You want to set your RTC with the same "time" as your note book,
but there is an initial delay between your RTC being adjusted and therefore it is a few seconds behind the note. This is it?

@jremington
Sorry, you're right.

Add this code to the bottom of setup() to set the time manually...

  DateTime dtetme = rtc.now();  
      
  char input[10];
  uint8_t x = 0;
  
  Serial.println("Enter the time to set (HHMMSS format)");
  while (Serial.available() == 0);

  while(Serial.available() > 0)
  {
    input[x++] = Serial.read();
    delay(5);
  }

  input[x] = '\0';
  
  Serial.print("Received :");
  Serial.println(input);
  
  uint8_t hour    = (input[0] - '0') * 10 + (input[1] - '0');
  uint8_t minute  = (input[2] - '0') * 10 + (input[3] - '0');
  uint8_t second  = (input[4] - '0') * 10 + (input[5] - '0');

  rtc.adjust(DateTime(dtetme.year(), dtetme.month(), dtetme.day(), hour, minute, second));

That is a good one. It is about 0.2 ppm which is more than 10 times better than the specified tolerance.

Yes, you centered my problem!

Hi,
use the serial monitor to adjust the seconds,
as suggested by @red_car .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.