**By Mike Stanley**

*Originally posted on Freescale’s Smart Mobile Devices Embedded Beat Blog*

* *** **

My last couple of posts have explored options and technologies for measuring magnetic fields (see “Magnetometers Come in Multiple Flavors” and “Magnetic Sensor makes Electronic Compass Design Easy”). Today, we’ll explore issues that you may encounter when using *any* magnetic sensor in consumer applications. To keep things simple, let’s consider the case where you’re integrating a magnetic sensor into a smart phone. Nominally, you would like to use your magnetic sensor to implement compass and navigation features. In a pristine environment, free from interference, we could take measurements directly from our sensor. The real world is not that simple.

**Problem #1: Distortion of the magnetic field due to the presence of soft iron.**

Soft iron, you say? What’s that? Think steel. Think EMI shields, screws and battery contacts. To illustrate the point, I performed a finite elements simulation of a U-shaped piece of steel sitting in a uniform magnetic field. Figure ‘1A’ shows how the magnetic field (which would otherwise be shown as vertical lines) is distorted by the presence of our “U-bar”. Steel provides a “lower resistance” path to the magnetic field than does the surrounding air. So it’s natural for the field to be diverted.

Figure “1B” takes that same U-bar and rotates it exactly 180 degrees in the same ambient field. You can see similarities in the field distortion. We can see just how similar “1A” and “1B” are by taking “1B”, and flipping it, first about one axis and then the other, to obtain “1C”, **which is identical in form to Figure “1A”. **

This makes a lot of sense when you realize that *from the steel’s perspective*, “1A” and “1B” are identical except for the polarity of the ambient magnetic field. We *should* get symmetrical results.

More importantly, we’re going to be able to use this simple observation to remove the distortion caused by soft iron from our measurement of the ambient magnetic field.

To see how, let’s take this same U-bar and rotate it in 20 degree increments in the same field. At the same time, let’s measure and plot the magnetic field at the “dot” you see nestled near the base of the “U”. It’s important to note that this point is fixed relative to the disturbing metal. They rotate together.

The symmetry seen above continues to hold as we rotate our soft iron. The field distortion at each angle of rotation matches (after the “flips” noted above) the distortion seen at that angle + 180 degrees. More importantly, the field magnitude measured at each angle matches the field magnitude measured at that angle + 180 degrees.

If we plot the x/y sensor readings for all of our points, we will get an ellipse (you can click on Figure 2 to see a larger image). This is a function of the basic physics, and always holds true, regardless of the sensor type used to make the measurement.

If there were no soft iron present, and we simply rotated our sensor, the ellipse would collapse into a simple circle. Since the field remains the same regardless of the angle of measurement, this must be the case. So we see that the effect of soft iron is to distort a circle whose radius is equal to the magnitude of the ambient magnetic field into an ellipse.

This result can be extended to 3 dimensions. Measurements taken while rotating a sensor in free space undisturbed by hard or soft iron can be visualized as a sphere with fixed radius equal to the magnitude of the ambient magnetic field. Adding soft iron to the mix will distort that sphere into a 3D ellipsoid (Figure 3).

The equation for a 3D sphere is:

x^{2} + y^{2} + z^{2} = r^{2}.

In matrix form, if we have X = [x y z], then

XX^{T} = r^{2}.

For an ellipsoid, it is:

x^{2}/a + y^{2}/b + z^{2}/c = 1.

In matrix form this is

You can see that the equation of the sphere can be linearly derived from that of the ellipsoid, and vice versa. If we take a representative set of samples on the surface of our ellipsoid, we can, through a variety of methods, determine the reverse mapping from distorted to undistorted magnetic field readings. Essentially, we’re curve fitting in 3 dimensions. Now on to…

**Problem #2: Distortion of the magnetic field due to the presence of hard iron**

“Hard iron” is just a physicist’s way of saying “permanent magnet”. “Why,” you might ask, “am I worried about permanent magnets?” Well, that smart phone we mentioned must have a speaker. And speakers have magnets. And a lot of phone holsters have magnets to secure the device. So it turns out that yes, we DO have to deal with them. The good news is that (compared to soft iron effects), compensating for hard iron offsets is relatively simple. If a magnet is fixed in location and orientation with respect to our sensor, then there is an additional constant field value added to the value that would otherwise be measured. If only soft iron effects are present, the ellipsoid mentioned above should be centered at [x,y,z] = [0,0,0]. A permanent magnet fixed relative to the measurement point simply adds an offset to the origin of the ellipsoid. If we have a large enough data set, we can determine that offset as

hard iron offset = [x_{max}-x_{min}, y_{max}-y_{min}, z_{max}-z_{min}]/2

This technique will NOT work for magnets that move with respect to the sensor. The magnet on the phone holster flap can’t be permanently canceled out. But that’s good news! A sudden shift in offset/magnitude of our calculated field probably implies that the phone has been inserted or removed from its holster. That can be a useful thing to know.

**Implications**

The techniques discussed generally employ some form of curve fitting, which raises subtle issues that don’t get discussed much: How many data points do we need in our constellation of sample points? How often does that constellation need to be updated? How do we decide to add or drop points to/from the constellation? What should we do when a sudden change in ambient field magnitude is detected? What numerical method(s) should be used to calculate the trim parameters? How do you deal with uncorrelated magnetic disturbances that occur around us every day? How do you deal with field variations over time and temperature? The answers to these questions make up much of the “secret sauce” used by various vendors in their calibration algorithms.

Earth ambient magnetic field as a function of zip code can be obtained from the online National Geophysical Data Center magnetic field calculator. But for some applications, you may not even care what the local value is. The important point to take from the discussion above is that we are leveraging the symmetry of the data set to drive a solution. Compass orientation is determined by ratios of the 3 dimensions calculated. If those values are off from the expected values by a multiplicative constant, the ratios still hold.

I purposely didn’t talk about the specific curve fitting procedures used to perform the reverse mapping described above. This area continues to be the subject of much research. I’ve listed a few papers in the references at the end of this post. Although often intense, don’t let the math scare you. Almost all have the basic assumptions discussed above at the heart of their approach. Some solve the problem only in the X/Y plane, others address all three dimensions. Tradeoffs include program and variable memory space, CPU cycles required, and ease of implementation. If your sensor vendor is serious about servicing this market, they should be able to supply pre-packaged routines to bootstrap your product development efforts.

A basic requirement of these approaches is that the sensor output data vary linearly with magnetic field. If your sensor is non-linear, correction factors must be applied prior to applying the techniques discussed. Linear scale factor differences *between* axes can be automatically resolved by a number of algorithms.

**More fun to be had**

If you click on Figure 1, you’ll be taken to an animated GIF that rotates through the various positions shown in Figure 2. You can also download the original PowerPoint file for both Figure 1 and Figure 2, which have been released under a Creative Commons Attribution-NonCommercial 3.0 Unported License.

My FE simulations were done with the free FEMM 4.2 simulation tool by Dr. David Meeker, who graciously offered examples and hints about the best way to run the simulations. I’ve used a lot of free engineering tools from the web. This one is more robust and easier to use than many others I’ve tried.

**References**

- Magnetometer Autocalibration Leveraging Measurement Locus Constraints, Demoz Gebre-Egziabher, Journal of Aircraft, Vol. 44, No. 4, July-August 2007
- National Geophysical Data Center magnetic field calculator at http://www.ngdc.noaa.gov/geomagmodels/IGRFWMM.jsp.
- A Geometric Approach to Strapdown Magnetometer Calibration in Sensor Frame, Vasconcelos, Elkaim, Oliveira & Cardeira
- A Non-Linear, Two-Step Estimation Algorithm for Calibrating Solid-State Strapdown Magnetometer, Gebre-Egziabher, Elkaim, Power & Parkinson.
- Iterative calibration method for inertial and magnetic sensors, Dec. 2009, Dorveaux, Vissiere, Martin & Petit
- A Research of an Improved Ellipse Method in Magnetoresistive Sensors Error Compensation, Aug. 2009, Lian-yan, Qing & Wen-yuan
**Free tool!**Finite Element Method Magnetics, FEMM 4.2.- Freescale Semiconductor Magnetic Sensors Page

## One Comment

always confuse about this topic..lots of question in my mind but no one can give me right ans…just browsing on the internet and find your blog after long time and i am really thankful to you…the way you describe this topic i really appreciate you…thanks a lot.