Go Down

Topic: Converting Magnetometer raw values to usable values? (Read 2169 times) previous topic - next topic



I have used HMC588l magnetometer for my quadcopter project where I will have to calculate yaw angle for the stabilization and I have planned to use complementary filter algorithm for accurate angles

In this equation I need to use Yaw compass values, Actually I am getting raw values from my Hmc 588l magnetometer, I am not certain about what values should the raw value be converted so that I can use it in this equation. The actual raw values from my magnetometer is attached
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

#include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  //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

void loop(){
  int x,y,z; //triple axis data

  //Tell the HMC5883 where to begin reading data
  Wire.write(0x03); //select register 3, X MSB register
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
    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("  y: ");
  Serial.print("  z: ");


The basic equation is angle = atan2 (y, x)  (for mathematical angle in radians)
angle = 180 / pi * atan2 (x, y)  - for clockwise bearing from north

[though that's in global coords, local coords are probably reversed angle]

magnetometers need calibrating to remove hard/soft iron errors, google about
this I suggest...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


That also assumes that the measurement is made with the device in a horizontal position.

Go Up