Wiring and Test

This 3-axis magnetometer breakout is thankfully very easy to use. It only requires 2 power pins and 2 data pins.
We'll be demoing it with an Arduino but any other microcontroller can be used if you are willing to port the I2C code. We suggest using an Arduino to start since its guaranteed to work

  • Connect VIN to 5V pin on the Arduino. If you have a 3V logic Arduino, connect VIN to 3V instead
  • Connect GND to the ground pin on the Arduino
  • Connect the SDA pin on the breakout to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
  • Connect the SCL pin on the breakout to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
The HMC5883L has a fixed I2C address, you can only connect one sensor per microcontroller!

Download & Install Libraries

Lucky for you we have written a very nice sensor library for the HMC5883L, which is based off of our Adafruit_Sensor framework library.

Download Adafruit_Sensor

To begin, you will need to download Adafruit_Sensor from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip

Rename the uncompressed folder Adafruit_Sensor and check that the Adafruit_Sensor folder contains Adafruit_Sensor.h

Place the Adafruit_Sensor library folder your arduinosketchfolder/libraries/ folder.
You may need to create the libraries subfolder if its your first library. Restart the IDE.

We also have a great tutorial on Arduino library installation at:

Download Adafruit_HMC5883

Next you can download the Adafruit HMC5883 "Unified" library. The "Unified" means that it is based on Adafruit_Sensor and will give you normalized values back in microTeslas instead of arbitrary numbers from 0 to 1023 or whatever is used internally. Again, either visit the github repo or, better, download by clicking below
Rename the uncompressed folder Adafruit_HMC5883_U and check that the Adafruit_HMC5883_U folder contains Adafruit_HMC5883_U.cpp and Adafruit_HMC5883_U.h

Place the Adafruit_Adafruit_HMC5883_U library folder your arduinosketchfolder/libraries/ folder as you did with Adafruit_Sensor

Finally, restart the IDE!

Load Demo

Open up File->Examples->Adafruit_HMC5883_U->magsensor and upload to your Arduino wired up to the sensor

And open up the serial console. You will see some quick spec's about the HMC5883 and then a stream of data with X Y Z magnetometer data in uTeslas and then a calculated heading. If you hold the module so that it is parallel with the ground then it will point you in the direction of North!
If you have to mount the module so that some other axis is parallel with the ground (X or Y) you will have to modify the heading code to take the arcTan of X&Z or Y&Z instead of X&Y
This guide was first published on Feb 28, 2014. It was last updated on Oct 17, 2018. This page (Wiring and Test) was last updated on Feb 01, 2016.