HTS221 Temperature sensor
Hello,
I have one Arduino MKR ENV board which has this sensor. The problem is that I'm reading higher temperature values than expected.
The routines that I use are :
For calibration:
/**
* HTS221_readCalibration
*
* Reads calibration values
* @return char - != 0 if any error happened
*/
char HTS221::readCalibration(void) {
unsigned char T0_degC_x8_H;
unsigned char T0_degC_x8_L;
unsigned char T1_degC_x8_H;
unsigned char T1_degC_x8_L;
unsigned char H0_rh_x2;
unsigned char H1_rh_x2;
int temp;
DEBUG("HTS221 - Reading Calibration: ");
DEBUG("\n\r");
/**
* Humidity calibration values
*/
if((temp = readReg(HTS221_H0_RH_X2)) < 0) return 1;
H0_rh_x2 = (unsigned char)temp;
H0_rh = H0_rh_x2 / 2.0;
DEBUG("HTS221 - H0_rh: ");
DEBUGF(H0_rh, 2);
DEBUG("\n\r");
if((temp = readReg(HTS221_H1_RH_X2)) < 0) return 2;
H1_rh_x2 = (unsigned char)temp;
H1_rh = H1_rh_x2 / 2.0;
DEBUG("HTS221 - H1_rh: ");
DEBUGF(H1_rh, 2);
DEBUG("\n\r");
if((temp = readReg(HTS221_H0_T0_OUT_L)) < 0) return 3;
H0_T0_out = (unsigned char)temp;
if((temp = readReg(HTS221_H0_T0_OUT_H)) < 0) return 4;
H0_T0_out |= ((unsigned char)temp << 8);
DEBUG("HTS221 - H0_T0_out: ");
DEBUGF(H0_T0_out, DEC);
DEBUG("\n\r");
if((temp = readReg(HTS221_H1_T0_OUT_L)) < 0) return 5;
H1_T0_out = (unsigned char)temp;
if((temp = readReg(HTS221_H1_T0_OUT_H)) < 0) return 6;
H1_T0_out |= ((unsigned char)temp << 8);
DEBUG("HTS221 - H1_T0_out: ");
DEBUGF(H1_T0_out, DEC);
DEBUG("\n\r");
/**
* Temperature calibration values
*/
if((temp = readReg(HTS221_T1T0_MSB)) < 0) return 7;
DEBUG("HTS221_T1T0_MSB - 0x35: 0x");
DEBUGF(temp, HEX); DEBUG(" decimal="); DEBUGF(temp, DEC);
DEBUG("\n\r");
T0_degC_x8_H = (unsigned char)(temp & 0x03);
if((temp = readReg(HTS221_T0_DEGC_X8)) < 0) return 8;
T0_degC_x8_L = (unsigned char)temp;
DEBUG("HTS221_T0_DEGC_X8 - 0x32: 0x");
DEBUGF(T0_degC_x8_L, HEX); DEBUG(" decimal="); DEBUGF(T0_degC_x8_L, DEC);
DEBUG("\n\r");
T0_degC = (((T0_degC_x8_H << 8) | T0_degC_x8_L)) / 8.0;
DEBUG("HTS221 - T0_degC: ");
DEBUGF(T0_degC, 2);
DEBUG("\n\r");
if((temp = readReg(HTS221_T1T0_MSB)) < 0) return 9;
T1_degC_x8_H = (unsigned char)((temp & 0x0C) >> 2);
if((temp = readReg(HTS221_T1_DEGC_X8)) < 0) return 10;
T1_degC_x8_L = (unsigned char)temp;
DEBUG("HTS221_T1_DEGC_X8 - 0x33: 0x");
DEBUGF(T1_degC_x8_L, HEX); DEBUG(" decimal="); DEBUGF(T1_degC_x8_L, DEC);
DEBUG("\n\r");
T1_degC = ((T1_degC_x8_H << 8) | T1_degC_x8_L) / 8.0;
DEBUG("HTS221 - T1_degC: ");
DEBUGF(T1_degC, 2);
DEBUG("\n\r");
if((temp = readReg(HTS221_T0_OUT_L)) < 0) return 11;
T0_out = (unsigned char)temp;
DEBUG("HTS221_T0_OUT_L - 0x3C: 0x");
DEBUGF(T0_out, HEX); DEBUG(" decimal="); DEBUGF(T0_out, DEC);
DEBUG("\n\r");
if((temp = readReg(HTS221_T0_OUT_H)) < 0) return 12;
DEBUG("HTS221_T0_OUT_H - 0x3D: 0x");
DEBUGF(temp, HEX); DEBUG(" decimal="); DEBUGF(temp, DEC);
DEBUG("\n\r");
T0_out |= ((unsigned char)temp << 8);
DEBUG("HTS221 - T0_out: ");
DEBUGF(T0_out, DEC);
DEBUG("\n\r");
if((temp = readReg(HTS221_T1_OUT_L)) < 0) return 13;
T1_out = (unsigned char)temp;
DEBUG("HTS221_T1_OUT_L - 0x3E: 0x");
DEBUGF(T1_out, HEX); DEBUG(" decimal="); DEBUGF(T1_out, DEC);
DEBUG("\n\r");
if((temp = readReg(HTS221_T1_OUT_H)) < 0) return 14;
DEBUG("HTS221_T1_OUT_H - 0x3F: 0x");
DEBUGF(temp, HEX); DEBUG(" decimal="); DEBUGF(temp, DEC);
DEBUG("\n\r");
T1_out |= ((unsigned char)temp << 8);
DEBUG("HTS221 - T1_out: ");
DEBUGF(T1_out, DEC);
DEBUG("\n\r");
return 0;
}For reading the values
float HTS221::readTemperature(void) {
unsigned char temp;
int value;
float result;
value = readReg(HTS221_TEMP_OUT_H);
temp = (unsigned char)readReg(HTS221_TEMP_OUT_L);
value = (value << 8) | temp;
DEBUG("HTS221 - T_OUT: ");
DEBUGF(value, DEC);
DEBUG("\n\r");
result = (((float)value - T0_out)/(T1_out - T0_out)) * (T1_degC - T0_degC) + T0_degC;
DEBUG("HTS221 - Temperature value readed: ");
DEBUGF(result, 2);
DEBUG("\n\r");
return result;
}The registers' values are:
HTS221_T1T0_MSB - 0x35: 0xC4 decimal=196
HTS221_T0_DEGC_X8 - 0x32: 0xA7 decimal=167
HTS221 - T0_degC: 20.87
HTS221_T1_DEGC_X8 - 0x33: 0x14 decimal=20
HTS221 - T1_degC: 34.50
HTS221_T0_OUT_L - 0x3C: 0x3 decimal=3
HTS221_T0_OUT_H - 0x3D: 0x0 decimal=0
HTS221 - T0_out: 3
HTS221_T1_OUT_L - 0x3E: 0xEB decimal=235
HTS221_T1_OUT_H - 0x3F: 0x2 decimal=2
HTS221 - T1_out: 747
HTS221 - T_OUT: 354
HTS221 - Temperature value readed: 27.30
Is it problem of a faulty device?
Thanks in advance
Jordi Mercader
