1

RTC seems to be sporadically returning bogus date/times. Here are the logs from my serial monitor:

2016/3/23 8:19:46
2017/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2165/165/165 15:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46
2016/3/23 8:19:46

That 2165/165/165 15:19:46 appears throughout the logs.

Here's the relevant code:

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

RTC_DS1307 rtc;

void setup() {
  Serial.begin(9600);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  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();
}

I have a Grove RTC module.

  • 2
    165 is a failure to communicate. Check your wiring for loose connections. – Majenko Mar 23 '16 at 13:45
  • 1
    I think the single 2017 is also quite interesting (2017/3/23 8:19:46) – Gerben Mar 23 '16 at 16:09
  • Are you using an I2C chip on the end of a 20 foot length of wire? – Majenko Mar 23 '16 at 16:13
  • @Majenko Negative. The RTC module (which, to my understanding has the I2C built in) is attached to a ~10 inch piece of wire. – Shpigford Mar 23 '16 at 16:32
  • 1
    I think the single 2017 is also quite interesting - a single-bit error like that tends to suggest that there is indeed something wrong with the communication, like lack of pull-up resistors, or too long a cable. – Nick Gammon Mar 23 '16 at 21:03
1

Previous reports of this RTC problem (1,2,3) attribute it to hardware issues, such as broken wires (to SCL or to battery) and overvoltage (>3.6 V supply vs 3 to 3.3 V).

If your setup uses breadboards or other temporary connections, you might try adding some redundant wires. If your setup uses soldered connections, go over them carefully, looking for shorts and opens; or use a solder sucker / solder wick to remove old solder; then solder anew to make fresh, bright connections.

If you don't locate a hardware problem (and perhaps even if you do) you could add error-testing to your sketch. That code would see if all relevant fields have valid, in-range values. If any fields are invalid, it would delay a few milliseconds and then reread the RTC.

|improve this answer|||||
  • Loose wiring was the ultimate cause and making some adjustments in that area did the trick. – Shpigford Mar 24 '16 at 22:41
3

The RTC library does not check for communications success. In particular, here:

DateTime RTC_DS1307::now() {
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.send((byte) 0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);
  uint8_t ss = bcd2bin(Wire.read() & 0x7F);
  uint8_t mm = bcd2bin(Wire.read());
  uint8_t hh = bcd2bin(Wire.read());
  Wire.read();
  uint8_t d = bcd2bin(Wire.read());
  uint8_t m = bcd2bin(Wire.read());
  uint16_t y = bcd2bin(Wire.read()) + 2000;

  return DateTime (y, m, d, hh, mm, ss);
}

There should be a check for a zero returned from Wire.endTransmission(). It isn't otherwise possible to detect if the data is invalid (if we got the endTransmission OK) however generally speaking I2C will return 0xFF if the transmission has ceased.

The code in the library for bcd2bin is this:

static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }

You can see that if passed 0xFF (255) it will return:

   255 - (6 * 15) = 165

Thus if any of the minutes, hours, day or month (which are not further affected in the code above) are 165 (or if the year is 2165) you can assume the whole date/time is bad.

|improve this answer|||||
  • Thanks for the explanation! You mentioned in a comment about there being something potentially wrong w/ communications...specifically "lack of pull-up resistors". In my set up, I don't have any resistors added. I've got wires from the RTC that go to +/- and then A4 & A5 on the Uno. Should I add resistors somewhere in that set up? – Shpigford Mar 24 '16 at 13:30
  • Judging by the schematic I just downloaded, that board already has them. – Nick Gammon Mar 24 '16 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.