Hot swap RTC DS3231 ?

I want to insert/remove DS3231 module while arduino is running but code stops executing at the line: Serial.println(rtc.getTimeStr()); I tried it in the if statement:

if(rtc.getTimeStr()){
  Serial.println(rtc.getTimeStr());
  }

but this also stops the loop

I am using DS3231 library from rinkydinkelectronics

I have only noticed a problem when the clock module is currently being read or written to when it is swapped out and it is an I2C bus problem.

I have one application, a time switch, with 2 clock modules (which check each other and signal any mutual time drift). Since they have the same I2C address, I have connected them together over a 4052 demultiplexer and I ensure that they are not being read from during the swap. Depending on what you are doing, maybe that could help .

I want to insert/remove DS3231 module while arduino is running

You might as well take a hammer to the RTC and the Arduino now. Removing hardware from a running system is a really bad idea. Why would you want to do that?

but this also stops the loop

What stopped the loop was NOT printing the value that the function returned. It was calling the function, to get data from a piece of equipment that you removed. If you insist on mistreating the Arduino and removing hardware, you have to stop reading from that hardware while it is missing in action. That really should be obvious.

rtc is the software representation of your Real time clock component. So if you remove it (really why would you do that!) then don't send messages to the object. It's probably not coded to gracefully handle that.

You talk to the component through the I2C interface. so query the interface to see if you object is still there.

See this example for scanning the bus. Adapt that to just check for address 0x68 ( If I remember correctly that's the one for your RTC)

Swapping is not essential in my project. I just want my code to continue running in case RTC module stops working or is removed. Basically this:

if(rtc.getTimeStr()){
Serial.println(rtc.getTimeStr());
}else{
Serial.println("Time not available");
}

In other words, is there a way to check the RTC module is present before calling function rtc.getTimeStr()?

You could scan the I2C bus to check if a DS3231 (address: 0x68) is present before doing anything with it.

Hi,

I actually do this with one of my programs. If the RTC does not respond then i simply do not read the time from it. I wanted it that way so that if the RTC went bad for some reason the program would still run, and switch to using millis() instead.

Not sure if i am using the same lib as you but if you still have not got it working i'll look up the code and library.

I figured this out:

Wire.begin();
byte i = 87;
 Wire.beginTransmission (i);
   if (Wire.endTransmission () == 0) //we got response
     {
Serial.println(rtc.getTimeStr());
     // end of good response
     }else{ //RTC not responding
      Serial.println ("Time not available ");
      }

Now loop continues even if RTC is removed

Congrats!

(You don't need i by the way, just code

#define RTCADDRESS xxx // the right address, yours seem weird
....

 Wire.beginTransmission (RTCADDRESS);
   if (Wire.endTransmission () == 0) //we got response

Which variant of the ds3231 has an I2C address (in decimal) of 87 ?

topitele: I figured this out:

Wire.begin();
byte i = 87;
 Wire.beginTransmission (i);
   if (Wire.endTransmission () == 0) //we got response
     {
Serial.println(rtc.getTimeStr());
     // end of good response
     }else{ //RTC not responding
      Serial.println ("Time not available ");
      }

Now loop continues even if RTC is removed

i2c Addres decimal 87 is 0x57. This is the address of the eeprom mounted on the module. The address of the ds3231 is indeed 0x68.