This paper presents the way how the hard iron effect could be compensated and a way to implement it on a small power device such as a microcontroller. Because of the magnetized materials that can stay near a magnetometer sensor and because of the very small magnetic field of the Earth, before the use of the measured values from a magnetometer to determine the heading (angle with the N direction) of the sensor a compensation is needed.