HMC5883L Magnetometer axis Problem!!

Hi all,
I have a problem with my magnetometer. If I rotate it left or right it measure normally degrees but it I turn it on x axis for example degrees changes why this happens?
Thanks

Please read and follow the directions in the "How to use this forum" post.

Try reading the instructions again. With 92 posts, you should know better.

What do you mean?

What part of "Please read and follow the instructions in the How to use this forum" are you having trouble understanding?

Just I can't understand what's wrong with my post

You have a problem.

You don't give a clear explanation, with details, of what the problem is.

You ask for help on the Arduino forum, but you don't give any details about how an Arduino is involved in this problem.

The Arduino forum provides clear guidelines on what to do when you are asking for help, yet you refuse to read those guidelines and follow the directions.

Ok sorry, you are right. Just I can't explain it with words so I make this scheme

Without knowing what program you're using, we can only guess what's causing the problem.

It's likely being caused by the 3D nature of the Earth's magnetic field.

The HMC5883L needs special algorithms and an accelerometer to be able to determine heading when tilted.

thanks for your reply… I’m using this algorithm.

 #include <Wire.h>
#include <HMC5883L.h>
#include <Servo.h>
Servo servo;
HMC5883L compass;
int valy, valx;
void setup()
{
  Wire.begin();
  Serial.begin(38400);
  Serial.println("Serial started.");
 servo.attach(3);
  compass = HMC5883L();
 
  Serial.println("Setting scale to +/- 1.3 Ga");
  int error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));

  Serial.println("Setting measurement mode to continuous.");
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
}

void loop()
{
   // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
 
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(raw.YAxis, raw.XAxis);
   
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI;

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);
}

void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw:\t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("   \tScaled:\t");
   
   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");

DimitrisTzam:
thanks for your reply… I’m using this algorithm.

  // Calculate heading when the magnetometer is level, then correct for signs of axis.

float heading = atan2(raw.YAxis, raw.XAxis);

As noted in the code, the algorithm only works when the magnetometer is level.

In many places in the world there is a strong vertical component to the Earth’s magnetic field (it’s closer to vertical than horizontal here in Idaho). This vertical component will wreak havoc on the heading when the sensor is tilted.

Also, that algorithm won't work well or at all if the magnetometer is not properly calibrated. You need to determine an offset and scale for each axis.

Adafruit has a guide to calibration, but there are others on the web.

I have already calibrate my magnetometer also is there any algorithm that will write degrees without affected from tilt?

I’m pretty sure you need an accelerometer in order to determine the amount of tilt. There will likely be a relatively complex algorithm to determine a tilt compensated heading.

How exactly I could measure degrees with accelerometer?

This application note describes how to implement a tilt-compensated compass.