2

I have a sensor named LSM303DLHC ,it have 2 temp register but I can't figure it out how to convert it to degrees Celsius. 2 Reg is:

    TEMP_OUT_H_M register  // high reg

TEMP11 | TEMP10 | TEMP9 | TEMP8 | TEMP7 | TEMP6 | TEMP5 | TEMP4

     TEMP_OUT_L_M register  //low reg

TEMP3 | TEMP2 | TEMP1 | TEMP0 | 0 | 0 | 0 | 0

In datasheet say: "TEMP[11:0] Temperature data (8 LSB/deg - 12-bit resolution)" My current code is

uint8_t hig_reg = read(TEMP_OUT_H_M)  // value = 0x03
uint8_t low_reg = read(TEMP_OUT_L_M)  // value = 0x40
int16_t temp = ((uint16_t)hig_reg  << 8) | (uint16_t)low_reg;  // temp = 0x0340 = 832

float mTemp = temp/256;  // = 3.25 
mTemp = mTemp +20  ;      // =23.25 (°C)  i add 20 more

But I don't understand where the 20 °C offset comes from? Datasheet never mentions it.

Community
  • 1
  • 1
dante
  • 984
  • 3
  • 10
  • 24
  • here is Datasheet: http://www.st.com/content/ccc/resource/technical/document/datasheet/56/ec/ac/de/28/21/4d/48/DM00027543.pdf/files/DM00027543.pdf/jcr:content/translations/en.DM00027543.pdf – dante Mar 25 '17 at 15:13
  • 1
    One extra implementation detail--The datasheet mentions that the values are reported in [two's complements](https://en.wikipedia.org/wiki/Two's_complement), so the most significant bit tells you whether it is negative (1) or positive (0). – Chris Combs Mar 25 '17 at 15:40
  • 2
    Also see : http://electronics.stackexchange.com/questions/219032/how-to-determine-temperature-with-lsm303dlhc – Arnaud F. Mar 25 '17 at 16:32
  • But my value still < 2^11 (2048) .so it still positive value and i dont need to change it. My question is what is the right formula ? – dante Mar 26 '17 at 01:34
  • 1
    Can you measure the room temperature with an other device? So do you know what output you should exactly have? – Bence Kaulics Mar 26 '17 at 10:10

3 Answers3

2

Thank for your answer. Turn out that temperature sensor just determine comparative temperature to calculate the variation. It not use for absolute temperature.They should add that information in datasheet. I just waste 2 day of my life for that.

dante
  • 984
  • 3
  • 10
  • 24
  • Sorry for tour loss :-(. I've read about 20 articles all are discussing about GPS and just afew from temp ... – Arnaud F. Mar 26 '17 at 19:12
1

My try... First, I have note that you are taking the whole 8 bit TEMP_OUT_L_M register and as you described is just the first 4 bits of it. Then try to make the 12 bit register first. I use python ans SMBus library,

temph = i2cbus.read_byte_data(i2caddress, TEMP_OUT_H_M) << 4 
templ = i2cbus.read_byte_data(i2caddress, TEMP_OUT_L_M) >> 4
tempread = temph + templ   # it is all ready converted to Decimal

Then you can go ahead with the transformation: see page 11 title 2.2 "Temperature sensor characteristics: 8 LSB/ºC , 12 bit resolution and 2.5 Vdd."

Then it is clear that:

ºC = (read_value * VDD * 10^(log 2 (LSB/ºC)) / ((resolution - 1) * (10*(ºC/LSB))

In the LSM303 then following the python code:

# temperature = (tempread * 2.5 * 1000)/(2^12-1) * (10/8))  better to write:
temperature = (tempread *2500)/(4095 * 1.25)

In your case: you have read: 0x0340, in 12 bits 0x34 in decimal: 54

temperature = (54 * 2500) / (4095 * 1.25) = 23.443223

I also noticed that:

  • The maximum secure register to read is 85ºC = 0x55, so we better make the register with the 4 bit LSB of TEMP_OUT_H_M and 4 bit MSB of TEMP_OUT_L_M.
  • In further test the LSM303 can resist near 125 ºC for a while, with out permanent damage, but it a good practice to use this temperature to put the Magnetometer and Accelerometer in a sleep mode. When the temperature reaches 80.
Syscall
  • 19,327
  • 10
  • 37
  • 52
apolo
  • 11
  • 3
0

My opinion is that TEMP is on 10 bits and one for the sign (value max you can read : 0x3FF), so:

0x03FF - 0x0340 = 0x0BF

0x0BF / 8 = 0x17 (23.875 in decimal).

As said, don't forget the two's complement in your computation.

Community
  • 1
  • 1
Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
  • i dont think your formula is right. 0x0340 (823) still < 2^11 (2048) . so dont need to use '-'. Beside when i touch the sensor , value increase to 0x05e0. – dante Mar 26 '17 at 01:30
  • 1
    You can eventually check : https://github.com/embeddedadventures/BME280/blob/master/BME280_MOD-1022.cpp – Arnaud F. Mar 26 '17 at 09:22
  • And this thread : https://forum.pololu.com/t/16-bit-values-in-lsm303/8499 – Arnaud F. Mar 26 '17 at 09:22
  • The whole data is 12 bits itself so 1+10 bit seems 1 bit short. The `0x340` actually should be `0x34` the last 4 bit are not part of the temperature data. – Bence Kaulics Mar 27 '17 at 06:51