First steps with the HMC5843 with Arduino: verify the accuracy of its results

Last updated on Mon, 2010-11-29 20:22. Originally submitted by fabio on 2010-11-24 14:04.

I finally got an HMC5843 magnetometer breakout board. I wasn't able to make a DIY breakout board for this chip (but I'm still willing to try that again) so I had to buy one. Of course I ended up with one made by Sparkfun sold by

HMC5843 breakout board from sparkfun

So now I'm moving my first steps with this chip. I'm now verifying the accuracy of its readings. I found a nice Arduino library on Arduino forum which contains a calibration routine (there is a copy in the attachments below). It looks like it simply uses an internal calibration feature. I'm still not sure on how this actually works.

However I ended up plotting some graphs from the readings from the chip while rotating it around the Z axis and while moving it in 3d rotations. Ideally, in a perfectly calibrated device these graphs should be respectively a circle and a sphere centered in the origin.

These are the resulting pictures:

X and Y readings rotating the HMC5843 around the Z axis:
X and Y readings rotating the HMC5843 around the Z axis

X, Z and Y readings rotating the HMC5843 in 3D:
X, Z and Y readings rotating the HMC5843 in 3D

As you can see the picture are not perfect circle and spheres so a more complex calibration routine its probably needed to get very precise readings.

I still don't know if the precision I'm currently getting would be enough or not for my needs. I asked for clarification on a Sparkfun forum thread on the topic but I still didn't got a reply.

Thanks to newbee on Arduino forum I found this nice paper by ST Microelectronics called Using LSM303DLH for a tilt compensated electronic compass which describe quite well a procedure to calibrate a magnetometer (it actually covers tilt compensating a magnetometer with an accelerometer, so it might came handy in further developments).

I attach here the gnuplot "programs" I implemented to create the above graphs. If you are working with the HMC5843 they may come useful.

makeplot3d.p reads data from a datafile. It expects data to be comma separated in the X,Y,Z form

makeplot.p instead expect a datafile with space as separator.

I created the dumps of the readings using the attached test_serial.py_.txt python script. You will need PySerial installed on your system to use it.

Save it, rename it to then simply run python > dump.txt so you will have all the values coming from the Arduino in the dump.txt file ready to be used by gnuplot.

EDIT: On Sparkfun forum user pvmellor asked what typical variance and standard deviations we get from the HMC5843. So, I created a simple octave (matlab) script to compute the std and variance from a csv file containing (not so) calibrated readings from the HMC5843 (see above).

My results, of a 3 minutes dump ( of the readings coming from the Arduino code _11_hmc5843_calibrazione.tar_.gz while keeping the device steady, computed with the Octave script HMC5843_statistics.m were (X Y Z):


    8.8842    5.5359   15.1949


    78.929    30.646   230.885
makeplot3d.p797 bytes
makeplot.p497 bytes
test_serial.py_.txt349 bytes
HMC5843_statistics.m292 bytes
HMC5843_steady.data32.52 KB
_11_hmc5843_calibrazione.tar_.gz3.6 KB
HMC5843_breakout_board_sparkfun.jpg99.64 KB
Posted in:

I cant read the arduino

Submitted by Anonymous (not verified) on Fri, 2013-02-01 20:31.

I cant read the arduino calibrating code when I download it. Can sumone post it? And is that the only code I need to calibrate the Magnetrometer

FreeIMU Lib has this calibration feature, how do i use it?

Submitted by PK (not verified) on Fri, 2012-04-20 18:14.


I saw that in FreeIMU.cpp in the init function you call the magnetometer calibration function.
I also saw the HMC58X3 examples and there is a calibration sketch.

Do i need to do something with the values I see after running calibration (from HMC58X3_testcal sketch)?

Also, the calibration function takes in a parameter: gain.
What is the effect of increasing the gain?

Lastly, the only way to know if my sensor is calibrated is to plot and look for 2D circles or 3D spheres?




Submitted by Michael (not verified) on Wed, 2011-04-06 07:29.

Thanks for posting this rundown on compass calibration. I've been struggling to get some magnetometers working to a reasonable degree (heh).

I was able to collect x,y,z data for a LSM303DLH, plot it with gnuplot as you did, and used a pretty simpleton kind of approach for calibration.

My plots didn't show any soft iron / tilt, so I just computed offset and scale from the min/max values of each axis.

I haven't done extensive testing to see how accurate the compass is, now, but it appears to behave quite a bit better than it did.

I know it's a pretty cheesy approach but until I get a better handle on linear algebra and 3d fitting, that's about all I can pull off :)