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
Lucky for you we have written a very nice sensor library for the HMC5883L, which is based off of our Adafruit_Sensor framework library.
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:
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
Place the Adafruit_Adafruit_HMC5883_U library folder your arduinosketchfolder/libraries/ folder as you did with Adafruit_Sensor
Finally, restart the IDE!