DS3231 (chronodot) reporting bogus temperature

My DS3231 (chronodot) consistently reports time, but after running for 10-12 hours, it starts reporting a bogus temperature.

On cycling the power, it returns to a normal state.

I know the temperature is bogus because I have 3 other temperature sensors in the cabinet, plus cycling the power resets the temperature.

Has anyone seen this behavior, or have any hints on troubleshooting?

Here is a sample data set:
date time clocktemp LM35_temp
12/27/10 02:39:53 32.00 32.00
12/27/10 02:41:59 32.00 32.00
12/27/10 02:46:45 32.00 32.00
12/27/10 02:48:52 261.95 32.00
12/27/10 02:50:58 261.95 32.00

And here is the temperature reading function I'm using (off the internet)

float get3231Temp()
{
  //temp registers (11h-12h) get updated automatically every 64s
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.send(0x11);
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 2);
  
  if(Wire.available()) {
    tMSB = Wire.receive(); //2's complement int portion
    tLSB = Wire.receive(); //fraction portion
    
    temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
    temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8
  }
  else {
    //oh noes, no data!
  }
  
  return temp3231;
}
temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8

What is the type of tMSB and tLSB? - assume they are byte, why not declared local?

The math seems correct to me - although you drop the sign bit, but that is a choice - the value of the var temp3231 cannot be above 127.75.
BUT you should give tmp3231 a value - e.g. 0.0 - in the else path. If the I2C communication drops somehow this is not noted and tmp3231 will get a random value depending on what's on the stack that moment.

SO two possible causes:

  • tampering of the value outside the function
  • I2C not returning a value.

Please share the whole code, as the problem might be outside the function.

Thank you for your advice.

I followed it, returning '999' when there is a problem reading the clock temperature. I also decreased the scope of 2 variables used in reading the clock temperature so that they are no longer global.

I cannot see why, but these 2 changes which you suggested have, prevented my bug from re-occuring.

The bug used to occur every 24 hours. Now it has been running 96 hours without any problem.

Thanks again.

OK, 4 days in a row is a real improvement. Can you post your final code? Instructive for people finding this thread :slight_smile:

I cannot see why, but these 2 changes which you suggested have, prevented my bug from re-occuring.

Also easier to explain when you post your final code.

For those who may be following this thread for a solution to strange DS3231 temperature output below freezing, see this forum link for the solution (thanks to Coding Badly).

Sorry - link to forum thread:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294869573/0