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"


void setup () {

  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(now.month(), DEC);
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(now.minute(), DEC);
  Serial.print(now.second(), DEC);

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.... Commented Jul 27, 2018 at 13:55

5 Answers 5


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.


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

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);

Regards, Hugo Bertini


With the <DS1307RTC.h> library the DS1307 can be adjusted during time setup, by using a global variable and then adding this to Sec reading in the getTime() function, for example as follows:

#include <DS1307RTC.h>
tmElements_t tm;
byte adjustt = 8;  // adustment for seconds lag allowing for compiler download time


bool getTime(const char *str) {
  int Hour, Min, Sec;
  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec + adjustt;
  return true;

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.

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