Skip to content

Instantly share code, notes, and snippets.

@fbelleau
Created November 11, 2017 23:15
Show Gist options
  • Select an option

  • Save fbelleau/2535d1abfb4b57617c05ef40bb5cedc8 to your computer and use it in GitHub Desktop.

Select an option

Save fbelleau/2535d1abfb4b57617c05ef40bb5cedc8 to your computer and use it in GitHub Desktop.
i2c-hmc5883
// http://arduino.bucki.pl/hmc5883-gy-273-test/
/*
An Arduino code example for interfacing with the HMC5883
by: Jordan McConnell
SparkFun Electronics
created on: 6/30/11
license: OSHW 1.0, http://freedomdefined.org/OSHW
Analog input 4 I2C SDA
Analog input 5 I2C SCL
*/
/*
* adopted to Wire library version distributed with Arduino IDE 1.0 and later
* where send() and receive() have been replaced with read() and write(). ( by
* Janusz Bucki November 2015
*/
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){
int x,y,z; //triple axis data
int xmin,xmax,ymin,ymax,zmin,zmax;
xmin=0; xmax=0; ymax=0; ymin = 0; zmin=0;zmax=0;
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
//Print out values of each axis
//Serial.print("x: ");Serial.print(x);Serial.print(" y: ");Serial.print(y);Serial.print(" z: ");Serial.println(z);
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(y, x);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.22;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment