Hi.
I'm trying to use a magnetometer HMC5883L.
Read and re-read a lot of tutorias an I get the north pole, but the sensor show me some wrong data.
The north is correct, show me 180 degree, but the rest of point is little strange.
Follow a graphic with the data that I receive (logando is my data):
The code is:
#define HMC5883_Address 0x1E
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand (uint8_t)0x00
#define HMC5883_DataOutputXMSBAddress 0x03
int regbdata = 0x40;
float gauss = 2.5;
int outputData[6];
int xCompass, yCompass, zCompass, regb;
void compass_setup(){
if(gauss == 0.88){
regb = 0x00;
}else if(gauss == 1.3){
regb = 0x01;
}else if(gauss == 1.9){
regb = 0x02;
}else if(gauss == 2.5){
regb = 0x03;
}else if(gauss == 4.0){
regb = 0x04;
}else if(gauss == 4.7){
regb = 0x05;
}else if(gauss == 5.6){
regb = 0x06;
}else if(gauss == 8.1){
regb = 0x07;
}
Wire.beginTransmission(HMC5883_Address);
Wire.write(regb);
Wire.write(regbdata);
Wire.endTransmission();
delay(1000);
}
void compass_loop(){
Wire.beginTransmission(HMC5883_Address); //Initiate a transmission with HMC5883 (Write address).
Wire.write(HMC5883_ModeRegisterAddress); //Place the Mode Register Address in send-buffer.
Wire.write(HMC5883_ContinuousModeCommand); //Place the command for Continuous operation Mode in send-buffer.
Wire.endTransmission(); //Send the send-buffer to HMC5883 and end the I2C transmission.
Wire.beginTransmission(HMC5883_Address); //Initiate a transmission with HMC5883 (Write address).
Wire.requestFrom(HMC5883_Address,6);
if(6 <= Wire.available()){ // If the number of bytes available for reading be <=6.
for(int i=0;i<6;i++){
outputData[i] = Wire.read(); //Store the data in outputData buffer
}
}
xCompass = outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
zCompass = outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
yCompass = outputData[4] << 8 | outputData[5]; //Combine MSB and LSB of Y Data output register
getDegrees();
}
float getDegrees(){
double angle = atan2((double)yCompass,(double)xCompass) * (180 / PI) + 180;
Serial.print("angle: ");
Serial.println(angle);
return angle;
}
Anybody get this problem any time?
Thanks.