HMC5883L/GY-273 giving me wrong heading

The issue:

Hello I recently bought a HMC5883L/GY-273 and I am getting the wrong heading it feels random if I turn it 180 degrees it has the same issue it doesn't go down or up by 180 degrees, I tried multiple scripts that I saw on the internet and it still doesn't work

I am new to magnetometers and compasses and i am still trying to understand it

And is there a way to save the magnetic deviation on my sd card and the arduino will load it based on lat and lon?

What I know:

  1. I have a real HMC5883L as I have the same writing on the ic that I saw on a forum of a real and fake HMC5883L, and the QMC5883L code doesn't work
  2. The x on the silkscreen needs to be directed to the north which I did
  3. I am sitting in my office and there shouldn't be any interference(or at least that is what I read) And I tried moving the Arduino and still had the same issue just random heading

This is the output this is from beginning to BREAK(i stopped it)

#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>

// Create a compass
HMC5883L_Simple Compass;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
    
  // Magnetic Declination is the correction applied according to your present location
  // in order to get True North from Magnetic North, it varies from place to place.
  // 
  // The declination for your area can be obtained from http://www.magnetic-declination.com/
  // Take the "Magnetic Declination" line that it gives you in the information, 
  //
  // Examples:
  //   Christchurch, 23° 35' EAST
  //   Wellington  , 22° 14' EAST
  //   Dunedin     , 25° 8'  EAST
  //   Auckland    , 19° 30' EAST
  //    
  Compass.SetDeclination(5, 13, 'E');  
  
  // The device can operate in SINGLE (default) or CONTINUOUS mode
  //   SINGLE simply means that it takes a reading when you request one
  //   CONTINUOUS means that it is always taking readings
  // for most purposes, SINGLE is what you want.
  Compass.SetSamplingMode(COMPASS_SINGLE);
  
  // The scale can be adjusted to one of several levels, you can probably leave it at the default.
  // Essentially this controls how sensitive the device is.
  //   Options are 088, 130 (default), 190, 250, 400, 470, 560, 810
  // Specify the option as COMPASS_SCALE_xxx
  // Lower values are more sensitive, higher values are less sensitive.
  // The default is probably just fine, it works for me.  If it seems very noisy
  // (jumping around), incrase the scale to a higher one.
  Compass.SetScale(COMPASS_SCALE_130);
  
  // The compass has 3 axes, but two of them must be close to parallel to the earth's surface to read it, 
  // (we do not compensate for tilt, that's a complicated thing) - just like a real compass has a floating 
  // needle you can imagine the digital compass does too.
  //
  // To allow you to mount the compass in different ways you can specify the orientation:
  //   COMPASS_HORIZONTAL_X_NORTH (default), the compass is oriented horizontally, top-side up. when pointing North the X silkscreen arrow will point North
  //   COMPASS_HORIZONTAL_Y_NORTH, top-side up, Y is the needle,when pointing North the Y silkscreen arrow will point North
  //   COMPASS_VERTICAL_X_EAST,    vertically mounted (tall) looking at the top side, when facing North the X silkscreen arrow will point East
  //   COMPASS_VERTICAL_Y_WEST,    vertically mounted (wide) looking at the top side, when facing North the Y silkscreen arrow will point West  
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  
}

// Our main program loop.
void loop()
{
   float heading = Compass.GetHeadingDegrees();
   
   Serial.print("Heading: \t");
   Serial.println( heading );   
   delay(1000);
}

Thank you

Magnetometers must be calibrated before use as a compass, at the very least to remove the offsets.

The best overview and tutorial on calibration is Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Thank you,I will look into it and update you if it works and is there any way to load the magnetic declination based on my GPS location from an sd card?

Yes, a file on an SD card can contain any type of data table.

Like can I get the data to a specific country? If so can you provide me a link or where I can get the data?

A web search for "magnetic declination tables" turns up lots of interesting possibilities.

Add the name of the country of interest. There is also the World Magnetic Model.

The World Magnetic Model (WMM) is the standard model for navigation, attitude, and heading referencing systems using the geomagnetic field. Additional WMM uses include civilian applications, including navigation and heading systems

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