All posts Signals

Taming the compass: magnetometer noise cancellation in practice

In short

A magnetometer anchors the one orientation angle gravity cannot provide, but Earth's field is weak and every desk is a minefield: steel frames, laptop magnets, phone speakers. The pen tames this with two classic calibrations, hard-iron and soft-iron, plus continuous plausibility checks against the other sensors.

The post explains each distortion, the calibration picture, and the rule that the compass corrects slowly or not at all.

The short version. The full post has the detail and the why.

In this post

  1. Why a pen carries a compass at all
  2. The two flavours of magnetic lying
  3. Calibration you can picture
  4. Trust, but verify continuously

Why a pen carries a compass at all

Gravity, read through the accelerometers, reveals two of the three angles of the pen's orientation, how it tilts. The third angle, which way it points on the horizontal plane, is invisible to gravity. That is the magnetometer's one job: anchor the heading. Without it, the heading estimate can only come from integrating rotation, and anything integrated drifts.

The two flavours of magnetic lying

Magnetic interference comes in two flavours with useful names. Hard-iron distortion is a constant bias, something magnetised near the sensor adds a fixed push. Soft-iron distortion is direction-dependent, nearby ferrous material bends the field differently as the pen rotates. There is also the plain ambush: the writer sets the pen down next to a laptop whose speaker magnet swamps Earth's field entirely.

calibrated: a centred sphere raw: shifted and squashed hard iron shifts · soft iron squashes correct
Rotate the pen through all orientations: a clean magnetometer traces a centred sphere; interference shifts and squashes it. Calibration measures the distortion and undoes it.

Calibration you can picture

Rotate a perfect magnetometer through every orientation and its readings trace a sphere centred at zero. Hard-iron bias shifts that sphere off-centre; soft-iron distortion squashes it into an ellipsoid. Calibration is the act of measuring that shifted, squashed shape and computing the correction that restores the centred sphere. It is geometry, honest and checkable, and it is redone over time because the pen's own magnetic environment can change.

Trust, but verify continuously

Calibration cannot fix the laptop ambush, so the firmware never trusts the compass blindly. The magnetic reading is continuously checked for plausibility, is its strength near Earth's field, is it stable, does it agree with what the gyroscope says about rotation? When checks fail, the compass is quietly ignored and heading rides on the gyroscope until sanity returns. The rule of thumb: the magnetometer corrects slowly, or not at all.

Key takeaways

  • The compass anchors the one orientation angle gravity cannot see.
  • Hard-iron bias shifts readings; soft-iron distortion bends them by direction.
  • Calibration restores the centred sphere and is refreshed over the pen's life.
  • Plausibility checks demote the compass instantly when the desk lies.

A sensor you sometimes ignore, on purpose, with criteria, is worth far more than a sensor you always believe.

Read next