Tag Archive for 'i2c'

HMC5843 Magnetometer Library for Arduino

HMC5843 and Arduino

I (finally) have a project taking up the idle cycles of my brain, the first step of which involves figuring out how to use a magnetometer.  The project will eventually use the digital compass, accelerometers, perhaps a gyro, and maybe absolute forms of positioning like an IR camera.  I’m being slightly vague about this project both because the idea is by far the coolest thing I’ve ever come up with and because it is still somewhat short of half baked.

Anyway, Honeywell recently released a rather reasonably priced three axis magnetometer, the HMC5843, which SparkFun carries a breakout board for.  It interfaces over i2c, which is conveniently supported in hardware by most AVR microcontrollers, including the ones used on Arduino.  Arduino is something of paradox.  On one hard, the hardware is so simple and easy to use, vastly cutting down on the amount of time I need to spend arranging parts on a breadboard.  On the other hand, computations that should take a few operations instead call long functions that get compiled into hundreds, and the IDE makes me want to stab a stick of RAM into my jugular.  Luckily, one can mitigate the downsides by using an external editor, communicating with cutecom/minicom, and using avr-libc instead of the Arduino libraries as much as possible.  Back to the project.

The actual circuit is fairly simple.  Analog pins 4 and 5 on the Arduino serve as i2c’s SDA and SDC lines, respectively.  I’m using a level shifter from SparkFun to get the Arduino’s 5v lines down to the 3.3v that the HMC5843 is looking for.  Note that one can skip this by using a 3.3v Arduino Pro.  The FTDI chip on the Arduino outputs 3.3v, which is brought out on the headers, allowing the level shifter and the magnetometer to be powered off the Arduino.

I tried using the Arduino Wire library for i2c communication, but had no luck.  Atmel made TWI, the i2c implementation on the AVR, fairly easy to use, so I read through the datasheet, looked at some examples, and wrote my own Arduino library specifically for the HMC5843.  The current implementation is absolutely alpha, but it seems to read the x, y, and z values at 10 Hz correctly.  Note that you probably can’t use this library at the same time as Wire or another i2c library, and that it also doesn’t support having multiple i2c devices connected.  Its sole purpose is interfacing the HMC5843.  Here is an example sketch using it:

#include <HMC.h>
void setup()
  delay(5); // The HMC5843 needs 5ms before it will communicate
void loop()
  int x,y,z;
  delay(100); // There will be new values every 100ms
  Serial.print(" y:");
  Serial.print(" z:");

Continuing the theme of a different license for each set of code I release, this library is under a two clause BSD style license.  Please feel free to try it out and give me feedback/suggestions/patches/pedantic advice/flames.

Download and extract into the hardware/libraries/ folder of your Arduino directory: