cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2MDL embedded temperature sensor

Thomas Lotz_2
Associate II

Hello,

I try to use LIS2MDL embedded temperature sensor value, thru TEMP_OUT_L_REG and TEMP_OUT_H_REG. Data is signed 16-bit byte in 2’s complement and 8 LSB / °C. The four most

significant bits contain a copy of the sign bit.

At room temperature I read 0xFE20, so it is negative, so I convert in positive value with 2’s complement : NOT(0xFE20) + 1 = 480 in decimal. Then I div by 8 and I find 60, so -60°C.

What is wrong ?

Thanks

Best Regards

Thomas

2 REPLIES 2
Eleon BORLINI
ST Employee

Hi @Community member​ , I suggest you look at the C drivers for the LIS2MDL device on Github repository. The temperature conversion is shown in the code below:

float_t lis2mdl_from_lsb_to_celsius(int16_t lsb)
{
  return (((float_t)lsb / 8.0f) + 25.0f);
}

Being the sensitivity 8 digit/°C, I believe you have to divide your value by 2^8=256, meaning -480/256 = -1.875°C, and then add 25°C, meaning 23.125°C which could be a room temperature.

Regards

Thomas Lotz_2
Associate II

Hi Eleon, that's perfect thank you !