Why this HMC5883L just output fixed number?

The HMC5883L output just one fixed value, no matter how to move the board. the value vary every time power on, why?

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void setup(void)
{
  Serial.begin(9600);

  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);


}

void loop()
{
  sensors_event_t event;
  mag.getEvent(&event);
  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;
  if (headingDegrees == 0 || headingDegrees == 360 ) {
Serial.println("N");
}
if (headingDegrees > 0 && headingDegrees < 90 ) {
Serial.print("N");
// Degree from north towards east direction
Serial.print(headingDegrees);
Serial.write(176);
Serial.println("E");
}
if (headingDegrees == 90) {
Serial.println("E");
}
if (headingDegrees > 90 && headingDegrees < 180 ) {
// Degree from south towards east direction
headingDegrees = 180 - headingDegrees;
Serial.print("S");
Serial.print(headingDegrees);
Serial.write(176);
Serial.println("E");
}
if (headingDegrees == 180) {
Serial.println("S");
}
if (headingDegrees > 180 && headingDegrees < 270 ) {
// Degree from south towards west direction
headingDegrees = headingDegrees - 180;
Serial.print("S");
Serial.print(headingDegrees);
Serial.write(176);
Serial.println("W");
}
if (headingDegrees == 270) {
Serial.println("W");
}
if (headingDegrees > 270 && headingDegrees < 360 ) {
// Degree from North towards west direction
headingDegrees = 360 - headingDegrees;
Serial.print("N");
Serial.print(headingDegrees);
Serial.write(176);
Serial.println("W");
}
delay(500);
}

Where did you get the module? Post a link if relevant.

The HMC5883L was discontinued long ago, and many new modules have the inferior QMC5883 knockoff chip, but are mislabeled. A different Arduino library is required for that chip.

1 Like

Thanks.
I bought the board from Amazon long ago. the link just shown the same parts, not the same supply.

The board can be output real time number when move it with other sketch.

mag.begin is missing.
See the example: https://github.com/adafruit/Adafruit_HMC5883_Unified/blob/master/examples/magsensor/magsensor.ino

1 Like

Great!
Thank you.

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