1

I'm using an RTC DS1307 to keep track of time for long periods. It seems to be stuck in a function. I'm using the RTClib suggested by adafruit.

How can I get it unstuck and get values returned? Are there any debugging or troubleshooting tips that would be helpful?

Referencing this excerpt for the code I'm running.

void setup () {
  while (!Serial); // for Leonardo/Micro/Zero

  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) { //// **Where I think my code errors.**
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

This is the example code modified slightly to increase logging, included with RTClib. It's for the DS1307.

Currently the output from the serial port is as follows:

RTC is found.

I changed the code to the below:

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

    RTC_DS1307 rtc;

    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    void setup () {
      while (!Serial); // for Leonardo/Micro/Zero

      Serial.begin(9600);
      if (! rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1);
      } else {
        Serial.println("RTC is found.");
      }

      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(F(__DATE__), F(__TIME__)));
        // This line sets the RTC with an explicit date & time, for example to set
        // January 21, 2014 at 3am you would call:
        // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
      } else {
        Serial.println("RTC is RUNNING.");
      }
    }

    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(daysOfTheWeek[now.dayOfTheWeek()]);
        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 midnight 1/1/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 + TimeSpan(7,12,30,6));

        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);
    }
  • What is the serial output? – dhimaspw Nov 21 '16 at 7:41
  • @dpw per your suggestion, I included the serial output. I also included full code to review. – remydib Nov 21 '16 at 17:59
  • Try changing the battery. – Gerben Nov 21 '16 at 19:25
  • @Gerben I've checked the battery voltage multiple times. It has 2.98 volts under load and 2.98 off load. Is this acceptable? – remydib Nov 21 '16 at 19:38
  • That's just fine. – Gerben Nov 21 '16 at 19:40
1

First thing I see is the Serial setup seems a bit backward:

  while (!Serial); // for Leonardo/Micro/Zero
  Serial.begin(57600);

First, begin Serial; then wait for it to become ready:

  Serial.begin(57600);
  while (!Serial); // for Leonardo/Micro/Zero

Next, is this while(1); inside the if statement:

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

Which says "if !rtc.begin(), then println and go into an infinite loop that does nothing".

Try:

while (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    //delay(500);  // depending on how fast you want this thing to retry/print
}

the final thing I see, is the next section of code appears like it might be "work code" but is in the setup() function and is missing any kind of a loop. So it's going to run the if statement once, then setup() exits (and loop() starts getting called).

Either replace the "if" with a "while" and wait for "rtc.running()"

while (!rtc.running()); // Waiting for RTC to be running
// Set the clock time

or move it out of setup() and put it inside loop()

void loop() {
  if (! rtc.isrunning()) { //// **Where I think my code errors.**
    Serial.println("RTC is NOT running!");
    // Perhaps try and fix it?
  } else {
    // If the time hasn't been set; set it; then move on
  }
} 
  • You make a good point that I'm asking about my program not responding, and I have all these loops that don't end and don't respond! This is the example code that came with the library. I believe it worked before. – remydib Nov 21 '16 at 17:06
0

Wow, that was hard to figure out. My RTC DS1307 came with an extra pin. This tripped me up when wiring the circuitry. I was off by a pin on my breadboard. Thank you everyone for your help.

Specifically, I had the ground going to the empty pin, and the 5v line going to the ground. Perhaps the circuitry allowed some current to go from the ground up and triggered the "i am here" part of the circuitry.

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.