RTC Gurus please Help me

Hi,
I spend over 15 hours fighting to get my RTC module one like thishttp://www.emartee.com/product/42059/Tiny%20RTC%20DS1307%20Shield%20V2.0) to work. I checked at least 100 times connectors - set as on webpage, red 100 topics on this forum but still cant make it run. I’m using IDE 1.0.3 with this sketch:

// Date and time functions using just software, based on millis() & timer

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

RTC_Millis RTC;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.begin(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(" seconds since 1970: ");
    Serial.println(now.unixtime());
    
    // 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);
}

in serial monitor comes out my time:

2013/3/3 20:40:36
 seconds since 1970: 1362343236
 now + 7d + 30s: 2013/3/10 20:41:6

2013/3/3 20:40:39
 seconds since 1970: 1362343239
 now + 7d + 30s: 2013/3/10 20:41:9

2013/3/3 20:40:42
 seconds since 1970: 1362343242
 now + 7d + 30s: 2013/3/10 20:41:12

but after I close serial monitor and open it later it starts on same time. When I upload this code with no RTC attached happens same thing - as RTC wouldn’t be there anytime.
I changed the battery - same thing. Is there any code to check if RTC module is good? Can be this affected with many different libraries for DS 1307 i have in Libraries folder(I think I downloaded and tried all of them :))
THANKS a lot I’m desperate…

What does this say to you?

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.begin(DateTime(__DATE__, __TIME__));
}

But how can I modify this sketch to set the right time?

The whole point of the RTC is that you don't need to set the "right" time. It remembers it.

I suggest two sketches. One which does the initial setting of the time and storing it in the RTC, and the other "working" sketch which uses the time in the RTC.

The way you have it at the moment won't even remotely work. Every time you connect to the serial port the Arduino resets. That will reset the time in the RTC to the time the sketch was compiled.

Alright, Can you give me clue what sketches to use, please?

Well, you could use a sketch of the Mona Lisa…

Or you could simply split your current sketch in two - remove the bit that sets the time from the existing sketch and put it in a new sketch.

Well, this didn't help me a lot.I cant compile Mona Lisa :)(not declared in this scope :)), and I'm beginner, need more dummies like stuff. Sorry

Compile and upload your program again. When you do this it replaces the tokens DATE and TIME with the current date time.

Then without unplugging the Arduino, remark out the line

RTC.begin(DateTime(DATE, TIME));

And compile and upload the sketch again.

Thanks Dannable,
I loaded code

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

RTC_Millis RTC;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
   RTC.begin(DateTime("Mar 04 2013","20:20:20"));
}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
.
.
.

serial print was ok like:

2013/3/4 20:20:20
 seconds since 1970: 1362428420
 now + 7d + 30s: 2013/3/11 20:20:50

After that I remarked RTC setup

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
   //RTC.begin(DateTime("Mar 04 2013","20:20:20"));
}

void loop () {

and the serial monitor shows

2106/2/6 6:28:16
 seconds since 1970: 0
 now + 7d + 30s: 2106/2/13 6:28:46

You still need an RTC.begin in your second version of the code to enable the RTC module.

Try replacing

   RTC.begin(DateTime("Mar 04 2013","20:20:20"));

with a simple

  RTC.begin();

in the second version.

Hi majenko,
cant compile, I'm getting

sketch_mar04a.ino: In function 'void setup()':
sketch_mar04a:12: error: no matching function for call to 'RTC_Millis::begin()'
D:\Arduino\arduino-1.0.3\libraries\RTClib/RTClib.h:41: note: candidates are: static void RTC_Millis::begin(const DateTime&)

Can you post a link to the RTC library you are using?

Its the one from adafruit tutorial on github:https://github.com/adafruit/RTClib

Aha... You're not actually using your RTC module at all.

RTClib.h:
// RTC using the internal millis() clock, has to be initialized before use
// NOTE: this clock won't be correct once the millis() timer rolls over (>49d?)
class RTC_Millis {

You need to be using the RTC_DS1307 class instead. You then have RTC.begin(); and RTC.adjust(DateTime(DATE, TIME));

The begin() call turns the module on, and the adjust(...) call sets the time and date in the RTC module itself - only ever needs doing once to set the time initially.

Mikee:
Its the one from adafruit tutorial on github:https://github.com/adafruit/RTClib

If you compare the "ds1307" example with the "softrtc" example from the library, it will become clear...

You want to use the "ds1307" example. :slight_smile:

Cheers,
John

Hi, its me again, annoying RTC guy :sweat_smile:,
I modified my code to:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
//RTC_Millis RTC;

void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
   //RTC.begin(DateTime("Mar 04 2013","20:20:20"));
  RTC.begin();
 RTC.adjust(DateTime("Mar 04 2013","20:20:20"));
}

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(" seconds since 1970: ");
    Serial.println(now.unixtime());
    
    // 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);
}

and I tried also “ds1307” example from RTC library but output is still:

RTC is NOT running!
2165/165/165 165:165:85
 since midnight 1/1/1970 = 1381535185s = 15989d
 now + 7d + 30s: 2013/10/18 23:46:55

Thanks, that's nice from you. I do have one, at least I was told that is RTC

Those figures are typical of no communication between the Arduino and the RTC. The RTC is an I²C device, so it must be connected correctly to the right I²C pins (depending on your board), and have pull-up resistors on the lines of typically 4.7K?.

Do you have that?

Can you provide some good clear photographs of your setup?

Im conected like this:
Vcc - 5V
GND - GND
SDA - A4
SCL - A5
no resistor

Looks to me like there should be a Wire.begin() in there...