Troubleshooting HMC5883 sensor

Dear all!
I need your help. I bought a HMC 5883 sensor for getting the heading. I tried several example codes (I attached one), but I am not able to get the right heading. When uploading the sketches to the Arduino UNO, the arduino reads the sensor informations only once via I2C, but it should read it all the time. The serial monitor only shows the first heading-values, but when it repeats this value all the time.
I thank you for your time spent.

#define address 0x1E //0011110b, I2C 7bit address of HMC5883
#include <Wire.h>
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);  
}

The code you posted simply reads the raw data, not headings. Do the values change as you reorient the sensor?

In any case, you do not need the Wire.Available() test, because Wire.requestFrom() is a complete transaction. Try this modification:

#define address 0x1E //0011110b, I2C 7bit address of HMC5883
#include <Wire.h>
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);
    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);  
}

To get headings, the sensor must be calibrated. This is the best calibration tutorial: https://thecavepearlproject.org/2015/05/22/calibrating-any-compass-or-accelerometer-for-arduino/

Thank you for your modification. Unfortunately it does not change my problem that the values do not change than I reorient the sensor.

There is nothing obviously wrong with the code, so that leaves the sensor itself, or intermittent wiring as the possible problem.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.