Sensors
Why your phone compass drifts
A magnetometer measures a very weak field, so almost anything nearby wins. What causes drift, what calibration really does, and how to tell them apart.
4 min read
- magnetometer
- calibration
- interference
The earth’s magnetic field is weak. At the surface it runs somewhere between 25 and 65 microtesla depending on where you are, which is roughly a thousandth of the field at the face of a fridge magnet. Your phone is trying to measure that faint background while sitting inside a device full of speakers, motors, metal shielding and a battery drawing current.
That it works at all is slightly remarkable. That it sometimes does not is completely unremarkable, and the reasons divide neatly into two categories that call for opposite responses.
A magnetometer does not measure direction
This is the thing worth internalising, because everything else follows from it.
The sensor measures field strength along three perpendicular axes and reports three numbers. Direction is inferred from the ratio between them, after the accelerometer has been consulted about which way is down so the reading can be corrected for how you are holding the device.
So there is no “compass” in your phone in the sense of a needle that points. There is a three-axis measurement and an arithmetic conclusion. Anything that corrupts the measurement corrupts the conclusion, and the conclusion will be reported to you with exactly the same confidence either way.
Failure mode one: a constant offset
Put a small permanent magnet near the sensor and it adds its own field to every one of the three axes. That addition is roughly constant, because the magnet is fixed relative to the phone.
Constant error in, constant error out. The inferred heading comes out wrong by some fixed amount, and here is the tell: as you turn, the bearing still changes smoothly and at the right rate. It is simply offset. A full turn still reads a full 360 degrees; it just starts from the wrong place.
This is what calibration fixes. The figure-of-eight motion is not superstition and it is not a loading animation. Rotating the device through many orientations gives the software many readings of the same two fields, one fixed to the phone and one fixed to the earth. Only the earth’s contribution changes as you rotate, so with enough orientations the fixed part can be solved for and subtracted.
That is also why calibrating without moving the device does nothing, and why a half-hearted wiggle in one plane does less than a proper figure of eight.
Failure mode two: a distorted field
Now put a large piece of steel nearby instead. Steel does not add a field of its own so much as bend the one already there, and how much it bends it depends on the angle between the steel, the field and you.
The error is no longer constant. It changes as you turn, unpredictably. This is what a car body does, and a lift, and a steel-framed desk, and reinforced concrete, and a laptop.
The tell is the opposite of the first case: the bearing behaves badly as you turn. It sticks for a while, then jumps. It runs fast through one part of the circle and slow through another. A full turn does not add up to 360 degrees.
Calibration cannot fix this, and attempting it makes things worse, because you are asking the software to solve for a constant that is not constant. It will find one anyway and then subtract a wrong number from every future reading.
The diagnostic
Turn slowly through a complete circle and watch the number.
- Smooth but wrong means offset. Calibrate.
- Lumpy, sticky or jumpy means distortion. Move somewhere else.
Two symptoms, two causes, opposite treatments. Getting this the wrong way round is the single most common reason people conclude their phone’s compass is broken.
The third thing, which is not drift at all
Before blaming the sensor, check what the reading is being compared against.
An overwhelming share of reports that a compass is “off by about fifteen degrees” turn out to be a magnetic reading being checked against a true direction, or the reverse. That is not drift, not interference and not calibration. It is magnetic declination doing exactly what it always does, and no amount of figure-of-eight waving will touch it.
If a freshly calibrated phone in open air still disagrees with a known direction by a consistent amount, that amount is probably your local declination. Which is its own subject, and worth understanding properly.