HMC5883 Compass/Magnetometer

This module interfaces with an HMC5883, an I2C compass/magnetometer.

Wiring

Device Pin Espruino
Vcc 3.3
Gnd GND
SCL I2C SCL
SDA I2C SDA
DRDY Any GPIO

Setup

Setup the I2C that will be used, and then connect to the HMC5883:

var compass=require("HMC5883").connect(i2c,drdy,mode)
compass.setMode(mode)

This sets the current measurement mode.

Mode Meaning
1* Single measurement
0 Continuous measure
2 Idle
compass.setGain(gain)

Set the gain. Due to limitations of the chip, the new gain is not applied to the next measurement taken, but rather the one after that. This module handles this. In single measurement mode, a dummy measurement is taken so that the next measurement requested will have the new scale. The chip starts up in gain=1.

Gain Rec. range resolution
0 +/- 0.88 Ga 0.73 mGa
1* +/- 1.3 Ga 0.92 mGa
2 +/- 1.9 Ga 1.22 mGa
3 +/- 2.5 Ga 1.52 mGa
4 +/- 4.0 Ga 2.27 mGa
5 +/- 4.7 Ga 2.56 mGa
6 +/- 5.6 Ga 3.03 mGa
7 +/- 8.1 Ga 4.35 mGa
compass.setup(sample,dout,ms)

Set other options

sample Samples
0* 1
1 2
2 4
3 8
dout Output Rate
0 0.75 hz
1 1.5 hz
2 3 hz
3 7.5 hz
4* 15 hz
5 30 hz
6 70 hz

Reading

In single measurement mode (the default), read it with reads():

compass.reads(function(a) {console.log(a);})

The function is a callback called when the measurement is complete.

In continuous measure mode, use readc():

compass.readc()

The measurement is returned as an object with properties overflow, x, y, and z. If overflow is true, at least one of the axes overflowed. The gain should be adjusted and measurement repeated. x, y, and z are the measured field strength in milligauss. Experimentation has found that when exposed to excessively strong magnetic fields at gain=7, the sensor may return 0 or -1 (which will be multiplied by the gain factor by this module), instead of an overflow result.

Buying

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.