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
At the top of the sketch you'll see a section where you can #include different sensor sets. Not every sensor-set is defined, but our most popular ones are! (You'll need sensors that are Adafruit_Sensor compatible.)
Uncomment whichever kit you are using, and comment out the rest
Open up the serial console and check that the EEPROM/Filesystem was found. There may already be an existing calibration from prior experiments
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)
In the top right you'll see the hard magnetic offsets at the top, the soft offsets in the middle and the field strength at the bottom.
In this case, the hard iron offsets are
[-31.25, 35.67, -116.44]
Take a screenshot of this display, so you can refer to these numbers later!
MotionCal does not calibrate the accelerometer or gyroscope (yet) - so those offsets will be zero
Eventually you'll have enough datapoints that the Send Cal button will activate (its grayed out by default).
Once you can click the button, try clicking it (we had to try a few times?)
Re-load the sensor_calibration_read sketch to verify the calibration was saved!