adafruit / Adafruit_MLX90393_Library

MIT License
14 stars 18 forks source link

Fix read so that it returns correct +/- values #1

Closed billpugh closed 5 years ago

billpugh commented 5 years ago

The values returned from the MLX90393 are signed 16 bit values assuming TCMP_EN=0, which it is when used with this library. Simply changing the types of xi, yi and zi from uint16_t to int16_t forces the appropriate conversions.

Under the previous version of the code, only non-negative values were returned. For gain 1x, the values returned for x/y values were 0-10,551.

ladyada commented 5 years ago

thanks - how to check, how would we know its working :)

billpugh commented 5 years ago

With the change, running the basicdemo code for the library, without any strong magnets nearby, generates results such as: Starting Adafruit MLX90393 Demo Found a MLX90393 sensor X: -267.9040 uT Y: -69.3910 uT Z: 331.9260 uT

Running with the original library code (using uint16_t) gives: Starting Adafruit MLX90393 Demo Found a MLX90393 sensor X: 10256.0215 uT Y: 10475.7871 uT Z: 353.9760 uT

The first set of numbers is much more likely to be correct.

In the data sheet for the MLX90393, on page 29, section 15.1.10 shows that with TCMP_EN=0 and Res 0 or 1, the magnetic values are provided in 2's complement form.

I haven't done anything to calibrate the sensor and compare the results, but I'm pretty sure that the values read are in 2's complement form, and thus the variables xi, yi and zi should be declared as int16_t.

ladyada commented 5 years ago

ok thanx!