Automatic nulling of magnetometer offsets

[There is now a better method for removing offsets in flight. See this discussion. - WJP]

The picture shows a UAV DevBoard connected to Jordi's magnetometer breakout board.

First, since my discussions tend to get long winded, if you want to "cut to the chase", you might want to read this.

I recently integrated Jordi's HMC5843 magnetometer breakout board with the UAV DevBoard (UDB).

Jordi's board is really great, having the onboard bidirectional level shifters and builtin pullup resistors made it possible to simply connect Jordi's board directly to the I2C pins in the programming connector. Thank you very much, Jordi.

So, heli and quadcopter pilots who are using the UDB would do well to buy Jordi's board, my plan is to integrate it into the heli firmware that is being developed for the UDB. There is now a magnetometer demo and instructions for the UDB.

But that is all besides the point of this discussion....

Along the way to implementing the magnetometer demo, I realized the hardest part was going to be determining the offsets of the magnetometer. The integration of the magnetometer itself into the DCM algorithm was trivial, but what to do about the offsets?

The HMC5843 has a self calibration feature, so you can determine the gains just fine. But the first magnetometer I bought had rather large offsets. So I bought 3 more (1 more from Jordi, and 2 more from Sparkfun). It turned out that all 4 units had some offsets. Even if they did not have offsets, it would still be possible for stray magnetic fields in the aircraft to create offsets.

So, some sort of procedure is needed to null the offsets.

I thought of several manual ways to do that, and I read several postings on the subject....

Then I wondered if there might be some way to use the direction cosine matrix to automatically compute the offsets in flight. And there is....even for dynamically changing offsets, such as from power leads to the motors...

The basic idea is the following...

When the aircraft is rotating, the offset fields rotate with the aircraft, while the earth's magnetic field is stationary. So it should be possible to use the direction cosines to separate the two fields.

The basis of the idea is the same as flipping the magnetometer exactly 180 degrees along one of the axis and averaging the two readings. The result is the offset.

The idea can be extended to rotations other than 180 degrees, even small rotations, by using the direction cosine matrix and some vector algebra.

The resulting theory and implementation is described here, and it has been tested. It works rather well.

It was interesting to watch the computations of the offsets during the tests. A few random turns along a couple of the axes was all that was needed to compute the offsets. So, you can either null the offsets prior to takeoff by rotating your aircraft a bit, or simply let it happen automatically during the first few turns of the flight.

It was also interesting to see what would happen if I deliberately manually set the offsets to the wrong values, they would quickly reset to the correct values after a few turns.

The bottom line is that now both the calibration and the nulling of offsets can be done completely automatically, without any manual operations or entry of measurements. It is possible to extend any DCM-based IMU, such as the ArduIMU, to use this method to simplify the integration of a magnetometer.

Best regards,

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • Hi Bill,

    Sorry, you should find the answer on page 6. The principal is easy. Whenever you apply a reset pulse to the strap the magnetometer will function with negative sensitivity the rest of the time.

    If you apply a set pulse vice versa is true. It will have positive sensitivity.

    Now here is the trick! Suppose the ambient magnetic field is a certain constant "m"

    This same magnetic field will be read as +m after a set pulse and as -m after a reset pulse. Averaging both readings you will be left with 2 x offset voltage.

    I hope this helps.

    Now can you please give the analytical expression for R12 matrix?

    Also how did you figure out that you need a low pass filter "eq 11 " in your algorithm and is it truely a low pass filter?

  • Hi Bill,

    Two small questions:
    1) How does equation 11 solve the problem in issue 1.
    2) Do you mean that the denominator is the same as tau.

    Please explain with some detail. I appreciate it.

  • Hi Bill,
    Please see the document : AN212 "Hnadling Sensor Bridge Offset" page. 5

    The method is simple. Suppose that the ambient magnetic field is -127mG.

    Now if you apply a set pulse to S/R strap you will have a reading of: -127mG + offset

    Now if you apply a reset pulse to the S/R strap the reading will be :+127mG + offset

    As you see the ambient magnetic field has changed sign after the reset. This a property of AMR sensors.

    Therefore offset = ( reading1 + reading2 ) / 2 .

    Best regards
  • Hi Bill,

    In your equation (8) you have an "exact formula" for the offset in the plane perpendicular to the rotation
    axis. You use instead the variable vector: the numerator of that equation. In equation (11) you update
    the offset estimation by adding that vector multiplied by the ratio of the sampling time to the time constant.
    I wish to know what is the reason of that?

  • Hi Bill,

    Why didn't you use the set/reset straps of the magnetometer. You could easily find the offset voltage by a simple algebraic sum of two samples?
  • Hello Bill,

    I have the sparkfun HMC5843 breakout board and it doesn't come with a voltage regulator. I was wondering if I have only to supply a 3.3V regulated to the board or Do I need to convert the I2C pins voltage as well?


  • Some thoughts for discussing:

    how is calculated the DCM?

    - If you are using only gyros and accelerometers, the algorithm is really nice depending on your sensors/estimator accuracy.

    - If you are calculating some DCM terms (yaw) based upon ground speed vector, the off-set algorithm won't work with helicopters or quad-rotors. And for planes, if you are going to use the magnetometer to measure, for instance, the side-slip angle, you will get a wrong value.

    - If you are using the same magnetometer to calculate the DCM, then you will obtain a "false off-set" coming from the 1st iteration, DCM feeds the off-set algorithm and off-set algorithm will feed the DCM and so on, you are not calibrating under a well known reference, you are using the same magnetometer to correct the magnetometer.

    - If you have a World Magnetic Model embedded in your system, well knowing b_c at every iteration, equation 3 is easily solved with a least squares algorithm.

    Actually with this last option, you can obtain the DCM directly as well.

    I think this algorithm is very nice to calibrate, in an automatic way, the magnetometers in ground. You know, placing the HW and turn it around, executing at the same time the algorithm. Thank you very much for the idea :P
  • I've been trying to understand the C code example. The line "MatrixMultiply(1,3,3,
    rmatTransposeMagField,magFieldEarthPrevious,rmat) ;" has got me stumped. I'm not familiar with the dsPic devices and their compilers, but this looks like it is multiplying rmat by the previous value of the earth field, and storing it in rmatTransposeMagField. But from the equation derivations, shouldn't the term rmat be rmat transpose?

  • Developer
    Nice work Bill,
    a really elegant solution to the problem...

  • Be careful with the magnetometer board. This capacitor DOESN'T MATCH the manufacture requirement (low ESR) and it can make troubles to you.

    I had a bad experience under ~0ºC, the magnetometer couldn't read anymore until the temperature rose again (aluminium electrolytic and this kind of SMD increasing its ESR dramatically under low temperatures).

    The solution is to remove this cap and put an polymer/organic electrolytic capacitor :P

    If you don't expect this temperatures then it doesn't matter xD
This reply was deleted.


DIY Robocars via Twitter
7 hours ago
DIY Robocars via Twitter
9 hours ago
DIY Robocars via Twitter
RT @breadcentric: Bingo card for AWS DeepRacer Finale, starting in 10 minutes on https://www.twitch.tv/aws ! #AWSDeepRacer #DeepRacer #Machin…
12 hours ago
DIY Robocars via Twitter
RT @NVIDIAEmbedded: It's #NanoFriday - the RB-0 uses the same suspension concept as #NASA's newer differential-bar rovers. This educational…
12 hours ago
DIY Robocars via Twitter
RT @chr1sa: On May 22, we're returning to in-person AI @DIYRobocar racing at @circuitlaunch. The Amazon @awscloud DeepRacer team will be pr…
DIY Robocars via Twitter
RT @breadcentric: On my CV: Hobbies: Training bananas to race on tracks #AWSDeepRacer #DeepRacer https://t.co/MKe14hNyux
DIY Robocars via Twitter
RT @breadcentric: See how the April AWS DeepRacer races have ended and a couple bits of news: https://blog.deepracing.io/2021/05/09/aws-deepracer-league-2021-update-11-end-of-april-special/ #AWSDeepRacer #Machin…
DIY Robocars via Twitter
RT @sunilmallya: Representation Learning +Instance Transfer to learn new reward functions along with advantage based filtering of new exper…
DIY Robocars via Twitter
Apr 27
DIY Robocars via Twitter
Apr 27
DIY Robocars via Twitter
RT @f1tenth: Sliding (autonomously) into the weekend like ... 🤖😎 #f1tenth #robots #AutonomousVehicles @OpenRoboticsOrg @NVIDIAEmbedded @Aut…
Apr 25
DIY Robocars via Twitter
RT @chr1sa: One of the problems with autonomous car racing is that watching software drive is not a very exciting spectator sport. To help…
Apr 25
DIY Robocars via Twitter
RT @SmallpixelCar: Replaced AGX Xavier with @NVIDIAEmbedded Jetson Xavier NX. Now both cars look fast and clean https://t.co/jlcoY2EjZf
Apr 25
DIY Robocars via Twitter
RT @chr1sa: Yesterday we had a record number of competitors (36) in our monthly @DIYRobocars virtual autonomous car race. We kept it going…
Apr 25
DIY Robocars via Twitter
Apr 16
DIY Drones via Twitter
RT @chr1sa: After more than a year of only virtual races, @DIYRobocars returns to the newly renovated @circuitlaunch on May 22 for the resu…
Apr 11