Paul Stoffregen of PJRC wrote a really awesome cross-platform calibration helper that is great for doing both soft and hard iron magnetometer calibration. What's nice about it is you get a 3D visualization of the magnetometer output and it also tosses outliers and tells you how much spherical coverage you got!
MotionCal is available for Mac, Windows and Linux, you can download it from clicking here.
Look for this section in the website:
And click the one that matches your computer the best.
Next we have to tell the microcontroller board to send the magnetometer (and, if there is one, accelerometer and gyroscope) data out over serial in the right format.
Open up the
Open up the serial console, you'll see SensorLab initialization and detection of whatever magnetometer is available. In this case is a LIS3MDL, but any magnetometer can be calibrated!
You'll then see a stream of data that looks like:
The first three numbers are accelerometer data - if you don't have an accelerometer, they will be 0
The middle three numbers are gyroscope data - if you don't have an gyroscope, they will be 0
The last three numbers are magnetometer, they should definitely not be zeros!
Twist the board/sensor around. Make sure its not near any strong magnets (unless that's part of the installation)