Problem with electronic compass

Hey guys! I am an engineering student from Taiwan. Because of school, I am trying to get angle from IC
And this is the board, combines 3 IC(Electronic compass,Gyro,Accelerometer)

But when I use the Electronic compass, which is HMC5883, the value keep on moving up and down about 5 degree even if I am holding the board

it really bothers me because what I need is more accurate value =(
Is my code(Arduino) wrong or this IC's ability problem?
many thanks :slight_smile:

void HMC5883_initialize(){
  //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 HMC5883_get_angle(){
  //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()){
    EC_x_angle = Wire.read()<<8; //X msb
    EC_x_angle |= Wire.read(); //X lsb
    EC_y_angle = Wire.read()<<8; //Y msb
    EC_y_angle |= Wire.read(); //Y lsb
    EC_pitch = Wire.read()<<8; //Z msb
    EC_pitch |= Wire.read(); //Z lsb
  }
}

21308145136453_334.jpg

Magnetic interference from nearby motors, machines, cars, wires and ground signals can cause small disturbances. Take the electronic device out of the city away from cars, into a grassy field. Maybe the noise will be less away from the city.

You can adjust the gain setting in configuration register B. Lower the gain if you can and it will help with noise amplitude. Also, in configuration register A you can set the number of data points that are averaged per reading.

hello groundfungus:
I looked for google tried that, but...I am too new to know how to adjust register
can you kindly show me how to adjust the setting in register A and B?
anyway, thanks for your reply :slight_smile: :slight_smile:

this code sets the gain to ±1.9Ga.

Wire.beginTransmission(compass); //open communication with HMC5883
  Wire.write(0x01); //select config b reg
  Wire.write(0x40); //set gain
  Wire.endTransmission();

To set up configuration register A write to 0x00 (in place of the 0x01 in above code). Consult the data sheet for the bits that you need to set or clear and send that byte in place of the 0x40 (bits MA0 and MA1 set number of samples averaged).

thanks again groundfungus:
I understand why abjusting register B by looking into the data sheet
http://www.seeedstudio.com/wiki/images/4/42/HMC5883.pdf

but can you tell me why "set gain" is 0*40?


Write out the bits in binary. The three most significant bits set gain and the five less significant bits are set to zero. To set gain at ±1.9Ga set the register to binary 0100 0000 which is 0x40 hex or if you want 64 decimal . Look a figures 10 and 11 in the data sheet. If you want gain to be ±5.5 you would send binary 1100 0000 or 0xc0 hex or 192 decimal.

It works much better now! I just found out accelerator can use this method to improve, too! can't thank you more :slight_smile: :slight_smile: