Hi, i have bought a bunch of HMC5883L magnetometers and I wonder whether I can use them in order to measure rotational angles. I don't care about orientating (e.g. "facing north") but more like "i have rotated X degrees since i started measuring". Not sure how these sensors' physics work exactly, so please forgive me if my question sounds off or ignorant.
Also, examples with code will be even more appreciated!
Thanks!
A magnetometer is useful for "facing 123 degrees from north". For "rotating at 0.987 radians per second" you would use a gyro.
Depending on your speed and accuracy requirements, one or the other might do the job but you'll probably need both.
Thanks for your answer. Can you elaborate on why would I need both since I just need the degrees? Or how could I use both in order to improve accuracy? Wouldn't in theory the degrees from north be enough in order to figure out the rotation? My error margin can be ± 5 degrees from the intended value and this will be used on a small remotely controlled car. Don't get me wrong, I have some nice single axis analog gyros that I'm looking forward to use in a project, however I'd rather keep this one as simple as possible.
P.S. right now I can't understand the data I'm getting from from the sensor (magnetometer) which is in the form of x,y,z positive and negative values. I'd be grateful for any hints on how to interpret them!
Without seeing the code you are using to get the x,y,z we can't help much with interpreting.
But yes, you could use magnetometer to measure the angle by which you turned. Use a variable to store the initial direction, execute the turn and take a reading of new direction. Once you take care of the wrapping over 360 heading, you should be able to easily calculate the angle.
My apologies for not presenting the code, but I was on my phone in the tram.
So here it is:
#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
//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);
delay(250);
}
The code is not mine, all credits go to Jordan McConnell of SparkFun Electronics.
Shpaget:
But yes, you could use magnetometer to measure the angle by which you turned. Use a variable to store the initial direction, execute the turn and take a reading of new direction. Once you take care of the wrapping over 360 heading, you should be able to easily calculate the angle.
I thought of doing that, however the numbers i was getting on the X axis did not make a lot of sense. In a certain direction/position i would (usually) get the same results but then rotating it 90 degrees at the time, did not give me even (or almost even) difference in my readings. But i should definitely look into that more.
Hi,
Probably you can have a look at this link.
note:- I am answering this question so that other people who come here looking for a solution can get something useful.
The key to making a compass is to use the atan2() function with the X and Y (horizontal) components of the magnetic field.
heading_in_degrees = 180*atan2(magy,magx)/PI;
However, you must calibrate the magnetometer before it can be used as a compass. Here are a couple of tutorials: