Consider an analog or even digital compass. While you can reasonably expect either to point towards magnetic north when held flat, when you add tilt and/or roll to the equation, things get a bit wonky. That is unless you’re maker “lingib,” who was able to construct a magical compass using an Arduino Uno and an MPU-9250 IMU unit, with an accelerometer/gyro in the same package.
As seen in the video below, when the compass unit is set at an angle, the heading output varies significantly—as much as 100 degrees according to the project write-up. When stabilization is turned on, however, the gyro/accelerometer is used to compensate for magnetometer heading variations—reducing output errors to just a few degrees.
This Instructable explains how to make a tilt compensated compass using an Arduino Uno R3, an LCD display, and an IvenSense MPU-9250 multi-chip-module that contains an MPU-6050 accelerometer / gyro and an AK8963 magnetometer within the same package.
The LCD simultaneously displays the heading, (P)itch, and (R)oll.
The heading accuracy is within 2 degrees depending on how well the compass has been calibrated.
Without tilt compensation the compass headings vary significantly … sometimes by as much as 100 degrees.
When stabilised, the tilted compass headings only vary by one or two degrees … the improvement is amazing.
The tilt stabilization may be disabled by placing a jumper wire between Arduino pins A0 and GND.