If you bought the cheap magnetomter module like HMC5883L you can not use it without calibration. Measurement of magnetic field will be subjected to distortion. There are two categories of these distortions: the hard iron distortions and the soft iron distortions. The hard iron errors refer to the presence of magnetic fields around the sensor (magnets, power supply wires) and are related to measurement offset errors, while the soft iron errors refer to the presence of ferromagnetic materials around the sensor, which skew the density of the Earth's magnetic field locally and are related to scaling offset errors. You can read more information about these distortions here.

In other words, to get the correct magnetometer data you should get the calibrated magnetometer data. One of the ways to resolve this problem: you should apply the bias to the vector of the non calibrated magnetometer data (X, Y, Z coordinates) and then multiply the transformation matrix by this resulting vector:

3689598033?profile=originalPicture 1

In this case the magnetometer calibration is the process of getting the transformation matrix and the bias. To get these data you can use the MagMaster program.

You can download MagMaster here.

Example of using the MagMaster



  • MagMaster (placed in MagMaster folder)
  • MagViewer (placed in MagMaster folder)


  • Arduino Sketch (placed in MagMaster folder)

3689598066?profile=originalPicture 2

3689598042?profile=originalPicture 3

3689598079?profile=originalPicture 4

Connect the magnetometer module to the arduino board via I2C bus (picture 4). Upload the arduino sketch to the arduino board (see "Arduino_Code" folder in the "MagMaster" folder). This arduino sketch requires the HMC5883L library, copy the folder "HMC5883L" (placed in "Arduino_Code" folder)  to the folder "C:\Program Files\Arduino\libraries".

Then 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 5, video 1, 2). These data are not calibrated yet.

3689597980?profile=originalPicture 5

Video 1

Video 2

If you see the points of the magnetometer vector coordinates in 3D space the arduino board and the PC connection works right.

Now close the MagViewer window and run the MagMaster.exe (picture 6). Select the serial port of the arduino board. The green strings X, Y, Z will indicates the coordinates of the magnetometer vector.

3689598100?profile=originalPicture 6

Place the magnetometer module as shown on the picture 8.1 and click "Point 0" button of the "Axis X+" groupbox. For the placement of the module you can use the wooden bar or the paper box (picture 7). If you can not connect your magnetometer device to PC then you can use any other way to get to know the magnetometer data. You can enter these data in the program manually.

3689598052?profile=originalPicture 7

Place the magnetometer as shown on the picture 8.2 and click "Point 180" button of the "Axis X+" groupbox and so on. You should do in the following way:

  • Picture 8.1: "Point 0", "Axis X+"
  • Picture 8.2: "Point 180", "Axis X+"
  • Picture 8.3: "Point 0", "Axis X-"
  • Picture 8.4: "Point 180", "Axis X-"
  • Picture 8.5: "Point 0", "Axis Y+"
  • Picture 8.6: "Point 180", "Axis Y+"
  • Picture 8.7: "Point 0", "Axis Y-"
  • Picture 8.8: "Point 180", "Axis Y-"
  • Picture 8.9: "Point 0", "Axis Z+"
  • Picture 8.10: "Point 180", "Axis Z+"
  • Picture 8.11: "Point 0", "Axis Z-"
  • Picture 8.12: "Point 180", "Axis Z-"

3689598213?profile=originalPicture 8

You should fill the table. After that click "Calculate Transformation Matrix and Bias" and get the required matrix and bias (picture 9).

3689598116?profile=originalPicture 9

The transformation matrix and the bias are got. Now you can calculate the calibrated magnetometer data in your device on a real time with using the matrix and the bias as shown on picture 1. The example of the arduino sketch of using this calculation you can find in the "Arduino_Test_Results" folder.

You can apply the sphere radius stabilization algorithm to your program (use for it the Arduino_Radius_Stabilization folder placed in MagMaster folder).

The calibrated magnetometer vector coordinates in 3D space with the radius stabilization shown on the picture 10 and video 3, 4.

3689598230?profile=originalPicture 10

Video 3

Video 4

Using of the arduino is only example, you can easy adopt the arduino code for any other system and use the MagMaster program with it.

E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones


  • Hey people,

    Would anybody here please tell me where I can find almost everything regarding magnetometer calibration from the basic methods to the advanced ones? I mean is there any article or book for that?

    I appreciate a reply.

  • Yury Matselenak, MagMaster работает на этих портах. Ко всем COM портам у меня подключены конвертеры на базе CP2102. MagViewer даже не пытается открыть порты COM11, COM16 и COM18, которые у меня есть, но открывает COM1, COM4. Я наблюдаю это в "Free Serial port Monitor".

    В любом случае, спасибо за проделанную работу. Эта инструкция по калибровке - именно то, что нужно. Все доходчиво, наглядно. Супер! Thanks! Great job!

  • Alex Tim,

    Прога Magviewer написана в Unity3D, возможно этот движок и имеет какие-то особенности по com-портам, я не знаю, но в коде программы я такого ограничения не делал. Может к COM16 и COM18 было другое устройство подключено? А MagMaster работает на этих портах?

  • I have the problem with Magviewer too. It doesn't receive serial data. But I noticed that it happens not for each serial port. I have a few USB-UART converters. For example, COM16 and COM18 don't work, but COM4 works well. So, probably the Magviewer has a limit on numbers of COM ports that can be used.

    So, if you have the problem try another COM port with smaller number. It will help

  • I  dropped some result ,

    This is a  sphere radius stabilization(Arduino_Radius_Stabilisation) &  Ring in MagViewer




  • Thanks for the confirmation.

    the first time, a bit confused about position of the Device(magnetometer)
    someone has already written about the topic you,

    And I have the answer

    "The box is not stationary, and magnetometer is attached (by glue in my case) to box"

    Thank you for share Yury. I'll follow your advice.

    Best regards,


  • coldPrime,

    Your results are correctly!

    My advise after some experiments: usage of wooden or plastic cube (or bar) will increase accuracy of calibration.

  • Hello Yuri,

    Matlab is indeed hard for a beginner.
    I'm not building Space shuttle, just hobbyist
    I'm glad I caught up with you. I've been looking everywhere.

    So, I followed your instructions and calibration with the following results.
    It seems to work correctly, Can you confirm my results?

    This is a great article, Thank you for share Yury.
    I Really enjoyed it.

    coldPrime from other side of the earth.

    Before calibration, After Calibration.




    In My Inclination (52° 7')



    Test Cube

     hmc5883l is attached by 3M Scotch Tape.


    Calibration progress...


    My old Silva (25years old) ^^


  • Yury, great article and thanks for publishing the code too.  I am attempting to use your code to calibrate a 3-axis accelerometer but the final results are not what I expected.  Here is a screenshot.

    3701913778?profile=originalAt the bottom are 3 numbers which are supposed to be the "calibrated values".  These are calculated using the raw values and the matrix.  The uncalibrated range of data is 0-4095 rather than the +/- values you are reading.  1) Will your matrix calculations work with 0-4095 values or do I need to adjust them to +/- values?  2) When everything is working, should the calibrated values be approximately equal to the uncalibrated values?  Thanks

  • Shrinivas Kattimani,

    You need no to know N, E, W, S directions for calibration. The horizontal plane is stationary and (picture 8) must be fixed relative to the Earth plane.

    The box is not stationary, and magnetometer is attached (by glue in my case) to box.

    Have you other questions?

This reply was deleted.