Phones, drones, robots, and handheld devices often contain an electronic compass. It looks as if the device knows where north is.
A magnetometer is not a tiny device that directly knows direction.
It measures the magnetic field vector in the sensor coordinate system. North, heading, and compass direction are computed later.
The first model is: a magnetometer measures local magnetic field. An electronic compass extracts the geomagnetic component, combines it with attitude and calibration, and interprets it as horizontal heading.
Earth field + nearby magnetic interference
-> 3-axis magnetometer measures Bx / By / Bz
-> Hard-iron, soft-iron, and offset correction
-> Tilt compensation using accelerometer
-> Horizontal heading estimate
What It Measures
A three-axis magnetometer outputs:
Bx, By, Bz
The unit is often uT, microtesla. Earth’s magnetic field near the surface is usually only tens of uT. That is small compared with many local fields from motors, magnets, high-current wires, speakers, or steel structures.
This is related to Hall sensing because both involve magnetic fields. But the use case is different. A Hall switch often detects a nearby magnet or current-generated field. A compass magnetometer tries to measure the much weaker geomagnetic vector and infer direction.
That makes it very sensitive to magnetic interference.
Why It Can Point North
Earth has a magnetic field. Projected onto the local horizontal plane, it roughly points toward magnetic north.
If the device is level and the local field is clean, heading can be estimated from horizontal X/Y components:
heading = atan2(By, Bx)
Signs and axis order depend on the device coordinate system, but the idea is the same.
Two boundaries matter:
First, magnetic north is not true north. The difference is magnetic declination and depends on location.
Second, the magnetometer provides a long-term direction reference, not fast angular-rate tracking. Gyroscopes usually handle short-term rotation better; magnetometers help pull heading back over time.
Why Tilt Compensation Is Needed
Earth’s magnetic field is not purely horizontal. It has a vertical component.
When the device is level, X/Y components can represent horizontal field. When the device pitches or rolls, the vertical component mixes into sensor X/Y axes.
If you still compute:
heading = atan2(By, Bx)
the heading changes with tilt.
Electronic compasses usually use an accelerometer to estimate gravity direction, derive roll and pitch, rotate the magnetic vector back to a horizontal frame, and then compute heading.
Accelerometer -> gravity direction -> roll / pitch
Magnetometer -> 3-axis magnetic field
Tilt compensation -> horizontal magnetic field
Horizontal field -> heading
Hard-Iron Interference
Hard-iron interference is a fixed magnetic field source near the device.
Examples:
- Permanent magnets
- Speakers
- Magnetic mounts
- Motors
- Magnetized screws
- Relays
- Stable fields from high-current wires
If the source is fixed to the device, it rotates with the device. To the magnetometer, it looks like a fixed offset added to the Earth field:
measured_field = earth_field + fixed_offset
In an ideal device, rotating through all directions would produce points on a sphere centered at the origin. Hard-iron interference shifts the sphere’s center.
Soft-Iron Interference
Soft-iron interference comes from ferromagnetic materials that distort the magnetic field.
Examples:
- Steel structures
- Iron housings
- Screws and brackets
- Motor cases
- Metal battery packs
- Vehicle frames
Soft iron stretches, compresses, or skews the magnetic field differently by direction. A sphere becomes an ellipsoid, sometimes with axis coupling.
Hard iron: moves the sphere center
Soft iron: stretches the sphere into an ellipsoid
Soft iron is harder because it changes scale and orthogonality, not only offset.
Why Figure-Eight Calibration Helps
When a phone asks you to draw a figure eight, the goal is not the shape itself. The goal is to collect magnetometer samples in many orientations.
Ideally, rotating the sensor through space produces a sphere of samples. Offsets, scale errors, hard iron, and soft iron produce a shifted ellipsoid. Calibration estimates an offset and correction matrix that map it closer to a centered sphere:
shifted ellipsoid -> offset and correction matrix -> approximate sphere
If you only rotate in one plane, the calibration data are incomplete. The figure-eight motion simply encourages richer 3D coverage.
Why Indoor and Vehicle Use Is Hard
Magnetometers fail when the local magnetic field is no longer clean Earth field.
Typical trouble locations:
- Near motors, fans, or speakers
- Near high-current power lines, batteries, or inverters
- Magnetic phone cases or mounts
- Cars, machinery, and metal frames
- Reinforced concrete and steel buildings
- Elevators, subways, and factory equipment
The magnetometer still measures a magnetic field. It is just no longer the field you wanted.
Fusion With Gyro and Accelerometer
The three sensors complement each other:
Gyroscope: fast rotation tracking, but drifts
Accelerometer: gravity reference for roll and pitch
Magnetometer: long-term heading reference, but sensitive to magnetic interference
Good fusion algorithms do not always trust the magnetometer. They check field strength, sudden direction changes, and consistency with history. If the field is clearly polluted, the algorithm should reduce magnetometer weight or ignore it temporarily.
Engineering Takeaway
A magnetometer can support an electronic compass, but its output is local magnetic field, not reliable direction truth.
It is useful for:
- Rough heading in phones and handheld devices
- Long-term heading support for drones, robots, and vehicles
- Yaw reference in IMU fusion
- Detecting abnormal magnetic environments
It is weak for:
- Strong magnetic interference
- High-accuracy heading near motors, magnets, and high-current wiring
- Arbitrary device orientation without tilt compensation
- Cross-device use without calibration
The key sentence is:
A magnetometer measures magnetic field.
Heading is inferred from geomagnetic field, tilt compensation, calibration, and fusion.