I have been using the FreeIMU library successfully but now I want to add an external magnetometer that I can mount away from my motors.

I've figured out how to modify the FreeIMU library to use an external magnetometer and I am getting data.

What I can't figure out is what I need to change now that my magnetometer orientation has changed.

On the free IMU it is mounted like this enter image description here

The external compass is mounted like this - upside down - rotated 180° around x enter image description here

I am changing the value inside the

void HMC58X3::getRaw(int16_t *x,int16_t *y,int16_t *z) { *x = cache_x; *y = cache_y; *z = cache_z; } function as all the other code calls this to get the magnetometer data.

So far I have tried:

  1. Changing the sign for the y and z values after I have got them from the magnetometer.

  2. Changing the sign for the y value only

  3. Changing the sign for the z value only

  4. Added 180 to both the y and z values

  5. Subtracting 180 from both the y and z values

  6. Subtracting 180 from y and adding 180 to Z

  7. Adding 180 to y and subtracting 180 from z

  8. Changing nothing

The calibration GUI gives always gives me strange results, and doing the changes above just rotated/mirrored the magnetometer red and green graphs. I am unable to get rid of the key hole shape. enter image description here

The red is XY. The green YZ. The blue is ZX. Does the fact that ZX works mean that my issue is with the Y value?

This is how it looks using the on board magnetometer. enter image description here

What should I try next?

Thanks Joe


I tried rotating the external magnetometer so it is in the same orientation as the FreeIMU magnetometer and I still get the same result so I don't think its the difference in orientation that is causing the problem.

So then I thought maybe its because the FreeIMU is mounted central to the rotation axis and the external magnetometer is mounted about 20cm above. I tested this by rotating only the external magnetometer around itself and I still got the same result.

This is all seems strange, do you think its possible that the external magnetometer I have brought is faulty? Any way to confirm it is working properly on its own?



Managed to get circles plotting by changing the gain from 0 to 1. It seem my new magnetometer was being saturated.

Now I just need to work out how to change my values around so the orientation is correct.


1 Answer 1


OK so I managed to solve this by doing a couple of things.

  1. Connect external magnetometer to the same I2C bus as the FreeIMU.

  2. The FreeIMU is 5V and the external magnetometer is 3.3V so I had to use a level shifter to place both devices on the I2C bus.

  3. Change this line "accgyro->setI2CBypassEnabled(1);" to "accgyro->setI2CBypassEnabled(0);" to enable the external magnetometer

  4. Change this line "magn->calibrate(0, 8);" to magn->calibrate(1, 8);" to increase the gain and remove saturation (this is what caused the strange "Keyhole" shape on the graph.

  5. Calibrate the sensors.

  6. Change this line

"AHRSupdate(val[3] * M_PI/180.0, val[4] * M_PI/180.0, val[5] * M_PI/180.0, val[0], val[1], val[2], val[6], val[7], val[8], magn_valid);"


"AHRSupdate(val[3] * M_PI/180.0, val[4] * M_PI/180.0, val[5] * M_PI/180.0, val[0], val[1], val[2], val[6], -val[7], -val[8], magn_valid);"

to account for the change in orientation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.