Any old-fashioned mechanical compass is probably going to out-perform any normal MEMS
magnetometer module I’m afraid, the price you pay for the miniaturization is noise,
inaccuracy, and calibration headaches! Most PCBs are covered in small bits of steel
(SMT component end-caps), so placing a magnetometer on a PCB is problematic
from the get-go.
Boat compasses are designed to stay level as they are mounted on gimbals - the same
applies to an electronic compass on a boat, you’d need to mount it just the same for
reliable readings, or else use an IMU to correct the reading (which implies full 3D accurate
calibration of the MEMS magnetometer, not an easy task I fear).
There are specialized sensor modules that use MEMS magnetometers and claim <=0.5 degree accuracy
out of the box, but they are specialist and need to be mounted well away from iron and steel objects
like any other compass for any hope of achieving that accuracy. You pay a lot for the privilege of a pre-
calibrated iron-free module - basically any mechanical prismatic compass will perform just as well, and
will function without needing a power source - for navigation you require that as a backup at least I
Another issue that affects magnetic sensing these days is the number of devices containing
strong magnets - many have these just to hold a lid shut! The reading of a compass is thrown out
by a field on the order of 1 microtesla, strong modern magnets are 100,000 microtesla or more.
Even though magnetic fields fall off as distance cubed, this means its difficult to be sure of a clean reading.