# Understanding readMagneticField in Arduino LSM9DS1 library

I am discovering Arduino Nano 33 BLE and starting with the magnetometer. I am trying to understand in the readMagneticField function to get magnetic x, y & z datas from LSM9DS1 registers. And my first question is : why the function has this calculation ?

x = data[0] * 4.0 * 100.0 / 32768.0;

extracted from this function : int LSM9DS1Class::readMagneticField in Arduino_LSM9DS1 library

in the function definition, x is supposed to be returned in uT (micro Tesla) and because 1 gauss = 100 uT, i think that it is normal to multiply by 100.  On the multiply by 4.0, i suppose that it is because the defaut value in the CTRL_REG_M register refers to +/- 4 Gauss for the Full-scale selection, but I do not understand why we have to multiply by 4.0. ?
And i do not really understand why there is a division by 32768 ?

Thanks
Eric

1 Like

I was able to understand details : this function returns in data[0] the raw value of the magnetometer (for the x Axis). And this value is in the maximum range [-32768,+32768]. And because the full scale used for the magnetometer is [-4Gauss, +4Gauss], the result in Gauss is data[0] * 4 / 32768. Multiplying it by 100 will give the result in uT (micro Tesla) because 1Gauss = 100 uT.

By reading the LSM9DS1 datasheet, it would be better to use the sensitivity that is (for a +/-4G range) 0.14 milliGauss for 1 unit value (mGauss/LSB).  The code line should be : data[0]*0.00014 to have the result in Gauss (to compare with data[0]*4/32768). But the difference is not so big (because 4/32768 gives 0.000122).

Hope this will help someone.

Eric

1 Like

Hi Eric, you helped me. Thanks!