# Magnetometer Calibration

I'm looking at using a magnetometer in a project and I'm struggling to understand the proper procedure for calibration of the sensor.

I understand that magnetometers need to be calibrated to compensate for hard and soft iron effects.

I've seen a number of algorithms for calibration based around ellipsoid fitting, similar to that detailed in https://www.st.com/content/ccc/resource/technical/document/design_tip/group0/a2/98/f5/d4/9c/48/4a/d1/DM00286302/files/DM00286302.pdf/jcr:content/translations/en.DM00286302.pdf. These algorithms all require the magnetometer be spun in three dimensions, to collect enough data for an ellipsoid to be fitted, thereby allowing estimation of scaling and offset parameters to translate and scale the ellipsoid to a unit sphere positioned at the origin.

However, I'm unclear on how the calibration algorithm should be used, for example if say I'm placing my magnetometer in the boot of a car next to several other bits of metal would I be able to calibrate the magnetometer by spinning the magnetometer in three dimensions just above its mounting point? Would this then calibrate out the effect of the hard and soft iron effects in the magnetometers vicinity? Or should I actually be mounting the magnetometer in its mounting point and then spinning the entire platform (in this example the car) in three dimensions? Could someone also explain the physics as to why the calibration should be performed in either manner?

• Did you ever find the answer to your question? I'm wondering the same thing for a project of mine. Nov 13, 2021 at 19:36

Interesting that this question has not been answered since it was asked almost 5 years ago. Let me propose an answer and curate it as others comment on it.

A magnetometer is designed to detect the earth's magnetic field and if perfectly aligned (all sensors are orthogonal from one another but otherwise act exactly alike) and no near by metals perturb the earth's magnetic field, we can simply sample the magnetometer. And if we know it orientation in the earth's frame of reference, derive magnetic north through calculations.

Of course nothing is perfect. The magnetometer's X, Y & Z sensors are not exactly alike and there are likely near by metals that perturb the earths magnetic field.

Simply put, a magnetometer should be calibrated as it is expected to be used. If you are going to use it in a car then you need to calibrate while securely mounted in the car. In the same place in the car. Preferably as far away from metal objects as possible. There is a reason most car (and airplane) compasses are located in the middle of the front windshield. Finally, if you do not expect the car to flip over, then you do not expect the magnetometer to have to work under such circumstances.

Hard Iron corrects for individual sensor offset differences. This offset calibration value is added to a sensor sample such that its absolute maximum and minimum samples are equal. Point each sensor directly at and directly away from magnetic north to obtain these readings. Do not forget magnetic declination and inclination when determining where magnetic north is at your location on earth.

Soft Iron correct for individual sensor sensitivity differences. Each sample is multiplied by its normalization value such that all sensors report the same value while that sensor is in the same orientation with respect to magnetic north.

At this point, we have described 3 offset values and 3 normalization values. Or a 1x3 offset matrix and a 1x3 normalization matrix. For a sensor that needs no adjustment these matrices would look like this:

offset = [0, 0, 0] normalization = [1, 1, 1]

Many applications can use this information to derive a suitable magnetic north vector. However, some may want to use a 3x3 normalization matrix. The extra 6 values in this 9 value normalization matrix takes into account the non perfect orthogonal arrangement of the 3 magnetometer sensors. Simply put, how much of sensor X and Y readings can contribute to sensor Z's reading. And X and Z to Y's. And Z and Y to X's. The 1x3 and 3x3 matrices for a perfect magnetometer would look like this:

offset = [0, 0, 0] normalization = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

Finding the missing calibration values when going from a 1x3 to a 3x3 normalization calibration matrix is not trivial.

We might be able to go further if there is interest in a more complete answer. Leave comments and I'll adjust this answer to better fit where people want to take this stackexchange question.