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


  • Look the comment by Alex Tim on February 24, 2015 at 3:27pm about this problem.

  • Hello guys!

    Try to use com port with number not more than 4 (for example COM1, COM2, COM3, COM4) for your device to show data in MagViewer. It seems like MagVeiwer has a bag with high number COM ports.

  • Frank, I can't offer any help but hope you get the comport issue solved. That sounds frustrating.  Too bad Yuri seems silent in this topic anymore. 

    Me, I must be dumber than I was a few years ago since I had the same problem before but can't solve it properly now.  What I did was find a processing sketch that draws a 3D plot of points just like magviewer does and then I took a wildass guess on how much to offset each axis and kept adjusting until the ellipsoid thing was pretty much centered.  That doesn't use Yuri's more sophisticated hard and soft iron solution though. 

  • As a test, I loaded the 'Arduino_Code' sketch onto my Mega, and then modified the code so that it simply reported random numbers between 1 and 1000 for the three magnetometer values, and then tried to connect MagViewer.exe to that, again without success.  I can see the values on my serial monitor, but they don't show up in MagViewer.exe.  Also, it appears that the Arduino_Code sketch will run forever when viewed on my serial monitor, but will reliably hang up with the Tx light on solid after 30-60 sec when MagViewer is running.

    My Mega is showing up a Comm 17 on my Win7 PC - could there be any issues with that?

  • Hi,

    I'm trying to use your very nice calibration toolset with the CK Devices Mongoose 9DOF magnetometer, and I can't seem to get the MagViewer program to 'see' the magnetometer data coming from the Mongoose.  I have the serial port set for 9600 baud, and I have the data coming in X,Y,Z triplets as you specified.  I can see the data on a serial monitor in my VS2013/Visual Micro IDE, but when I try to connect using the MagViewer program, nada.

    Any thoughts?



  • Yuri, Sir, I used your program a few years ago and was successful in calibrating.  I have a slightly different board this time and cant orient the sensor for anything.  When I test the results with the Arduino results sketch, all I get is a straight line along the X axis as viewed in Magviewer.  Its like its canceling itself out.  If I try to swap the axis around and recalibrate, It seems impossible to follow the diagrams correctly. 

    One thing I tried was to use the magviewer to identify the axis and direction.  If I get positive X values, then that means my X axis is pointing in the positive direction?  If I rotate 90 degrees and get positive Y values, then that is my positive Y direction?



  • hey yuri. excellent piece of work. we are using linear highly sensitive 3-axis magnetic field sensors for power system applications. I am wondering if we can use this approach to calibrate our linear sensor (HMC-1043L) for instance. I have already gone through your code. I am confused about whether we need ellipse fitting on sphere for this kind of magnetic field (from arbitrary position clean from geo-magnetic field).what are your thoughts on that. is it possible for you to make a flow chart of your code and share. what do you think if there is a linear magnetic field vector from arbitrary direction (we already remove earth magnetic field with dc filter) how efficient is your code to remove the non orthogonality of our sensor ?


  • Hello Yuri,

    I have a different baud rate(115200) and that makes it difficult to use magviewer (I have to do a lot of hand soldering to change the connections). I will try and let you know.

    Thank you very much for your feedback :)

  • Akhil Mohan,

    Use the MagViewer software for visualisation your uncalibrated or calibrated measurements to best understanding your problem. Can you place here the screenshot of your uncalibrated measurements in MagViewer?

  • Hello Yuri,

    Thanks for your immediate response, I have read this when you first mentioned. How does the magnetic inclination affects my calibration here . As per the datasheet the +z is pointing towards earth and in both cases (+z pointing down and -z pointing up) the reading from magnetometer is negative. Do I have to something before doing the calibration ?

This reply was deleted.