So first, many people prefer to use the TRK mode (if a GPS is connected), which uses GPS ground track, instead of HDG, which uses the magnetometer, to help smooth things out.
Beyond that, find the magnetometer and make sure it's in a suitable location, away from power and ferrous metals, etc...