An HMC5843 magnetometer library for Arduino based on Wire

UPDATE 2012-07-27:

The HMC58X3 library is now part of the FreeIMU library. Please refer to the FreeIMU library page for newer code.

UPDATE 2011-01-22:

I moved the development of the HMC5843 Arduino library to Launchpad. This way we will have a proper repository, bug tracking, question/answer, etc.. You can find it here: A libre Arduino Library for the HMC5843 and HMC5883 Honeywell magnetometers

I just completed porting the HMC5843 Arduino library, originally created by Nirav Patel (his library) and then extended/improved by E.J.Muller (his library), to use only the Arduino Wire library to use the I2C bus.

I did this because I didn’t see any reason to use directly TWI given that we have the nice Wire library. Of course, this comported a ~2KB size increase in the program binary size but that’s ok because once you’ll program 2 or more I2C devices they’ll share the Wire code decreasing the total memory consumption.

Moreover, working with Wire make it possible to easily change it to something more sophisticated. For example one could use SoftI2CMaster, a library to use I2C on any pin, just including that instead of Wire.

So, you find the code below, released under GNU GPL v3. Enjoy it! If you are interested in the changes, there is a patch against E.J.Muller’s library in the attachments below. A part for using Wire I also did some code polishing and bug fixing. See the patch for any detail.

For any issue, question, bug report, patch, etc.. please leave a comment below.

NOTE: I still did not carefully investigated the issue of calibrating the HMC5843. The library contains a routine for that but I can’t guarantee on its behavior.

Connecting the HMC5843 to Arduino

If you never connected any I2C device to Arduino, here is a picture of how to connect the HMC5843 to the Arduino.

The resistors are 2.2K Ohm, the power is sourced from the Arduino 3.3V pin.

In this example I did not used a Logic Level Converter/Translator such as Sparkfun’s one. As the Arduino is a 5V device and the HMC5843 is a 3V one, using a Logic Level Converter would be the correct way to connect the two devices.

AttachmentSize
_11_hmc5843_cal_wire_20101201.tar_.gz17.3 KB
HMC_cal_to_FabioVaresano_HMC5843_cal_wire.patch8.27 KB
Scroll to Top