cancel
Showing results for 
Search instead for 
Did you mean: 

How to read temperature data from lis3dh?

LPeng.1
Associate

Refer to lis3dh datasheet, I setting bit 7 and bit6 to 1 in TEMP_CFG_REG, but the data I read from OUT_ADC3_L always 0x40/0x80, OUT_ADC3_H always 0xff. Anything wrong in my config?

Thanks.

3 REPLIES 3
Eleon BORLINI
ST Employee

Hi @LPeng.1​ , please check the C drivers available on Github repository for LIS3DH.

If you are working in Normal mode, the reference function for the temperature data conversion is the following one:

float lis3dh_from_lsb_nm_to_celsius(int16_t lsb)
{
  return ( ( (float)lsb / 64.0f ) / 4.0f ) + 25.0f;
}

if you are working in Low Power mode, the reference function is the following one:

float lis3dh_from_lsb_lp_to_celsius(int16_t lsb)
{
  return ( ( (float)lsb / 256.0f ) * 1.0f ) + 25.0f;
}

So, in your case, supposing you are in normal mode, OUT_ADC3_H + OUT_ADC3_L = 0xFF80 = 0d-128 (two's complement conversion)

Temperature = (-128/64)/4 + 25°C = 24.5°C

Regards

@~Eleon BORLINI Thanks, I have som questions about the temperature data conversion. Refer to datasheet, in normal mode, the adc resolution is 10bit. Is bit1 of OUT_ADC3_H the sign bit? If OUT_ADC3_H+OUT_ADC3_L = 0xC0 = -64,then will temperature be 24.75?As you said about two's complement conversion, 0xC0 inverts by bit then plus 1. What does 64 & 4 mean in ( ( (float)lsb / 64.0f ) / 4.0f ) + 25.0f ? Which part of document can find its description?

Hi @LPeng.1​ , the temperature is calculated on 8bits, both in LP and NM. In LP the dataout is on 8bit, while in NM on 10bit. The (float) cast converts the 16bit binary value into decimal through two's complement conversion, while the division for 256=2^8 or 64 = 2^6 (and 4) shifts the OUT_ADC3_H+OUT_ADC3_L data of the proper bits according to the working mode. Please note that you will always have a number of 6 zeros '0' for your 16bit word's LSBs (e.g. 0xFF80 or 0xFF40). Regards