If your hobby is RC, drones, robotics, electronics, augment reality or similar then sooner or later you will meet with the task of magnetometer calibration. Any magnetometer module must be calibrated, because the measurement of magnetic field subjected to some distortions. There are two kinds of these distortions: the hard iron distortions and the soft iron distortions. The theory about these distortions you can find here. To get the accurate measurements you should calibrate magnetometer for hard and soft iron distortions. This instructable describes the easy way how to do it.
Step 1: Stuff You Need
*But you can easily adopt this instructable for another magnetometer module or arduino board.
*This sketch is written for the HMC5883L module, but you can easily adopt it for your module.
Step 2: Making the Calibration Box
For the calibration process you should make the special calibration box (picture 2.1). For making this I used a paper box, but you can use a plastic one, a wooden bar or something else too. You should join the magnetometer module with the box (for example with glue) as shown in the picture 2.1. On the faces of the box you should draw the coordinate system according to the coordinate system of the magnetometer module.
Step 3: Electrical Connection
Connect the magnetometer module and arduino board as shown in the picture 3.1. Note that the supply voltage of magnetometer module can be 3,3 V (as in my case with HMC5883L GY-273 version).
Step 4: Installing the Software and Firmware
Download the software and firmware here.
This archive contains files:
- MagMaster.exe - the magnetometer calibration program
- MagViewer.exe - the magnetometer measurements visualisation program
- Arduino_Code - the arduino sketch for the calibration process
- Arduino_Test_Results - the arduino sketch for testing calibration results
- Arduino_Radius_Stabilisation - the arduino sketch for testing calibration results with sphere radius stabilization algorithm
- MagMaster Files and MagViewer Files - the system files for MagMaster.exe and MagViewer.exe
Copy all these files to any folder. Upload the "Arduino_Code" sketch to the arduino board. This arduino sketch requires the HMC5883L library, copy the folder "HMC5883L" (placed in "Arduino_Code" folder) to the folder "C:\Program Files\Arduino\libraries" before sketch uploading.
Step 5: Calibration
Calibration of magnetometer is the process of getting the transformation matrix and bias.
To get the calibrated measurements of the magnetic field you should use these transformation matrix and bias in your program. In your algorithm you should apply the bias to the vector of non calibrated magnetometer data (X, Y, Z coordinates) and then multiply the transformation matrix by this resulting vector (picture 5.4). The C algorithm of these calculations you can find in "Arduino_Test_Results" and "Arduino_Radius_Stabilization" sketches.
Run MagMaster.exe and select the serial port of the arduino board. The green strings on the program window indicates the coordinates of magnetometer vector (picture 5.1).
Place the magnetometer module (calibration box with attached magnetometer module) as shown on the picture 5.2.1 and click "Point 0" button of the "Axis X+" groupbox. Note that the calibration box is not stationary relatively to fixed horizontal plane. Then place the magnetometer as shown on the picture 5.2.2 and click "Point 180" button of the "Axis X+" groupbox and so on. You should do in the following way (see picture 5.3 too):
- Picture 5.2.1: "Point 0", "Axis X+"
- Picture 5.2.2: "Point 180", "Axis X+"
- Picture 5.2.3: "Point 0", "Axis X-"
- Picture 5.2.4: "Point 180", "Axis X-"
- Picture 5.2.5: "Point 0", "Axis Y+"
- Picture 5.2.6: "Point 180", "Axis Y+"
- Picture 5.2.7: "Point 0", "Axis Y-"
- Picture 5.2.8: "Point 180", "Axis Y-"
- Picture 5.2.9: "Point 0", "Axis Z+"
- Picture 5.2.10: "Point 180", "Axis Z+"
- Picture 5.2.11: "Point 0", "Axis Z-"
- Picture 5.2.12: "Point 180", "Axis Z-"
You should fill the table. After that click "Calculate Transformation Matrix and Bias" and get the transformation matrix and bias (picture 5.3).
The transformation matrix and bias are got! The calibration is complete!
Step 6: Testing and Visualisation
The non calibrated measurements visualisation
Upload the "Arduino_Code" sketch to the arduino board. Run the MagViewer.exe, select the serial port of the arduino board (the boud rate of the seraial port should be 9600 bps) and click "Run MagViewer". Now you can see the coordinates of the magnetometer data vector in 3D space on a real-time (picture 6.1, video 6.1, 6.2). These measurements are non calibrated.
The calibrated measurements visualisation
Edit the "Arduino_Radius_Stabilization" sketch, replace the default transformation matrix and bias data with your obtained during calibration data (your transformation matrix and bias). Upload "Arduino_Radius_Stabilization" sketch to the arduino board. Run the MagViewer.exe, select serial port (the boud rate is 9600 bps), click "Run MagViewer". Now you can see the calibrated measurements in 3D space on a real-time (picture 6.2, video 6.3, 6.4).
By using these sketches you can easy write the algorithm for your magnetometer project with calibrated measurements!