Problems with HMC5883L

Hello everyone. Very excited to be here .I hope this is the right place .
I have two HMC5883L sensors . First one is not working at all . I think because I soldered it wrong way .
Second one is working but I think something is wrong .No matter which side I am pointing compass I am getting "dummy " result . Something like this.

X: -1.09  Y: -0.45  Z: -417.96  uT
Heading (degrees): 215.22
X: -0.91  Y: -0.55  Z: -417.96  uT
Heading (degrees): 223.57
X: -0.73  Y: -0.36  Z: -417.96  uT
Heading (degrees): 219.17
X: -0.64  Y: -0.18  Z: -417.96  uT
Heading (degrees): 208.55
X: -1.09  Y: -0.36  Z: -417.96  uT
Heading (degrees): 211.04
X: -0.45  Y: -0.55  Z: -417.96  uT
Heading (degrees): 242.80
X: -0.91  Y: -0.55  Z: -417.96  uT
Heading (degrees): 223.57
X: -0.73  Y: -0.73  Z: -417.96  uT
Heading (degrees): 237.61
X: -1.00  Y: -0.45  Z: -417.96  uT
Heading (degrees): 217.05

I have tried multiple libs. but result is the same .
this is the code .

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT"); 
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
 
  /* Initialise the sensor */
  if(!mag.begin())
  {
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
   displaySensorDetails();
}
void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.22;
  heading += declinationAngle;
  if(heading < 0)
    heading += 2*PI;
  if(heading > 2*PI)
    heading -= 2*PI;
  float headingDegrees = heading * 180/M_PI;
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  delay(800);
}

I guess you're doing these tests indoors. That's the normal behavior as indoors there are too many metal parts that influences the compass sensor. Try it outdoors at least 50m from the next building. Does it show the same results then?

Thanks for response .
Yes I am testing it at home . I assumed something is not right .
So it is ok I jsut need to go out and test it outside.
Thanks I almost lost my hope :slight_smile:

You must always calibrate a magnetometer, before attempting to use it as a compass. They are useless out of the box. Calibration overview here.

Please do not double post.

Sorry for making it double. I panicked :slight_smile: . Thanks for help.

Hi,

You don't have to go outside. In an ordinary house, you can do calibration. I did it, and the sensor work nicely.

You have to take readings at 0°,90°,180° and 270°. Base on these numbers you have to find a correction factor to include in the formula.

For my sensor, I have to use the following correction factor for "Y" (+10):

float heading = atan2(y + 10, x);

Of course, every sensor will have a different correction factor for Y or X.

Of course, every sensor will have a different correction factor for Y or X.

If you want any accuracy at all, there are 6 correction factors for a 3D compass.

Thanks everyone for responses

You have to take readings at 0°,90°,180° and 270°. Base on these numbers you have to find a correction factor to include in the formula.

The point is I am getting only results close to ~ 214 not below 200 and not above 240 .
correction factor still applies to my problem ?
Thanks

correction factor still applies to my problem ?

No, but proper calibration would apply.

Hi,

The code you posted look like it is incomplete.

In my opinion, the following lines are missing:

int x, y, z;

void loop(void)

{ etc.

mag.getEvent(&event);this one is OK

x = event.magnetic.x;
y = event.magnetic.y;
z = event.magnetic.z;

etc.
}