Arduino and RTC

636 views
Skip to first unread message

Tom Gondek

unread,
Jan 20, 2014, 10:47:16 PM1/20/14
to milwaukee...@googlegroups.com
Hi,
 
I have a DS1307 Clock connected to an arduino via the I2C  bus.  Every now and then I get strange results.  For testing I am running a sketch that displays the time every two seconds.  When I had it displaying every second, it seemed to be worst.
 
I know I didn’t set the actual time, but it’s the random garbage I am talking about below Hour of 165 etc.  Sometimes it’s a few lines of  this, and other times its just one.
 
Any ideas on where to start looking?  Got the same results on two different setups.  I’ve tried two different libraries – “time” and “newrtc”
I don’t see a pattern, so I’m not sure how to start looking for the problem.
 
Any ideas / suggestions?
 
23:42:55 08-04-2011 FRI seconds since 1.1.2000:355621375 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A06 - Clock was set!
23:42:57 08-04-2011 FRI seconds since 1.1.2000:355621377 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A07 - Clock was set!
23:42:59 08-04-2011 FRI seconds since 1.1.2000:355621379 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A08 - Clock was set!
23:43:01 08-04-2011 FRI seconds since 1.1.2000:355621381 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A09 - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0A - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0B - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0C - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0D - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0E - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A0F - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A10 - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A11 - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A12 - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A13 - Clock was set!
165:165:85 165-165-2165 MON seconds since 1.1.2000:1076433489 MEZ=0, MESZ=1 : 0 - Address in NV-RAM is: A14 - Clock was set!
23:43:26 08-04-2011 FRI seconds since 1.1.2000:355621406 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A0A - Clock was set!
23:43:28 08-04-2011 FRI seconds since 1.1.2000:355621408 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A0B - Clock was set!
23:43:30 08-04-2011 FRI seconds since 1.1.2000:355621410 MEZ=0, MESZ=1 : 1 - Address in NV-RAM is: A0C - Clock was set!
 
 
this is the current sketch:
// #############################################################################
// #
// # Scriptname : DS1307_Test.pde
// # Author     : Peter Schmelzer, Oliver Kraus
// # Date       : 2011-04-08
// # Version    : 1.21
// # License    : cc-by-sa-3.0
// #
// # Description:
// # Test file for the DS1307new library. Assumes that you have a DS1307
// # connected to the I2C-Bus of your Arduino and that it has a battery backup.
// #
// #############################################################################
// *********************************************
// INCLUDE
// *********************************************
#include <Wire.h>                       // For some strange reasons, Wire.h must be included here
#include <DS1307new.h>
 
// *********************************************
// DEFINE
// *********************************************
 
// *********************************************
// VARIABLES
// *********************************************
uint16_t startAddr = 0x0000;            // Start address to store in the NV-RAM
uint16_t lastAddr;                      // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55;            // Helper that time must not set again
 
// *********************************************
// SETUP
// *********************************************
void setup()
{
  pinMode(2, INPUT);                    // Test of the SQW pin, D2 = INPUT
  digitalWrite(2, HIGH);                // Test of the SQW pin, D2 = Pullup on
 
  Serial.begin(9600);
 
/*
   PLEASE NOTICE: WE HAVE MADE AN ADDRESS SHIFT FOR THE NV-RAM!!!
                  NV-RAM ADDRESS 0x08 HAS TO ADDRESSED WITH ADDRESS 0x00=0
                  TO AVOID OVERWRITING THE CLOCK REGISTERS IN CASE OF
                  ERRORS IN YOUR CODE. SO THE LAST ADDRESS IS 0x38=56!
*/
  RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08
 
/*
   Uncomment the next 2 lines if you want to SET the clock
   Comment them out if the clock is set.
   DON'T ASK ME WHY: YOU MUST UPLOAD THE CODE TWICE TO LET HIM WORK
   AFTER SETTING THE CLOCK ONCE.
*/
//  TimeIsSet = 0xffff;
//  RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t)); 
 
/*
  Control the clock.
  Clock will only be set if NV-RAM Address does not contain 0xaa.
  DS1307 should have a battery backup.
*/
  RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
  if (TimeIsSet != 0xaa55)
  {
    RTC.stopClock();
       
    RTC.fillByYMD(2011,4,8);
    RTC.fillByHMS(22,7,0);
   
    RTC.setTime();
    TimeIsSet = 0xaa55;
    RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
    RTC.startClock();
  }
  else
  {
    RTC.getTime();
  }
 
/*
   Control Register for SQW pin which can be used as an interrupt.
*/
  RTC.ctrl = 0x00;                      // 0x00=disable SQW pin, 0x10=1Hz,
                                        // 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
  RTC.setCTRL();
 
  Serial.println("DS1307 Testsketch");
  Serial.println("Format is \"hh:mm:ss dd-mm-yyyy DDD\"");
 
  uint8_t MESZ;
 
  MESZ = RTC.isMEZSummerTime();
  Serial.print("MEZ=0, MESZ=1 : ");
  Serial.println(MESZ, DEC);   
  Serial.println();
}
 
// *********************************************
// MAIN (LOOP)
// *********************************************
void loop()
{
  RTC.getTime();
  if (RTC.hour < 10)                    // correct hour if necessary
  {
    Serial.print("0");
    Serial.print(RTC.hour, DEC);
  }
  else
  {
    Serial.print(RTC.hour, DEC);
  }
  Serial.print(":");
  if (RTC.minute < 10)                  // correct minute if necessary
  {
    Serial.print("0");
    Serial.print(RTC.minute, DEC);
  }
  else
  {
    Serial.print(RTC.minute, DEC);
  }
  Serial.print(":");
  if (RTC.second < 10)                  // correct second if necessary
  {
    Serial.print("0");
    Serial.print(RTC.second, DEC);
  }
  else
  {
    Serial.print(RTC.second, DEC);
  }
  Serial.print(" ");
  if (RTC.day < 10)                    // correct date if necessary
  {
    Serial.print("0");
    Serial.print(RTC.day, DEC);
  }
  else
  {
    Serial.print(RTC.day, DEC);
  }
  Serial.print("-");
  if (RTC.month < 10)                   // correct month if necessary
  {
    Serial.print("0");
    Serial.print(RTC.month, DEC);
  }
  else
  {
    Serial.print(RTC.month, DEC);
  }
  Serial.print("-");
  Serial.print(RTC.year, DEC);          // Year need not to be changed
  Serial.print(" ");
  switch (RTC.dow)                      // Friendly printout the weekday
  {
    case 1:
      Serial.print("MON");
      break;
    case 2:
      Serial.print("TUE");
      break;
    case 3:
      Serial.print("WED");
      break;
    case 4:
      Serial.print("THU");
      break;
    case 5:
      Serial.print("FRI");
      break;
    case 6:
      Serial.print("SAT");
      break;
    case 7:
      Serial.print("SUN");
      break;
  }
  Serial.print(" seconds since 1.1.2000:");
  Serial.print(RTC.time2000, DEC);
  uint8_t MESZ = RTC.isMEZSummerTime();
  Serial.print(" MEZ=0, MESZ=1 : ");
  Serial.print(MESZ, DEC); 
  Serial.print(" - Address in NV-RAM is: ");
  RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
  Serial.print(lastAddr, HEX);
  lastAddr = lastAddr + 1;              // we want to use it as addresscounter for example
  RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
  RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
  if (TimeIsSet == 0xaa55)              // check if the clock was set or not
  {
    Serial.println(" - Clock was set!");
  }
  else
  {
    Serial.println(" - Clock was NOT set!");
  }   
  delay(2000);                          // wait a second
}

Larry Andersen

unread,
Jan 21, 2014, 8:49:56 AM1/21/14
to milwaukee...@googlegroups.com
How long and how shielded are the lines from the clock chip to the Arduino?  My guess is noise on the lines.  I've built several clocks like this and have found them reading a LOT of garbage from the clock chips.  I ended up adding code to filter out the inconsistent results.

Larry
--
 
---
You received this message because you are subscribed to the Google Groups "milwaukeemakerspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to milwaukeemakers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bill Dussault

unread,
Jan 21, 2014, 9:36:37 AM1/21/14
to milwaukee...@googlegroups.com
I have some code of the RTC and Arduino I used several years ago. it worked very well but the noise issue mentioned here is very real. for lines longer than 3" it's easy to pick up noise. I suggest sending the signal twice for checking. 

Bill

Royce Pipkins

unread,
Jan 21, 2014, 9:40:01 AM1/21/14
to milwaukee...@googlegroups.com
Is it always hour 165? Could it just be an initialization value in the library that is getting returned because the library can't successfully talk to the chip?


--
 
---
You received this message because you are subscribed to the Google Groups "milwaukeemakerspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to milwaukeemakers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
The real problem is not whether machines think but whether men do.
B. F. Skinner

Tom Gondek

unread,
Jan 21, 2014, 10:45:23 AM1/21/14
to milwaukee...@googlegroups.com
Nope,  It was all over the place, sometimes it was just garbage like that,  other time it would be totally different time (in the actual time / date format, but just way off)
 
Another thing I forgot to mention,  My original setup also had an I2C LCD screen connected to the bus, and that seemed to work fine – always displayed what ever I sent – no garbage etc.  Once I saw the problems with the clock, I removed it from the setup to simplify the circuit with just the RTC.  I have 4.7K pull up’s on both I2C lines.
To unsubscribe from this group and stop receiving emails from it, send an email to mailto:milwaukeemakerspace%2Bunsu...@googlegroups.com.



--
The real problem is not whether machines think but whether men do.
B. F. Skinner

Ron Bean

unread,
Jan 21, 2014, 11:28:51 AM1/21/14
to milwaukee...@googlegroups.com
>Another thing I forgot to mention, My original setup also had an I2C
>LCD screen connected to the bus, and that seemed to work fine
>�\200\223 always displayed what ever I sent �\200\223 no garbage
>etc.

I was going to ask about this. If other things can work with I2C, why
not the clock chip? Is there something wrong with its I2C interface?
Are there other ways to suppress noise on the bus?

(And, how far is it from the CPU? I'd mount them right next to each
other.)

Also, is there some reason why you need to read it so often, or is that
just for testing?

Royce Pipkins

unread,
Jan 21, 2014, 11:37:25 AM1/21/14
to milwaukee...@googlegroups.com
The even though you see multiple values at different times, plainly in the log you show you had an extended period where
you just kept getting the same value back over and over. Electrical noise doesn't typically follow that kind of pattern. I would have expected each log entry to be different. Alternately, if the software were detecting the noise some how I'd expect all the log entries to always be the same. Some initial value.

I might start looking at the library since you can just read that code. The other thing to do is break out our logic analyzer and set it to decode bus traffic. You'll have to come to understand the wire protocol, but at least you'd be able to see if the chip is really saying what appears in your log.




--

---
You received this message because you are subscribed to the Google Groups "milwaukeemakerspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to milwaukeemakers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Tom Gralewicz

unread,
Jan 21, 2014, 4:07:13 PM1/21/14
to milwaukeemakerspace
Random things to try:

1)  Add a pair of capacitors as close to the time chip as possible between VCC and Ground:  10uf and 0.1uf

2) Reset the Register Pointer in the clock before you read it - it will pick up where it left off if it got out of sync on the last transfer.

3) Try a slower baud rate


Tom Gralewicz
Chronic Maker

Reply all
Reply to author
Forward
0 new messages