# Algorithms used to calibrate electronic compass (magnetometer) ?

• posted

Hi,

Modern smartphones etc come with built in magnetometers alowing them to be used as magnetic compasses. The apps for these often have calibration rout ines where you have to turn the "compass" slowly in one direction for a cou ple of turns or else, more recently, wave it in a figure of eight patern.

Can anyone tell me how this calibration is done and whet the point of turni ng things in a circle / figure eight pattern is ? A good web reference wou ld be much appreciated.

Thanks,

Mike

• posted

In 2D, if you rotate the compass, you can plot a series of points based on the readings from the magnetometers. In the common two-magnetometer approach, you can use simple trigonometry to convert each orientation to a point on a circle as you turn. In a correctly calibrated compass, you'll have a perfectly round circle, centered over the origin.

"Hard iron" errors, where a magnetic object exists (say) in your car, and produces a static magnetic field, will tend to displace the circle from the origin, since they add a constant bias to the output from the magnetometer, no matter its orientation. "Soft iron" errors, which are due to flow of magnetic field along lines of ferromagnetic materials, will tend to distort the circle into an ellipse. Again, that's constant. Both of those errors are dependent on the mount, and nothing else. The calibration then consists of finding the transform that bends the ellipse back into a circle, and moves it back over the origin. Note that neither of these is actually dependent on which way north actually is, just the local magnetic characteristics of the compass mount.

The Earth's magnetic field is not exactly perpendicular to the earth's surface, especially nearer the poles, so addition work needs to be done depending on your precision/accuracy requirements, and if the device can move in three dimensions, you need a somewhat more complex procedure (which is where the figure-eight comes from).

And then none of that takes into account declination (that the magnetic poles are not actually collocated with the geographic poles), or local variance. For both of those you need to know your position and you need a database for the local variance (you can compute the declination from just your position, so long as you know where the magnetic pole is - it does move slowly).

• posted

In an ideal world, where you have no disturbances around the magnetic sensor, if you plotted the readings you got from the magnetometer as you rotated the device you would get a perfect circle (2d) or sphere (3d). In this case, you can tell the direction of magnetic north by just looking at the vector you get by the reading.

In real life, things aren't so simple.

First, there are 'hard' magnetic disturbances, caused my metals getting magnetized, or current flowing through wires. The affect of this type of disturbance is to shift your circle/sphere off in some direction. This type of disturbance can be corrected for by just measuring the offset of the circle during calibration and subtracting it.

A second, and harder to correct, disturbance is called 'soft' as is caused by having ferrous metals around the sensor that disturb the field. This will tend to squish and stretch the circle/sphere. The normal way to correct for this is to fit the squished pattern to an ellipse/ellipsoid, and scale the readings along the axis to get the data to be more circular.

The spinning action requested is intended to give the unit a number of readings in various directions so that the shape of this response pattern can be measured.

The figure 8 pattern can be needed if the sensor is slow or is affected by the recent history of the readings it has made, as the figure 8 makes you rotate the unit in both directions. It also can get you to calibrate with more data points as 1 figure 8 is about the same as 2 rotations.

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.