DS3231 - Reading Temperature

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dcaroth552
 
Posts: 4
Joined: Mon Jan 15, 2018 9:36 am

DS3231 - Reading Temperature

Post by dcaroth552 »

The Arduino library for the DS3231 doesn't seem to support negative temperatures.
Can anyone help with this?
Last edited by dcaroth552 on Mon Jan 15, 2018 11:05 am, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DS3231

Post by adafruit_support_bill »

Which library are you using? Adafruit's RTCLib does not support reading the temperature registers of the DS3231.

User avatar
dcaroth552
 
Posts: 4
Joined: Mon Jan 15, 2018 9:36 am

Re: DS3231

Post by dcaroth552 »

I'm using DS3231.h

The following line of code reads the temperature (Celsius) from the DS3231:

Temp = (rtc.getTemp());

But a negative temperature (say -5 degrees C) reads back 251, not -5.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DS3231

Post by adafruit_support_bill »

I'm using DS3231.h
That is not an Adafruit library. We support the DS3231 via our RTCLib library: https://github.com/adafruit/RTClib

There are many DS3231-specific libraries out there. You should contact the author of the one you are using.

User avatar
dcaroth552
 
Posts: 4
Joined: Mon Jan 15, 2018 9:36 am

Re: DS3231

Post by dcaroth552 »

Below is the actual code I’m using to retrieve the temperature from the DS3231:

Code: Select all

void Display_Temperature(){
  int Deg = 0;
  Display.clear();
  Display.writeDisplay();
  if(digitalRead(Temp_Switch) == HIGH){
    Deg = 15; // display F
    Temp = (rtc.getTemp() * 1.8 + 32);
  }
  else{
    Deg = 12; //display C
    Temp = (rtc.getTemp());
  }
  Temp = Temp * 10;
  if(Temp == 0){
    Display.writeDigitNum(3,0);
  }
  else{
    Display.print(Temp);
  }
  Display.writeDigitNum(4,Deg);
  Display.writeDisplay();
}
This works fine so long as the temperature is a positive number.
I thought the library checked register 11h for a negative value (MSB) but it appears that it's not doing it.

The following is the code from the library:

Code: Select all

float DS3231_get_treg()
{
    float rv;
    uint8_t temp_msb, temp_lsb;
    int8_t nint;

    Wire.beginTransmission(DS3231_I2C_ADDR);
    Wire.write(DS3231_TEMPERATURE_ADDR);
    Wire.endTransmission();

    Wire.requestFrom(DS3231_I2C_ADDR, 2);
    temp_msb = Wire.read();
    temp_lsb = Wire.read() >> 6;

    if ((temp_msb & 0x80) != 0)
        nint = temp_msb | ~((1 << 8) - 1);      // if negative get two's complement
    else
        nint = temp_msb;

    rv = 0.25 * temp_lsb + nint;

    return rv;
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: DS3231 - Reading Temperature

Post by adafruit_support_bill »

It is not clear what the author was attempting with that operation. Looks to me like it would always evaluate to 0xFF.

User avatar
dcaroth552
 
Posts: 4
Joined: Mon Jan 15, 2018 9:36 am

Re: DS3231 - Reading Temperature

Post by dcaroth552 »

Well I guess I write some code when time permits to by-pass the library for reading the temperature register.
If I find a solution I'll post it here for knowledge base.

Thanks

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”