Sparkfun: HMC5883L Magnetometer

Overview
The [https://www.sparkfun.com/products/10530? HMC5883L Magnetometer] is a sensor that measures the magnetic field in three dimensions. the Magnetometer uses a simple I2C bus to communicate. The breakout board comes with filtering capacitors and four pins for the I2C, Vcc and ground.

Connecting to the Bone
Ground and Vcc on the breakout board should be connected to pins 1 and 2 respectively on the bone's P9 header, and the SCL and SDA pins should be connected to one of the I2C pairs on the bone. 4.7k resistors should be connected between SCL and Vcc and between SDA and Vcc. I used I2C3 (P9, pins 19 and 20). The address of the magnetometer can be found by using the i2cdetect command from the shell. I get: beagle$ i2cdetect -y -r 3 0 1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- 1e -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

Communicating with the HMC5883L
the HMC5883L has 13 registers:

By default the magnetometer takes a single sample and enters idle mode.The lowest two bits of the mode register control the measurement mode. Setting these to 0x01 will cause the device to take one measurement and then enter idle mode again.

beagle$ i2cset -y 3 0x1e 2 1

The values for each axis can be read from registers 03-08 as shown above. By default the measurements correspond to 1090 LSB/Gauss, but this can be changed by modifying the value in Config Register B.

Note: reading a value of 0xF000 (-4096) in a data output register indicates an overflow or underflow error in the ADC.

Using C
the I2C code from exercise 12 should work for the magnetometer, but it produces an error trying to read from or write to the device. This is probably because the magnetometer I2C operates at 400Khz and the bone uses 100kHz, so anything other than the simple shell commands produces currupted dat due to timing errors.