Go Down

Topic: Calibration of a HMC5843 compass (Read 5564 times) previous topic - next topic


Feb 12, 2011, 03:08 pm Last Edit: Feb 12, 2011, 03:10 pm by FraesmeisterFinn Reason: 1
Hello Arduino Community,
We have build a roboter-car which shall automatically turn to one direction (e.g. north) and drive into that direction. While it is driving into this direction it shall always adjust the measurement of the HMC5843 with the given value (e.g north or 0 degree).
We use this code to get the values from the HMC

Code: [Select]

#include <HMC.h>

float dir;
const float pi = 3.14159265;

void setup()  {
  delay(5); // The HMC5843 needs 5ms before it will communicate

void loop()
  int x,y,z,xb,yb,quadrat;
  delay(2000); // There will be new values every 100ms

  dir = 0;
  dir = atan2(float(y),float(x))*180/pi;


But the problem is, that the values that are given by the compass aren't approxamitly exact.
If I turn the compass by 90° the value given by the HMC changes by 70° or 100° or 110°.
I read about calibrating the compass to get more exact values. But I don't know how to do it.
Has somedoy of you a calibrating-code for the HMC?
Or has somebody an idea how to do it?


Do you have a link to the HMC library you are using?

have you read - http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC5843.pdf ?

It seems that there are diiferent modes to operate, don't know what the library is using?

On page 19 there is some info about a selftest?

hopes this helpes.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Thank you for your quick answer robtillaart

I have the HMC library from this page: http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
I have read the document. Maybe I can try it with this selftest but I don't know if this will calibrate the HMC.
I thought about a code from somebody how has got the same problem.

In this thread was a discussion about it, but it ended without a result and it is closed now:

I will try the self test on monday, if there are no other ideas how to calibrate it


From - http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino

I'm using a level shifter from SparkFun to get the Arduino's 5v lines down to the 3.3v that the HMC5843 is looking for. 

Do you use the proper 3.3V?  I can imagine that 5V can cause errors (from experience with another device and other voltage level)

Also from the URL above:
I tried using the Arduino Wire library for i2c communication, but had no luck.  Atmel made TWI, the i2c implementation on the AVR, fairly easy to use, so I read through the datasheet, looked at some examples, and wrote my own Arduino library specifically for the HMC5843.  The current implementation is absolutely alpha, but it seems to read the x, y, and z values at 10 Hz correctly.  Note that you probably can't use this library at the same time as Wire or another i2c library, and that it also doesn't support having multiple i2c devices connected.  Its sole purpose is interfacing the HMC5843.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Calibration of Compass:

1) Get a high accuracy GPS and calibrate against that...
2) "Swing" the Compass and find magnetic north...
3) You will need a topographical / navigation map for your location if you like method 2

Marine maps are great if you are near the water as they usually have the compass rose.

Get the navigation/topical map.
Find the rose or the magnetic declination at the time the map was printed.
Note the "Rate of Change per Year" -- whatever it is called.
Calculate where magnetic North is this year.
Now you know the deviation between true North and Magnetic North.

These maps are often in UTM (square grids) with a local DATUM and with the lat and long as a secondary measurement. They should still have some description of how to find magnetic north -- this years.

Your GPS is usually WGS84 and can often be set to the DATUM of any map.

I mention the DATUM business because it is easy to lose confidence in the map if measurements don't make sense.

Plus I don't know what your knowledge level is on maps and magnetic vs true north and how to "swing" a compass.

See marine Navigation/Power squadron for dead reckoning and "how to swing a compass" (google that).

Just so you don't think I'm "nuts"


There is one link for aircraft one for boating...  :)


Or have a peek at this version of the HMC-lib.


After picking up my robot project again (or V2 for that matter) i'm checking the calibration again, setting the Data Output Rate slower also helps to make it more accurate.

There was also a discussion about getting more than a heading from it, it's basicly very simple, calculate a heading, same way calculate tilt (or azimuth and elevation).

Go Up