1
#include <Wire.h> // I2C Arduino Library
3
#define address 0x1E // 0011110b, I2C 7bit address of HMC5883
7
// initialize Serial and I2C communications
11
// put the HMC5883 IC into the correct operating mode
12
Wire.beginTransmission(address); // open communication with HMC5883
13
Wire.write(0x02); // select mode register
14
Wire.write(0x00); // continuous measurement mode
15
Wire.endTransmission();
20
int x, y, z; // triple axis data
22
// tell the HMC5883 where to begin reading data
23
Wire.beginTransmission(address);
24
Wire.write(0x03); // select register 3, X MSB register
25
Wire.endTransmission();
28
// read data from each axis, 2 registers per axis
29
Wire.requestFrom(address, 6);
30
if(6<=Wire.available()){
31
x = Wire.read()<<8; // X msb
32
x |= Wire.read(); // X lsb
33
z = Wire.read()<<8; // Z msb
34
z |= Wire.read(); // Z lsb
35
y = Wire.read()<<8; // Y msb
36
y |= Wire.read(); // Y lsb
39
// print out values of each axis