# MPU-9255 with AK8963C looking for data comparison

#### misha666

Joined Feb 6, 2020
3
" Hi Guys,
firstly I want to say Hello to everybody and thanks for adding me to the group
I am working on electronic compass project, my HW are: Arduino Uno, MPU-9255, and 1.8 inch LCD module from Waveshare.
MPU is conected by I2C, LCD by HW_SPI. Everything is working fine but...
My general problem is with raw data readings from magnetometer AK8963C. It connects, reads and show me results but this data are strange. Range of raw data should be from -32760 to 32760. And in some cases readings are almost good in some not.
1. Q
Is my byte shifting for mx, my, mz(those variables are ints) good? -> int16_t mx = ((xl << 8) | xh );
Because in this kind of shiftings where low byte goes to high position in int values of mx/my/mz are in range..almost.
If I change it to int16_t mx = ((xh << 8) | xl ); which most of libraries on github have, the values of mx are not in range they are from -150 to +31 with two times 0. And this is strange.
For checking why I barely read values from xl/yl/zl and xh/yh/zh and those values were:
xl, yl, yz - range from 0 to 255 like should have unsigned 8 bit int, while
xh- has only two values 0 or 255, yh- 0 or 1 and zh - 0 or 255 and what I am thinking, they only change the sign of value, especially that it happens when rotating for about 180 degrees in every axis.
2. Q
Can anybody check those values in his/hers sensor and we can compare? Maybe my MPU is broken or something else. Maybe I am doing something wrong? If You need more code please tell. I am attaching sensor datasheet. All other sensors are switched off(gyroscope and accelerometr).

Here is a sample of code from my library which is change fragment from somebodys lib. I know that it is not very good but firstly i want to get good readings from registers than make good code. "

Code:
C:
uint8_t MPU9255::read_mag()
{
dor_status = drdy_dor;
if(drdy_dor==1 || drdy_dor==3)
{
hofl_status = hofl; //overflow status
if(hofl!=1)
{
int16_t mx = ((xl << 8) | xh );
int16_t my = ((yl << 8) | yh );
int16_t mz = ((zl << 8) | zh );
return 0;
} return 1;
} return 2;

}

#### Attachments

• 649.7 KB Views: 1

#### mckenney

Joined Nov 10, 2018
70
If I change it to int16_t mx = ((xh << 8) | xl );
This is the form you want. xh and xl should be unsigned, else sign propagation will make a mess. (Your library code uses "byte", which is traditionally unsigned.)
xh- has only two values 0 or 255
A signed value which is jittering between -1 and 1 will show bit (unsigned) values alternating 0xFFFF to 0x0001, so don't read too much into the bit representations.

#### misha666

Joined Feb 6, 2020
3
A signed value which is jittering between -1 and 1 will show bit (unsigned) values alternating 0xFFFF to 0x0001, so don't read too much into the bit representations.
but please look into the attachment where is a part of datasheet. Values in mx/my/mz should be in range from -32760 to 32760 after converting, two onebyte of data read from register, to one 16 bit variable(for example mx). My readings from registers after converting with this scheme int16_t mx = ((xh << 8) | xl ) are in range from ca. -150 to 31, and those values are not even good. I have checked my readings using 3 different libraries and all the time they were almost the same. So again if Anybody could check Yours readings and tell how it looks like I will appreciate. I am considering if maybe the sensor is broken....but when Ive done self control test it has good readings....

#### Attachments

• 128.9 KB Views: 2

#### mckenney

Joined Nov 10, 2018
70
How do you know that [-150,+31] is incorrect? I think 1.5 Gauss is in the right order of magnitude for the Earth's magnetic field. Did you try holding a magnet nearby?

I don't have your equipment, so I have to defer to others there.