Calibrating the magnetometer is required to get good orientation data!
Before running this example - make sure you have done the calibration pre-check and write check!

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!

Step 1 - Download MotionCal Software

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.

Step 2 - Configure & Upload the AHRS calibration Example

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 Adafruit_AHRS->calibration example

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

Select your desired board & port from the Tools menu then click Upload

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:
Raw:-58,-815,8362,76,-121,-95,-375,-159,-24
Uni:-0.07,-0.98,10.00,0.0832,-0.1327,-0.1046,-37.50,-15.93,-2.50

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!

Step 3 - Run MotionCal

Close the serial port, and launch MotionCal

Select the same COM / Serial port you used in Arduino

Twist the board/sensor around. Make sure its not near any strong magnets (unless that's part of the installation)

Keep twisting until you get a complete 'sphere' of red dots. At this point you are calibrated!

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?)

You'll see a large green checkmark once the calibration is saved and verified!

Step 4 - Verify Calibration

Re-load the sensor_calibration_read sketch to verify the calibration was saved!

This guide was first published on Feb 17, 2020. It was last updated on Feb 17, 2020.

This page (Magnetic Calibration with MotionCal) was last updated on Feb 16, 2020.

Text editor powered by tinymce.