RTC acting strange

Where did you get the RTClib from?

I bought everything RTC related off of DipMicro.com but got the code off of ladyada.
DS1307 RTC tutorial

Just tried re-uploading again and now I have an even weirder response, despite not changing anything.

¥ %¡»bâs Aá¹ÿRTC is NOT running!
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39

repeated over and over

Something that's always confused me though is the error checking to see if the RTC is running:

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

Why would it print "RTC is NOT running!" if the conditions were that it was running?
I should probably add that it has always printed "RTC is NOT running!", even when it was working
correctly.

Tried re-uploading again and at first it was acting weird again but then all of a sudden cleared itself up. What's going on here??

2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2012/10/14 12:5:54
since 1970 = 1350216354s = 15627d
now + 7d + 30s: 2012/10/21 12:6:24
2012/10/14 12:5:55
since 1970 = 1350216355s = 15627d
now + 7d + 30s: 2012/10/21 12:6:25
2012/10/14 12:5:56

And now, not only has it been unstuck from 17:18:39, but it's the correct time, as opposed to earlier when it was counting correctly but about an hour and ten minutes off. Again, I changed nothing except re-uploading the program.

Well it was working correctly for a minute, then went back to 17:18:39.

I just unplugged the arduino entirely from power, plugged it back in and reuploaded the program and for the first time it didn't print 'RTC is not running' and instead printed this:

#Râ¨Mâò:'Ï@Çbâ¨Iâò:'Í@Ë2012/10/14 12:6:18
since 1970 = 1350216378s = 15627d
now + 7d + 30s: 2012/10/21 12:6:48
2012/10/14 12:6:19
since 1970 = 1350216379s = 15627d
now + 7d + 30s: 2012/10/21 12:6:49
2012/10/14 12:6:20
since 1970 = 1350216380s = 15627d
now + 7d + 30s: 2012/10/21 12:6:50

Right now my only guess is it's a bad RTC chip because it is acting so abnormally. I have no idea why it would work sometimes though and not others. This is driving me CRAZY!!!!

The 'RTC is NOT running' message is worrying. There is a flag on the chip that is reset to 1 when the power is first applied. Is the battery ok and making good contact?

dannable,

You beat me by seconds. I was about to suggest removing the battery and try again.

Since the whole system is still in the early stages and experiencing lots of unplugging I decided to remove the battery from the circuit to prevent it from draining. The pin for the battery is currently wired to ground, which is a note on ladyada:

"You MUST have a coin cell installed for the RTC to work, if there is no coin cell, you should pull the battery pin low."

Your code does take quite some time to do all those the serial prints. Add that time to the delay(1000) and the time between 2 prints will be definitely more than 1 second (say ~1.1 second) so once in about 10 times it will skip a second.

instead of using delay(1000) you better check if 1000 millisec have gone by.

Give this (untested) code a try:

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

// Declarations
int led = 2;
float brightness = 0;
float fadeAmount = 0;

unsigned long prevTIme = 0;  // must be global to keep value over multiple calls of loop()
 
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__));
  }
  pinMode(led, OUTPUT);

}



void loop () 
{
  // These next 4 lines do the 1 second timing 
  unsigned long t = millis();
  if (t - prevTime >= 1000)
  {
    prevTime = t;

    DateTime now = RTC.now();
    printTime(now);
 
    unsigned long tt = now.unixtime();       // take a snapshot 

    Serial.print(" since 1970 = ");
    Serial.print(tt);
    Serial.print("s = ");
    Serial.print(tt / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (tt + 7 * 86400L + 30);
 
    Serial.print(" now + 7d + 30s: ");
    printTime(future);

    fadeAmount = 0;
    
    if (future.hour() == 11 && future.minute() == 35) 
    {
    fadeAmount = .14167;
    }
    else if (future.hour() == 21 && future.minute() == 0)
    {
      fadeAmount = -.14167;
    } 
    brightness = brightness + fadeAmount;
    analogWrite(led, brightness); 
  }  // end if (t - prevTime >= 1000)

}


// to prevent double code this function
printTime(DateTime dt)
{
  Serial.print(dt.year(), DEC);
  Serial.print('/');
  Serial.print(dt.month(), DEC);
  Serial.print('/');
  Serial.print(dt.day(), DEC);
  Serial.print(' ');
  Serial.print(dt.hour(), DEC);
  Serial.print(':');
  Serial.print(dt.minute(), DEC);
  Serial.print(':');
  Serial.print(dt.second(), DEC);
  Serial.println();
}

First of all, thanks a bunch to everyone. I can't thank you enough for helping, it's gotten to the point of hair-pulling.

robtillaart,

I tried your code, at first I got an error on prevTime being undeclared. In the declarations it was prevTIme so I just changed the 'I' to a lowercase and it fixed that. But now I'm getting this:

sketch_oct13a.cpp: In function 'void loop()':
sketch_oct13a:41: error: 'printTime' was not declared in this scope
sketch_oct13a.cpp: At global scope:
sketch_oct13a:75: error: ISO C++ forbids declaration of 'printTime' with no type

Also is this 'end if' supposed to be commented out?

//end if (t - prevTime >= 1000)

I'd stick the battery back in. From the datasheet:

A lithium battery with 48mAh or greater will back up the DS1307 for more than 10 years in the absence of power at +25°C.

I'll give it a shot and report back, thanks again so much for the help.

Well, I put the battery back in the circuit, re-uploaded everything and at first instead of printing "RTC is NOT running" or some garbage text like " #Râ¨Mâò:'Ï@Çbâ¨Iâò:'Í@Ë " on the first line, it printed "#" , and then the time/date. But it was the wrong time/date. So I re-uploaded it and this time it printed what is below:

4F N I Ù2165/165/165 14:18:43
since 1970 = 2028268227s = 23475d
now + 7d + 30s: 2034/4/17 7:50:57
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2100/0/0 14:18:46
since 1970 = 4102409926s = 47481d
now + 7d + 30s: 2100/1/7 14:19:16
2100/0/0 14:18:47
since 1970 = 4102409927s = 47481d
now + 7d + 30s: 2100/1/7 14:19:17
2100/0/0 14:18:48
since 1970 = 4102409928s = 47481d
now + 7d + 30s: 2100/1/7 14:19:18
2165/165/165 165:165:55
since 1970 = 2028820659s = 23481d
now + 7d + 30s: 2034/4/23 17:18:9
2100/0/0 14:18:50
since 1970 = 4102409930s = 47481d
now + 7d + 30s: 2100/1/7 14:19:20
2100/0/0 14:18:51
since 1970 = 4102409931s = 47481d
now + 7d + 30s: 2100/1/7 14:19:21
2000/0/0 18:52:0
since 1970 = 2314009424s = 26782d
now + 7d + 30s: 2043/5/7 12:24:14
2165/165/165 25:18:53
since 1970 = 2028307837s = 23475d
now + 7d + 30s: 2034/4/17 18:51:7
2100/0/0 14:18:54
since 1970 = 4102409934s = 47481d
now + 7d + 30s: 2100/1/7 14:19:24
2100/0/0 14:18:55
since 1970 = 4102409935s = 47481d
now + 7d + 30s: 2100/1/7 14:19:25
2125/0/0 14:18:56
since 1970 = 596447440s = 6903d
now + 7d + 30s: 2125/1/7 14:19:26
2165/165/165 12:18:57
since 1970 = 2028261041s = 23475d
now + 7d + 30s: 2034/4/17 5:51:11
2165/165/165 165:165:45
since 1970 = 2028820649s = 23481d
now + 7d + 30s: 2034/4/23 17:17:59
2165/165/165 165:25:59
since 1970 = 2028812263s = 23481d
now + 7d + 30s: 2034/4/23 14:58:13
2165/165/165 17:19:0
since 1970 = 2028279044s = 23475d
now + 7d + 30s: 2034/4/17 10:51:14

Both the date and time are going crazy

My guess now is that perhaps it has something to do with the wiring, as I did have some confusion there.

My confusion was the datasheet at the top has a picture of a 'typical operating circuit', which has both SDA and SCL with a pullup resistor to Vcc, however further down the datasheet it says only SDA needs the pullup resistor and SCL doesn't say anything about the resistor, only that it should have a pullup voltage of 5.5V?

First picture at top of datasheet:

Pin description 1/3 the way down:

Any chance of a link to the RTC module you bought? Or is it a home brew?

Homebrew, but here's my part list:

Product: Plastic Battery Holder 1-cell ø20mm PC 120591-1
http://www.dipmicro.com/store/BH120591-1

Product: Lithium Battery CR2032 Ф20mm 225mAh 3V CR2032
http://www.dipmicro.com/store/BAT-CR2032

Product: 4.7k Ohm 1/4W Resistor ±1% 4k7 472
http://www.dipmicro.com/store/R3F47-4

Product: 32.768kHz Crystal Cylinder
http://www.dipmicro.com/store/XC4-32768

Product: DS1307 Dallas Maxim 64 x 8 Serial Real-Time Clock DIP8
http://www.dipmicro.com/store/DS1307N

And I wired it up based on the 'typical operating circuit', except without the scl being pulled to 5.5V.

It's also all wired up on a breadboard.

My confusion was the datasheet at the top has a picture of a 'typical operating circuit', which has both SDA and SCL with a pullup resistor to Vcc, however further down the datasheet it says only SDA needs the pullup resistor and SCL doesn't say anything about the resistor, only that it should have a pullup voltage of 5.5V?

Possibly worded a little awkward, but 'have a pullup voltage...' means also having a pull-up resistor. The I2C standard dictates that both data and clock signal use external pull-ups as these signals are bidirectional so they cannot rely on one side always providing a valid logic voltage level output. The actual value of the pull-up resistor can depend on things like the speed the bus will be operated on, the number of slave modules wired to the bus, that total length of the bus, etc. But 4.7K ohms is a pretty standard recommendations.

Now to confuse you further (not intended, really) one can sometime get away with wiring a I2C device to an arduino board with no pull-ups at all. That is because the arduino I2C library enables the internal 'weak pull-ups' on the open drain clock and data I2C pins. I've run a 1307 I2C RTC module with several of my arduino board without using external pull-ups and it works fine, however that is not per I2C standards and is not be a recommended practice.

Lefty

Hmm, interesting, I appreciate the informative post, though you are correct about me being confused XD.

I only have one 4.7k ohm resistor, so I had both SDA and SCL going through that to the voltage, but then that turned the entire system off, outputting nothing to the serial monitor. Is this just a case of requiring another resistor?

One other thing I should note is I have SQW/OUT wired to nothing.

You use the same setup I use on all my phi-1 and phi-2 shields (same source too), literally hundreds of them spread across the world. No reason it won't run unless you made some mistakes. First, make sure you have very short wires. Then make sure you did the pull-up correctly. Actual setup pictures are way better than your picture-perfect diagram. It is totally correct but whether you followed the diagram is up for debate. You can only end the debate by providing actual pictures. Is that clear enough?

Correct connections won't produce this:

I only have one 4.7k ohm resistor, so I had both SDA and SCL going through that to the voltage, but then that turned the entire system off, outputting nothing to the serial monitor.

The following is not going to affect the output:

One other thing I should note is I have SQW/OUT wired to nothing.

Very strange things are going on. I rewired almost everything with shorter wires like you suggested (I was using cheap male-to-male wire connectors and switched to stripping solid core 22 gauge wire) and this time I did nothing to pullup SCL, like before. This time the output seemed to be working, but displayed the time as 3:50, which is approximately the time I tried pulling SCL up with the same resistor as SDA. So I tried pulling SCL up again, and just like the last time I pulled it up, nothing worked and nothing output to the serial monitor. Just afterwards I tried the circuit without pulling up SCL and this time it reads 4:39. The actual time was 4:41, and 4:39 is when I tried to pull up SCL.

So I did this procedure over again, except tried to switch it as fast as possible, between having a pullup resistor and having no pullup resistor for SCL. I found the quicker I can switch it, the more accurate the time displays. So for some reason the chip's time is set when SCL is pulled up, but can only display the time when there's no voltage?

Just took some pictures, they'll up in a couple minutes.