Normalised HMC5883L Compass

Fueling off my desire for wearable navigation, I’ve decided to try and use the simple HMC5883L magnetometer module to create a simple non tilt-compensated compass. As the name suggests, it currently only works on flat surfaces and tilting it will give a pretty erroneous result! None the less, on a flat surface it pretty much matches the quality of the compass within my phone (Sony Xperia M) so its nice to know I’m getting somewhere!

Testing against the inbuilt compass in my phone

The same picture just at my phones angle!

Its hard to see from the image but the line which says “Ang: ” states that the current angle to north (with respect to the compass modules X axis) is at 359deg (or -1degree depending on how you perceive it!) As can be seen on my phone too, the direction of north corresponds to the X direction of the module. The LCD unfortunately has a bit of a fit from the flash on my camera so sorry about not easily being able to see the LCD!

As you can see in the images above, I actually have both my phone and the board on my floor away from anything electrical. The reason for this is if I try and use it next to my laptop, the magnets within my hard drive actually have a large influence on the sensor creating a pretty erroneous result. On the floor, this problem is still present but less of an effect is measured as magnetic field strength decreases as a square law!

Obviously however, the compass module doesn’t work straight out of the box and requires calibration everytime I reprogram it. Calibration works through rotating it about the X, Y and Z axes in a point in space (preferably the point that you want to check your orientation at). I do this by grabbing the board and rotating it in every plane I can manage. This allows the program to find the maximum and minimum magnetic field strengths for that given point in each orientation. By knowing these maximum and minimum values, the current reading can then be normalised to the point where if the current magnetic field strength was equal to the minimum value, the normalised value would be zero, whereas if the current reading of the magnetic field strength was equal to the maximum value, the normalised value would be one. By using these normalised value, one can calculate the vector corresponding to north in 2D space, ensuring the sensor is kept flat.

The calibration process! Rotating the whole board in a variety of orientations

Once the sensor has been calibrated, placing it on a flat surface, one should be able to see their bearing to north and the normalised magnitude of the magnetic north vector (from 0-1000 as an integer)! All the data is being displayed on a good ol’ 128×64 UC1701X driven LCD that I got off eBay.

I now actually have a variety of accelerometers that I could use for tilt compensation, namely the good ol’ MPU-6050 and an Analog ADXL335. In the future, I hope to use the MPU-6050 and its ability to have a slave I2C device connected to it – with the slave being the HMC5883L compass, along for tilt compensation.

Until I actually write the tilt compensating code, you can find the current code on my Github!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s