I'm using the STM32 Blackpill board. The library is adapted from the Adafruit Library.
The I2C address is 0x18 for the adafruit board I'm using for this test.
The X-Axis data seems to match the Arduino Values.
However, it's not the same for the Y and Z axes.
Below is the data from Arduino:
-3333.000,2152.500,6834.806
-3335.700,2158.800,6819.076
-3333.600,2153.700,6842.550
-3330.900,2162.100,6828.272
-3335.100,2152.200,6823.190
-3327.300,2154.300,6819.802
-3332.700,2159.100,6822.706
-3334.500,2158.800,6815.930
-3338.700,2160.000,6804.072
-3336.000,2151.600,6810.848
-3331.800,2153.400,6812.058
-3333.600,2156.700,6823.190
-3330.300,2156.400,6823.190
-3335.400,2156.700,6816.414
-3330.300,2154.600,6824.158
-3326.400,2150.100,6833.838
-3328.200,2147.400,6825.852
-3329.400,2152.200,6828.998
-3336.000,2160.300,6815.930
-3330.900,2158.800,6835.290
-3334.500,2157.000,6817.624
Below is the Data from STM32:
-3367.800, 72.000, 1053.184
-3368.400, 72.000, 1053.184
-3360.600, 72.000, 1053.184
-3364.500, 72.000, 1053.184
-3368.400, 72.000, 1053.184
-3363.600, 72.000, 1053.184
-3362.100, 72.000, 1053.184
-3369.900, 72.000, 1053.184
-3366.000, 72.000, 1053.184
-3363.600, 72.000, 1053.184
-3362.700, 72.000, 1053.184
-3362.100, 72.000, 1053.184
-3369.900, 72.000, 1053.184
-3368.400, 72.000, 1053.184
-3366.300, 72.000, 1053.184
-3369.300, 72.000, 1053.184
-3363.900, 72.000, 1053.184
-3367.200, 72.000, 1053.184
-3369.600, 72.000, 1053.184
-3368.100, 72.000, 1053.184
-3369.600, 72.000, 1053.184
The HAL_I2C code I adapted is as follows:
uint8_t dataX[3];
int16_t xi, yi, zi;
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x30 | 0x02, 1, dataX, 1,
HAL_MAX_DELAY);
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x40 | 0x02, 1, dataX, 3,
HAL_MAX_DELAY);
xi = dataX[2] | (dataX[1] << 8);
uint8_t dataY[3];
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x30 | 0x04, 1, dataY, 1,
HAL_MAX_DELAY);
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x40 | 0x04, 1, dataY, 3,
HAL_MAX_DELAY);
yi = dataY[2] | (dataY[1] << 8);
uint8_t dataZ[3];
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x30 | 0x08, 1, dataZ, 1,
HAL_MAX_DELAY);
HAL_I2C_Mem_Read(&hi2c1, 0x18 << 1, 0x40 | 0x08, 1, dataZ, 3,
HAL_MAX_DELAY);
zi = dataZ[2] | (dataZ[1] << 8);
Later, I scale it according to the gain and resolution:
I choose 1X gain and 16bit for Z-axis, 17bit for X&Y-Axis:
sprintf(msg, "%.3f, %.3f, %.3f\r\n", xi * 0.300, yi * 0.300, zi * 0.242);
What could be wrong here?
I doubt the register that I'm reading for magnetometer values.
NOTE: I did not move the sensor while reading values for Arduino and STM32 boards.
Thanks.