Rotate the sensor through every direction to calibrate it. Keep it away from motors, wires with current and steel.
What the message means
Nearby metal and magnets add a fixed offset, and current in wires adds a changing field. The heading then points the wrong way.
Fix it in this order
- Run the calibration routine and turn the robot slowly through all orientations.
- Store the offsets and subtract them from each reading.
- Mount the sensor on a mast, away from motors and the battery.
- Keep power wires twisted and away from the sensor.
- Use tilt compensation with the accelerometer when the robot is not level.
- Check the local magnetic declination if you need true north.
Why it happens
The compass in the MPU9250 is a separate AK8963 chip inside the package, and it measures the local magnetic field, which is the Earth’s field plus everything near the sensor. Fixed magnetic parts on the robot, such as steel screws and motor magnets, add a constant offset called hard-iron error. Soft magnetic metal bends the field and stretches the readings into an ellipse, called soft-iron error. Turning the robot through all orientations while logging the minimum and maximum of each axis lets you estimate and subtract both.
Motor and battery wires carry current, and current makes its own field, which is why the heading shifts when the motors run. Magnetic north is also not true north; the difference, called declination, depends on where you are, so look up the value for your location if you need true north.
If you see this, do this
| You see | Do this |
|---|---|
| Heading off by a fixed amount | Calibration offset. |
| Heading changes when motors run | Magnetic interference. |
Related
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.
Why this happens
A magnetometer measures the total magnetic field at its position, not only the Earth’s. A permanent magnet or a steel screw fixed to the robot adds a constant vector to every reading. As the robot turns, the readings on the horizontal plane should trace a circle around the origin; a fixed extra field moves the centre of the circle (hard iron). Soft iron, such as a steel plate, distorts the field and turns the circle into an ellipse.
Heading is then calculated from the angle of the horizontal vector. If the circle is off centre, the angle is wrong by an amount that depends on direction, so the error is not a simple fixed offset. Current in motor or battery wires makes its own field, which is why the heading shifts when the motors start.
How to confirm the cause
- Log raw X and Y while turning the sensor slowly through a full circle on a level surface and plot the points. A circle centred on zero means good data; an off-centre circle means hard iron; an ellipse means soft iron.
- Record the minimum and maximum of each axis during that turn.
- Hold the robot still, then run the motors. If the reading moves by a large amount, the wiring or motors are too close.
- Move the sensor away on a short mast and repeat the test. A big improvement points to interference.
- Check the axis orientation. The compass inside the MPU9250 has its axes arranged differently from the gyro and the accelerometer, so the mapping matters.
Worked example
Applying a simple min-max (hard-iron) calibration. Suppose the X axis ranged from -18 to 42 and Y from -30 to 30 during the turn, in microtesla:
float xo = (xmax + xmin) / 2.0; // (42 + -18) / 2 = 12
float yo = (ymax + ymin) / 2.0; // (30 + -30) / 2 = 0
float hx = mx - xo;
float hy = my - yo;
float heading = atan2(hy, hx) * 180.0 / PI;
if (heading < 0) heading += 360.0;Common mistakes
- Calibrating on a table, then mounting the sensor next to motors and not calibrating again.
- Calibrating with the motors off but running them in use.
- Mixing up the axis order of the compass and the other sensors.
- Using the heading when the board is tilted, without tilt compensation from the accelerometer.
- Forgetting that magnetic north differs from true north by the local declination.
If this did not fix it
- The magnetometer chip in your board is not the one the library expects, which happens with clones.
- Nearby steel such as a battery holder or chassis screws; test with the sensor on a plastic mast.
- The compass not being enabled or read correctly, so it returns the same value every time.