Go Down

Topic: RTC acting strange (Read 4023 times) previous topic - next topic

megamax42

The RTC in have wired to my arduino has been acting rather strange.


First, it doesn't seem to be keeping the correct time. I had the program ( a simple dim on/off program for LED's, trying to simulate sunrise/sunset) working perfectly last night, then when I wake up this morning they haven't started their dim on cycle like they should have. I plug the usb cable into the computer and open the serial monitor it reads the time as 10:47 a.m. instead of 11:36 a.m. This leads me to my second problem.

Every once in a while when I try to recompile and upload it, when I open the serial monitor it gives a completely different time than the computer, which is what it should be setting itself off of. For example it's 11:36 a.m. right now (correct on my computer) and after recompiling and re-uploading the program still thinks it is 10:47 a.m.

Additionally I watched the serial monitor for a moment and caught this:


2012/10/14 10:49:22
since 1970 = 1350211762s = 15627d
now + 7d + 30s: 2012/10/21 10:49:52
2012/10/14 10:49:24
since 1970 = 1350211764s = 15627d
now + 7d + 30s: 2012/10/21 10:49:54


I set the delay to (1000) so it should print every second, but sometimes it skips one.

Then scrolled along and found something even more disturbing:


2012/10/14 10:49:56
since 1970 = 1350211796s = 15627d
now + 7d + 30s: 2012/10/21 10:50:26
2012/10/14 10:49:57
since 1970 = 1350211797s = 15627d
now + 7d + 30s: 2012/10/21 10:50:27
2012/10/14 10:49:58
since 1970 = 1350211798s = 15627d
now + 7d + 30s: 2012/10/21 10:50:28
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
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
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
2165/165/165 165:165:85
since 1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:15
since 1970 = 2028820619s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2012/10/14 10:50:10
since 1970 = 1350211810s = 15627d
now + 7d + 30s: 2012/10/21 10:50:40
2012/10/14 10:50:11
since 1970 = 1350211811s = 15627d
now + 7d + 30s: 2012/10/21 10:50:41
2012/10/14 10:50:12
since 1970 = 1350211812s = 15627d
now + 7d + 30s: 2012/10/21 10:50:42


Originally the code had a delay of 3000 ms, could this perhaps be the problem? Thanks ahead of time to those who read this.

Here's the actual code:
Code: [Select]
// 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;

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

   
  if (future.hour() == 11 && future.minute() == 35)
  {
    fadeAmount = .14167;
  }
  else if (future.hour() == 12 && future.minute() == 5)
  {
    fadeAmount = 0;
  }
  else if (future.hour() == 21 && future.minute() == 0)
  {
    fadeAmount = -.14167;
  } 
  else if (future.hour() == 21 && future.minute() == 30)
  {
    fadeAmount = 0;
  }
       
    analogWrite(led, brightness);
   
    brightness = brightness + fadeAmount;
     
         
    delay(1000);   
     
}

dannable

Where did you get the RTClib from?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

megamax42

I bought everything RTC related off of DipMicro.com but got the code off of ladyada.
http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html


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:
Code: [Select]
 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.

megamax42

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.

megamax42

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!!!!

dannable

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?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

liudr

dannable,

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

megamax42

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."

robtillaart

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:

Code: [Select]

// 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();
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

megamax42

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

megamax42

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

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

dannable

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.
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

megamax42

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

megamax42

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

megamax42

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:

Go Up