Showing results for 
Search instead for 
Did you mean: 

How to get mG from LSM303DLHC

Bi ribu
Posted on November 13, 2017 at 19:49

I have developed a very simple driver for this sensor. I have a stm32f411ve discovery board which has one included.

This is my configuration:

PM_LSM303DLHC_config.ctrl_reg1_a = 0x27;

PM_LSM303DLHC_config.ctrl_reg2_a = 0x00;

PM_LSM303DLHC_config.ctrl_reg3_a = 0x00;

PM_LSM303DLHC_config.ctrl_reg4_a = 0x00;

PM_LSM303DLHC_config.ctrl_reg5_a = 0x00;

PM_LSM303DLHC_config.ctrl_reg6_a = 0x00;

As you see, the only parameter I set is reg1_a. I have tested also with 0x57.

This is how I parse the output:

x = (((PM_LSM303DLHC_values.out_x_h_a << 😎 | (PM_LSM303DLHC_values.out_x_l_a)));

y = (((PM_LSM303DLHC_values.out_y_h_a << 😎 | (PM_LSM303DLHC_values.out_y_l_a)));

z = (((PM_LSM303DLHC_values.out_z_h_a << 😎 | (PM_LSM303DLHC_values.out_z_l_a)));

Where x,y and z are int32_t.

I get this kind of values: 64000 60480 16128 while my board is over a table. What ind of values should I see then? 

How can I get mG from this information? I haven't seen anything on the datasheet.

Miroslav BATEK
ST Employee
Posted on November 14, 2017 at 09:57

The output value is expressed in two’s complement, it is 12-bit value left adjusted in the 16-bit output register.

You have to convert the two bytes (high and low) into signed number. I think simple solution is change the type of PM_LSM303DLHC_values.out_x_h_a from unsigned to signed value. Then you have to divide the value by 16. This gives you number of LSB which you can convert into mg using sensitivity value, which you can find in Table 3. in the datasheet.

For example:

64000 (unsigned) ... -1536 (signed) ... -96 LSB .... -96 mg

60480 (unsigned) 


-5056 (signed) ... -316 LSB .... -316 mg (isn't it typo? I would expect lower value close to 0)

16128 (unsigned) 



(signed) ... 1008 LSB .... 1008 mg