Magnetometer Calibration – Calculating Orientation pt3

By | March 13, 2016

Magnetometer Calibration

Calibrating magnetometers is critically important. Without a good mag cal, you’ll get rubbish out of your magnetometers. Fortunately it isn’t that hard to do a basic calibration.

If you were to create a scatter plot of your magnetometer readings, it would probably show both hard iron and soft iron errors. An ideal plot is circular and is centred on the chart. Hard iron errors cause the measurements to be off centre. Soft iron errors cause the shape to be elliptical rather than circular.

To remove these errors, you need to find the minimum and maximum points for each axis of the sensor. This means gathering data. Lots of data. So get your magnetometer and move it around until you’ve got a few thousand samples in as many different orientations as possible.

Hard iron errors are the simplest to remove. Find the maximum and minimum measurements for each axis and average them. This gives the amount of offset for each axis. Every time you take a reading from your magnetometers, you should subtract this offset from each axis.

Soft iron errors are slightly harder to remove. There are some more advanced techniques that involve calculating rotation matrices and bla bla, but it’s computationally expensive (for my brain) and simply scaling each axis to remove the elliptical shape works quite well too.

First the hard iron errors are removed from the maximums and minimum magnetometer vectors. These minimum and maximum vectors are the same as the ones being used to correct for hard iron errors.

The average distance from the centre is now calculated. We want to know how far from the centre, so the negative values are inverted.

The components are now averaged out

Finally calculate the scale factor by dividing average radius by average value for that axis.

With these scale values we can correct for soft iron errors by multiplying them with the relevant magnetometer axis reading. Here’s an example

It doesn’t use any matrix maths and the magnetometers are now quite accurate. In fact, using a map I can’t see any errors. Without any proper test equipment, I’d guess that it’s good to +/- 2 degrees.

Continue on to part 4 …

2 thoughts on “Magnetometer Calibration – Calculating Orientation pt3

  1. Pingback: IMU Maths – How To Calculate Orientation pt2 - CamelSoftware

  2. Liz Walsh

    Hi Sam,
    I’d like to discuss this code with you, can you email me directly?
    Kind regards,


Leave a Reply

Your email address will not be published. Required fields are marked *