RTCLib Questions - SOLVED

Hello I'm trying to automatkcally set ds3231 rtc module using the RTCLib library. It is what I found online to work. The problem is there are some strange time numbers after the date. Also the date is in correct. And I do not know how to set it. I'm attaching sketch below and screenshot image.

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

RTC_DS3231 rtc;

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

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

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // 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));
  }

  // When time needs to be re-set on a previously configured device, the
  // 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));
}

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, 12 hours, 30 minutes, 6 seconds into the future
//    DateTime future (now + TimeSpan(7,12,30,6));
//
//    Serial.print(" now + 7d + 12h + 30m + 6s: ");
//    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.print("Temperature: ");
//    Serial.print(rtc.getTemperature());
//    Serial.println(" C");
//
//    Serial.println();
    delay(1000);
}

I have comment out the future time and date I honestly don't know what that is for Only just the current time and date.

It shows the incorrect date I do not know why. It shows sometimes Tuesday and Sometimes Thursday.

Screenshot

Can someone please help me out?

Joseph

Communication with the DS3231 is done using the Wire library which is i2c. If the chip does not respond the bus is left HIGH and the library will read all '1's and that translate into those bad dates.

Is this on a breadboard? You may be getting noise on your i2c bus... You also do not need to call the chip that often (multiple times per second)

Hello, Yes it is on a breadboard. I try to keep the jumper wires close to the module.

Edit: Let me try to get rid of the breadboard and go straight jumper wire to uno board.

Joseph

Just a update: It looks like it is showing the correct Day But incorrect date. Aso in the begining it shows 16:26:10.801 ->that is time but what is 801 at the end? also shows 17:12:45 at the end strange?

16:26:00.793 -> 2016/10/13 (Thursday) 17:12:35
16:26:01.768 -> 2016/10/13 (Thursday) 17:12:36
16:26:02.790 -> 2016/10/13 (Thursday) 17:12:37
16:26:03.771 -> 2016/10/13 (Thursday) 17:12:38
16:26:04.799 -> 2016/10/13 (Thursday) 17:12:39
16:26:05.775 -> 2016/10/13 (Thursday) 17:12:40
16:26:06.793 -> 2016/10/13 (Thursday) 17:12:41
16:26:07.769 -> 2016/10/13 (Thursday) 17:12:42
16:26:08.791 -> 2016/10/13 (Thursday) 17:12:43
16:26:09.816 -> 2016/10/13 (Thursday) 17:12:44
16:26:10.801 -> 2016/10/13 (Thursday) 17:12:45

Joseph

   //To set the RTC time include the following line of code:
   rtc.adjust(DateTime(2014, 01, 21, 03, 00, 00));
   //                  YYYY  MM  DD  hh  mm  ss

After you have uploaded the code, comment that line of code and upload a second time.

You have checked the "Show timestamp" in your Serial Monitor [lower left corner]. It is the time this text was received.

That line been commented out.

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


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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // 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));
  }

  // When time needs to be re-set on a previously configured device, the
  // 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));
}

Joseph

LOL oh man A Duhhh moment for me, I did not see that Thank you. I got to figure out this date problemnoe.

Joseph

The above sets the RTC to the time you insert.

But that sets it manually if i uncomment that part. I want to set it automatically and not manually.

Joseph

I’ve always done it that way, never had problems.


I have not used the below method, however, try the same sequence with:

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

That is the compile time. The sketch can't use it later on, after running for a while. This code can only work at upload time.

I'm actually changing it in the wrong. Place. There is a place called loss power and and it was in the wrong place.

It is now working Another Duhh moment for me Sorry about that.

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // 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(2021, 10, 28, 4, 39, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // 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));
}

Joseph

It is now working Another Duhh moment for me Sorry about that. I did it in the wrong place. myfault.

Joseph

Thank you all very much for the help. I don't know what I was thinking.

Joseph