Go Down

Topic: How to read DS3231 Internal temperature sensor, example code (Read 25519 times) previous topic - next topic

casemod

#15
Aug 25, 2014, 09:34 pm Last Edit: Aug 25, 2014, 10:18 pm by casemod Reason: 1

My gripe was with the blog item. The author ( I assume that wasn't you?  :smiley-red:  :D) seems to conclude that the two temperature sensors have comparable accuracy, which is what I take exception with. OTOH, if the DS3231 sensor is a good match for a particular application, then by all means use it!


I understand, just to make clear that by quoting you I was not saying you were wrong or anything.

[Wrong content removed] - DS18B20 is 1 wire not I2C

casemod

#16
Aug 25, 2014, 09:43 pm Last Edit: Aug 25, 2014, 09:47 pm by casemod Reason: 1

Here is some quick code to display the temperature with no floats.  I leave it to you to figure out how to handle the below zero values of temperature.  If you do see them, I'm not sure I would be sitting at your desk.  ;)

Code: [Select]
#include <Wire.h>
#define DS3231_I2C_ADDR             0x68
#define DS3231_TEMPERATURE_MSB      0x11
#define DS3231_TEMPERATURE_LSB      0x12

byte temp_msb;
byte temp_lsb;

void setup()
{
 Serial.begin(9600);
 Wire.begin();
}

void loop()
{
 temp_msb = DS3231_get_MSB();
 temp_lsb = DS3231_get_LSB();

 Serial.print(temp_msb);

 switch(temp_lsb){
 case 0:
   Serial.println(".00");
   break;
 case 1 :
   Serial.println(".25");
   break;
 case 2:
   Serial.println(".50");
   break;
 case 3:
   Serial.println(".75");
   break;
 }


 delay(2000);
}

byte DS3231_get_MSB(){
 Wire.beginTransmission(DS3231_I2C_ADDR);
 Wire.write(DS3231_TEMPERATURE_MSB);
 Wire.endTransmission();

 Wire.requestFrom(DS3231_I2C_ADDR, 1);
 temp_msb = Wire.read();

}

byte DS3231_get_LSB(){

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

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


}




You used two functions as I was thinking, thanks for clarifying!
About negative values, what exactly happens? I know I cant read them, but havent found out the reason why, just a specification "wrong values displayed".

Does it go "negative binary"?

JChristensen

#17
Aug 25, 2014, 09:55 pm Last Edit: Aug 25, 2014, 10:16 pm by Jack Christensen Reason: 1
This will handle temperatures below zero:

Code: [Select]
   const int DS3231_RTC_ADDR = 0x68;
   const int DS3231_TEMP_MSB = 0x11;
   union int16_byte {
       int i;
       byte b[2];
   } rtcTemp;
   
   Wire.beginTransmission(DS3231_RTC_ADDR);
   Wire.write(DS3231_TEMP_MSB);
   Wire.endTransmission();
   Wire.requestFrom(DS3231_RTC_ADDR, 2);
   rtcTemp.b[1] = Wire.read();
   rtcTemp.b[0] = Wire.read();
   long tempC100 = (rtcTemp.i >> 6) * 25;    //degrees celsius times 100
   Serial.print( tempC100 / 100 );
   Serial.print( '.' );
   Serial.println( abs(tempC100 % 100) );

JChristensen


Does it go "negative binary"?


See http://en.wikipedia.org/wiki/Two%27s_complement

casemod



Does it go "negative binary"?


See http://en.wikipedia.org/wiki/Two%27s_complement


All explained on the DS18B20 datasheet. Thanks.

trik125

many thanks to all  :) 
for this usefull posts, finally I can get temperature without using float variables.

Go Up