DS3231 Library problems

Hello,

I have a project using this library.

My test code is

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to 
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231:  SDA pin   -> Arduino Analog 4 or the dedicated SDA pin
//          SCL pin   -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 2 or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the 
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access 
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  rtc.setDOW(THURSDAY);     // Set Day-of-Week to SUNDAY
 rtc.setTime(17, 10, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(22, 11, 2018);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

with all comments removed for readability:

#include <DS3231.h>

DS3231  rtc(SDA, SCL);

void setup()
{
  Serial.begin(115200);
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  rtc.setDOW(THURSDAY);     // Set Day-of-Week to SUNDAY
  rtc.setTime(17, 10, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(22, 11, 2018);   // Set the date to January 1st, 2014
}

void loop()
{
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
  Serial.println(rtc.getTimeStr());
  delay (1000);
}

SCL-SCL
SCA-SCA
GND-GND
VCC-5V

are the connections.

So I used an arduino to set the time and date and then hooked the RTC module up to my project (standalone atmega chip). I later realized that the event I set for a certain time didn't occur. So I hooked the module up to my arduino again and saw that the time was wrong. So I corrected it and put everything back together, to no avail. Checked Vbat = 2.24V, low but not completely empty I think. It's a CR1220.

I then disconnected the RTC module from my arduino (where I set the time to say 17:30), set the time to 17:10 (with no RTC module connected). When I put the RTC back, time was 17:10. Everytime I reset the arduino the time was the one I set. The module didn't seem to keep counting the time, but strangely enough it also didn't really reset.

I then connected a 2nd RTC module, fresh out of the box and the arduino reported the correct date and the last time I set with no module connected.

So it seems I never really programmed the RTC module, I just set a time for the arduino and from there it started counting when I connected it.

I tried the RTClib library and that one seems to work. I will incoorporate that into my project later and see.

Basically my question is: Did I miss something? Is there a misconception on my side somewhere? Or maybe the lib is just not working..

best regards!

Did you re comment lines 11 - 13 after uploading? Like this:

  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(THURSDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(17, 10, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(22, 11, 2018);   // Set the date to January 1st, 2014

Good point! I assume you are right and I should have done this:

upload code with set time code
comment out that portion
upload again

bc otherwise it always reverts back to the set time on boot.

However when I used the RTClib, it has

 if (rtc.lostPower()) {
   Serial.println("RTC lost power, lets set the time!");
   // following line sets the RTC to the date & time this sketch was compiled
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

this portion in it, which i didn't comment out and reuploaded. That did not reset the time when unplugging and plugging back in the arduino. If what you say was the culprit with the first lib, then everytime I reset the arduino with RTClib the time should also go back to the compiled time?

Now I did this:

RTC connected to arduino
upload code with  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
disconnect RTC
upload arduino as isp code
connect atmega
upload my real code (also wit the adjust portion)
put atmega + rtc module where they belong in the real project

I assume this did work because the RTC had its battery removed and inserted before I set the time with the RTClib and afterwards the portion never ran again because the RTC itself did not lose power because of the battery. If that's right I think Í got it now.