One of the first articles we did was on the hmc6352 digital compass. Well this is his older brother (or younger but smarter). I really don't know what to say about the HMC5883L other than it is a three axis magnetometer, so it is capable of sensing in 3 directions.

That doesn't mean that it is tilt compensated, because you really don't know how it is orientated when the reading comes in. For that, you would need to combine this with an accelerometer to do that. But this can get you one step closer. If you are looking at using this as a digital compass, make sure to read the following section.

Magnetic Declination

Something I found interesting and obvious once I thought about it, is that all compasses point to magnetic north, and not true north. The difference between them is called Magnetic declination. And how much they vary depends on where you are, and... when it is, because as weird as it is, it changes over time (awesome animation of that). You can look up your Magnetic declination here.

Hooking it up

Hooking you HMC5883L up to your arduino is pretty simple. The HMC5883L is an I2C device, a type 2-wire serial connection, so we just need to connect the SDA (Data) and SCL (Clock) lines to your Arduino for communication. On your Arduino (everything but the mega) SDA is on analog pin 4, and SCL is on analog pin 5. On an arduino mega, SDA is digital 20, and SCL is digital 21. (The Arduino Leonardo will also be different). Other than these 2 lines, we just need to connect power(3.3v), ground and we are all set.


The code for the HMC5883Lis a bit more complex than his single-axis brother, and the readings from it come in a few different forms, so we will be using a library to achieve this.

To make this code work, before you load the code, or even open the Arduino program, we need to place the "HMC5883L" folder into your Arduino Library. If you don’t know where that is by default, Look to the right.

If you click the download button to the right of “Arduino” you can download the whole thing as a zip, so you dont need to copy all the files.

Default Library Folder Location

On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box:: (home directory)/sketchbook/libraries