Wrong return values - DS1307 RTC code question

by powellite on Wed Oct 26, 2011 4:15 pm

Wrong return values - DS1307 RTC code question

Hi;
I've read most of the FAQ and queried this forum (search RTClib).
I'm having a problem with the returned vals using the familiar RTClib library.

I'm using http://www.ladyada.net/learn/breakoutpl ... 07rtc.html web page containing a good sample source:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#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
RTC.adjust(DateTime(__DATE__, __TIME__)); //yes - I want it to adjust to compile time
}
<snip the rest>

I'm running the entire source code on an arduino uno using the RTC clock (purchased from adafruit) The returned values via serial monitor are 2043/5/6 17:32:14 even though the code was compiled and uploaded on October 26,2011 at 13:02. Why is the time so far off. I've tried three different RTC units and 3 different computers /WinXp and Win7 and have the same result.

Can anyone point out how to fix so the year, month, day and time are correct?

Thanks
Dan (powellite)
Sorry about cross-posting. I'm new to the ardunino forum and its wonderful intricacies.
powellite
 
Posts: 6
Joined: Mon Sep 26, 2011 3:54 pm

Re: Wrong return values - DS1307 RTC code question

by adafruit_support_bill on Wed Oct 26, 2011 4:42 pm

Do you see the "RTC is NOT running!" message in the serial monitor?
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Wrong return values - DS1307 RTC code question

by powellite on Wed Oct 26, 2011 4:47 pm

Nope, no message so I assume the RTC IS running.

Thanks
Dan
powellite
 
Posts: 6
Joined: Mon Sep 26, 2011 3:54 pm

Re: Wrong return values - DS1307 RTC code question

by adafruit_support_bill on Wed Oct 26, 2011 6:31 pm

If it is already running, then the code will not make the call to:
Code: Select all
RTC.adjust(DateTime(__DATE__, __TIME__)); //yes - I want it to adjust to compile time

If you want to to make that call unconditionally, remove the "if (! RTC.isrunning())"
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Wrong return values - DS1307 RTC code question

by powellite on Wed Oct 26, 2011 7:28 pm

Thanks but that does not help.

When run the program tells me:
since 1970 = 26781d

which is actually incorrect since since 1970, 14899 days have elapsed. I am thinking there is a inherited miscalculation in the library - perhaps to due to CPU cycles??

Thanks

Dan
powellite
 
Posts: 6
Joined: Mon Sep 26, 2011 3:54 pm

Re: Wrong return values - DS1307 RTC code question

by JamesC4S on Wed Oct 26, 2011 7:56 pm

powellite wrote:Thanks but that does not help.
which is actually incorrect since since 1970, 14899 days have elapsed. I am thinking there is a inherited miscalculation in the library - perhaps to due to CPU cycles??


If your RTC thinks it is 2043, why would the time since 1970 be correct? 2043-1970 = 73 years. 73 * 365 = 26645.
User avatar
JamesC4S
 
Posts: 115
Joined: Sun Sep 26, 2010 10:49 pm
Location: Austin, TX

Re: Wrong return values - DS1307 RTC code question

by adafruit_support_bill on Thu Oct 27, 2011 4:46 am

I am thinking there is a inherited miscalculation in the library

There are a lot of people using it successfully on logger shields, DS1307 breakouts and Chronodots.

When run the program tells me:
since 1970 = 26781d

And you modified the code to unconditionally call RTC.adjust?

Can you post the exact code you are using?
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Wrong return values - DS1307 RTC code question

by powellite on Thu Oct 27, 2011 3:55 pm

Thanks for your reply.

Yes, I realize that many, many people are successfully using the code , which is why I find it perplexing that I'm getting the wrong values returned.

The SRC code is available directly on:
http://www.ladyada.net/learn/breakoutpl ... 07rtc.html

I just block copied the code (as is) into the arduino software, used the correct RTClib library and it compiles without any problems. It's just the vals that are incorrect. I'm sure its something I'm doing wrong....just don't know what. Seems simple enough but...

Here's the code (copied directly off the above-mentioned link)

[Edit - moderator - use code block]
Code: Select all
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#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
    //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();
 
    Serial.print(" since 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.unixtime() + 7 * 86400L + 30);
 
    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);
}




thanks
PS*
//RTC.adjust(DateTime(__DATE__, __TIME__)); is run once in non-commented version then commented again so you don't reset the RTC each run.


Dan - an obvious newbie..
powellite
 
Posts: 6
Joined: Mon Sep 26, 2011 3:54 pm

Re: Wrong return values - DS1307 RTC code question

by adafruit_support_bill on Thu Oct 27, 2011 4:15 pm

You said earlier:
Nope, no message so I assume the RTC IS running.


The conditional code does not attempt to set the time unless it detects that the RTC is not running. It appears that it was set once with the wrong time and is continuing to run with the wrong time. Using that code, it will never reset the time.
//RTC.adjust(DateTime(__DATE__, __TIME__)); is run once in non-commented version then commented again so you don't reset the RTC each run.


But you need to set the time or it will continue to run with the wrong time. To reset the time, you need to do what I said before: Comment out the conditional statement and call rtc.adjust() unconditionally.

Code: Select all
 
// unconditionally reset the time!
//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(__DATE__, __TIME__));
}
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

RE: erroneous RTC return values - NOW correct

by powellite on Thu Oct 27, 2011 5:47 pm

Thanks to all for their help. You folks are GREAT !! <insert hand clapping here>

It was actually my newbie error. It was a hardware mis-location. I had the RTC clock board SDA and SCL input plugged into the A4 and A3 (analog in) ardunino board when they should have been plugged into the A5 and A4. I re-read the I2C info regarding arduino uno.

SCL is the i2c clock pin - its required to talk to the RTC
SDA is the i2c data pin - its required to talk to the RTC

In my attempt to pin down the problem, I tried a recently purchased and third RTC clock - one from Sparkfun and when it gave me the same erroneous vals, I knew it had to be a bad electrical issue. Three clocks, from two manufacturers can't be wrong and few if any people have problems with the RTClib code so I figured it was something I did.

Sorry - I'm old and the eyes are even worse..... but I re-learned a old matra "check the wiring...check the wiring...check the wiring..."

It's now working perfectly.
Thanks again

Dan
powellite
 
Posts: 6
Joined: Mon Sep 26, 2011 3:54 pm

Re: Wrong return values - DS1307 RTC code question

by captainhooyah on Sat Dec 08, 2012 9:54 pm

Hey Guys, I just wanted to say I was getting crazy dates from my 1307 too, I did what you suggested and it fixed the problem. When I had my wires crossed like powellite had mentioned the numbers were.

2165/165/165 165:165:85
since midnight 1/1/1970 = 1393371985s = 16126d
now + 7d + 30s: 2014/3/4 23:46:55

2165/165/165 165:165:85
since midnight 1/1/1970 = 1393371985s = 16126d
now + 7d + 30s: 2014/3/4 23:46:55

Thanks for your help!
captainhooyah
 
Posts: 2
Joined: Sat Dec 08, 2012 9:49 pm

Re: Wrong return values - DS1307 RTC code question

by captainhooyah on Sat Dec 08, 2012 9:57 pm

I just want to clarify my problem was the if statement, not the crossed wires. I just swapped them when I was troubleshooting and wanted to document it.
captainhooyah
 
Posts: 2
Joined: Sat Dec 08, 2012 9:49 pm

Re: Wrong return values - DS1307 RTC code question

by rbright on Mon Dec 10, 2012 7:20 am

Using the ada code above I found time was way out using 3 different RTC modules. Solution was on each to remove the backup battery for a minute then refit & run and all runs OK.
Interesting issue I've since found is that the clock after loading with RTC.adjust(DateTime(__DATE__, __TIME__)); then runs a couple of minutes slow when printed out with the lines
DateTime now = RTC.now(); etc..... Sure the code doesn't run that slow.
Obvious thing to try is run program connected to another PC to see the effect.
Comments?
rbright
 
Posts: 3
Joined: Mon Dec 10, 2012 7:11 am

Re: Wrong return values - DS1307 RTC code question

by adafruit_support_bill on Mon Dec 10, 2012 7:27 am

"DateTime(__DATE__, __TIME__)" gives you the compile time, so it will always be at least a few seconds slow due to the time it takes to upload and restart. If you use the conditional code "if (! RTC.isrunning())", the time will only get adjusted if the clock is stopped. If you remove the conditional, every time you restart the Arduino it will 'adjust' the clock back to the compile time.
User avatar
adafruit_support_bill
 
Posts: 25601
Joined: Sat Feb 07, 2009 9:11 am

Re: Wrong return values - DS1307 RTC code question

by rbright on Mon Dec 10, 2012 9:20 pm

Yes compile time will have an effect but loosing 2 minutes seem excessive.

I've combined the RTC code in a sketch with LCD code to both make a simple LCD display while also sending serial strings to monitor.

Would it be possible to add a correction factor to the minutes / seconds to restore the correct time.
I'm thinking of something between the DateTime now=RTC.now(); and where the data is used to display but I can't find a reference to the syntax for DateTime now instruction.
Any further help appreciated
rbright
 
Posts: 3
Joined: Mon Dec 10, 2012 7:11 am