0

DS1307 on arduino: cannot update clock
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

DS1307 on arduino: cannot update clock

by resodad on Fri Aug 03, 2018 11:29 am

Hello,
I am trying to set the time on a DS1307 RTC on an Adafruit SD data logging shield.
I checked to make sure this is the right RTC chip (the board in not labeled with the newer RTC)
I have a battery installed.
I have the ETC Library installed.
The example code below loads and runs without error, but the clock does not update.
I also tried uncommenting the rtc.adjust(DateTime(2018, 8, 3, 10, 40, 0)); line instead.
I tried this on an odler Uno, an newer Uno, and a Mega2650.
Same result.
The serial monitor displays the time, but it is incorrect:

2018/8/3 (Friday) 11:29:10
since midnight 1/1/1970 = 1533295750s = 17746d
now + 7d + 30s: 2018/8/10 23:59:16

Please let me know if you see what I'm doing wrong.
Thanks,
John

Code: Select all | TOGGLE FULL SIZE
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

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

void setup () {
  while (!Serial); // for Leonardo/Micro/Zero

  Serial.begin(57600);
  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(2018, 8, 3, 10, 40, 0));
  }
}

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(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();
   
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
   
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
   
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
   
    Serial.println();
    delay(3000);
}

resodad
 
Posts: 4
Joined: Fri Aug 03, 2018 11:16 am

Re: DS1307 on arduino: cannot update clock

by Zaven on Mon Aug 06, 2018 4:04 am

I have a similar problem. I get UTC time. Same problem with DS3231.
I use Windows 10.
I didn't have this problem before Microsoft uptated automatically my Windows.
Could this be related?

I'll appreciate your help.
Thanks

Zaven
 
Posts: 2
Joined: Tue Nov 15, 2016 1:34 pm

Re: DS1307 on arduino: cannot update clock

by adafruit_support_carter on Tue Aug 07, 2018 4:40 pm

The serial monitor displays the time, but it is incorrect:

2018/8/3 (Friday) 11:29:10

It seems close. What was it suppose to be? Keep in mind that this line of code:
Code: Select all | TOGGLE FULL SIZE
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

doesn't run everytime. Only if the RTC isn't configured.

adafruit_support_carter
 
Posts: 6881
Joined: Tue Nov 29, 2016 2:45 pm

Re: DS1307 on arduino: cannot update clock

by resodad on Wed Aug 08, 2018 8:37 am

It is off by 10 minutes.
It has drifted that much over several months.
I only run the line containing:
rtc.adjust(...
when I am trying to set the clock, otherwise it remains commented.

John

resodad
 
Posts: 4
Joined: Fri Aug 03, 2018 11:16 am

Re: DS1307 on arduino: cannot update clock

by adafruit_support_carter on Wed Aug 08, 2018 10:35 am

Just to be clear - is the problem 10 minutes of drift over several months? or is the problem the time not being correctly set initially?

adafruit_support_carter
 
Posts: 6881
Joined: Tue Nov 29, 2016 2:45 pm

Re: DS1307 on arduino: cannot update clock

by resodad on Wed Aug 08, 2018 10:47 am

Neither.
The clock was set correctly months ago.
Yes, it's drifting, but I'm not concerned about that.
I am trying to reset the clock so that the time is correct now.
The device does not update when I run the rtc.adjust command.
John

resodad
 
Posts: 4
Joined: Fri Aug 03, 2018 11:16 am

Re: DS1307 on arduino: cannot update clock

by adafruit_support_carter on Wed Aug 08, 2018 10:58 am

The device does not update when I run the rtc.adjust command.

If you're running that using the code you posted above, you would need to see this printed in the serial monitor:
Code: Select all | TOGGLE FULL SIZE
RTC is NOT running!

to know that the rtc.adjust() command was actually run. Do you?

adafruit_support_carter
 
Posts: 6881
Joined: Tue Nov 29, 2016 2:45 pm

Re: DS1307 on arduino: cannot update clock

by resodad on Wed Aug 08, 2018 12:00 pm

Ah...
I see.
Thanks.
the code:
Code: Select all | TOGGLE FULL SIZE
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

was inside the if code:
Code: Select all | TOGGLE FULL SIZE
if (! rtc.isrunning()) {}


and since the clock was already running, it never got to the code I needed inside the if.

Problem solved.
John

resodad
 
Posts: 4
Joined: Fri Aug 03, 2018 11:16 am

Please be positive and constructive with your questions and comments.