HMC5883L Sensor problem

I have an HMC5883L Sensor.
I am trying to get the data from it to serial monitor and to an LCD display.
I get in three values x,y,z a 0 as result.
I use an arduino UNO R3 and the connections are:

VCC to 5V
GND to GND
SDA to A4
SCL to A5

The example code i use

#include <Wire.h> //I2C Arduino Library
#include <LiquidCrystal.h> //LCD Library
#define addr 0x0D //I2C Address for The HMC5883

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
  
  Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.print("HMC5833L COMPASS SENSOR BEGIN");
  Serial.println();
  Wire.begin();
  
  
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
  Wire.endTransmission();
}


void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC what regist to begin writing data into
  Wire.beginTransmission(addr);
  Wire.write(0x03); //start with register 3.
  Wire.endTransmission();
  
 
 //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //MSB  x 
    x |= Wire.read(); //LSB  x
    z = Wire.read()<<8; //MSB  z
    z |= Wire.read(); //LSB z
    y = Wire.read()<<8; //MSB y
    y |= Wire.read(); //LSB y
  }
  
  // Show Values
  lcd.clear();
  lcd.print("X:");
  lcd.setCursor(2, 0);
  lcd.print(x);
  lcd.setCursor(8, 0);
  lcd.print("Y:");
  lcd.setCursor(10, 0);
  lcd.print(y);
  lcd.setCursor(0, 1);
  lcd.print("Z: ");
  lcd.setCursor(2, 1);
  lcd.print(z);
  
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();

  delay(1000);
}
#define addr 0x0D //I2C Address for The HMC5883

Try with address 0x1E.

I tried with 0x1E address and i get this:

 x: -27643 y: 0 z: 773
 x: -27643 y: 0 z: 773
 x: -27643 y: 0 z: 773
 x: -27643 y: 0 z: 773

Even if i move the sensor the values at the serial monitor is the same.
I have tried two sensors with the same result.

I suggest that you run an I2C scanner to confirm the address and communication with the i2C bus.

// I2C scanner by Nick Gammon.  Thanks Nick.

#include <Wire.h>

void setup() {
  Serial.begin (115200); //*****  make sure serial monitor baud matches *****

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

Post the result here.

I have already did that and i found that the address is 0X0D.

See page 11 of the HMC5883 data sheet under Register Access.

succeed the 7-bit address (0x1E)

I got out a HMC5883L module, hooked to my Uno, and uploaded the code from your original post with the exception of changing the addr define to 0x1E. I get changing and believable data to serial monitor. I did not connect a LCD.

X Value: -361 Y Value: -258 Z Value: -461

X Value: -220 Y Value: 63 Z Value: -438

X Value: -83 Y Value: -19 Z Value: -484

X Value: 200 Y Value: -233 Z Value: -411

Is there another device on the I2C bus? Where did you get the module? As far as I know there is only one address for HMC5883 and it is not changeable.

I got it from ebay. This is the link in ebay. It says that it is HMC5883L. I read somewhere that it is different from HMC5883.

My apologies for not referring to the correct part number. Mine is a HMC5883L. I have done a Google search for HMC5883 and all hits refer to HMC5883L. I do not think that HMC5883 (without the L exists).

I connected an LCD as well and the LCD shows the correct output. The numbers match the serial monitor output. So the program is OK so the issue must be wiring or hardware. Can you post a photo of hour wiring? Are the solder joints between header and the module good? Pullup resistors are on the module so external pullups are not required.

I believe you have the QMC5883L compass instead. It is a Chinese imitation with a few differences, including the I2C address. Google for forum posts on the topic.

Library here: https://github.com/keepworking/Mecha_QMC5883L

I was not aware of the Chinese knock off. That would explain why the HMC5883L library works for me and not the OP.

Thank you guys for your help!!!! It is the chinese clone QMC5883L. I use the Mecha_QMC5883L library as jremington says and now everything working. But i should tell that in the module is written that it is HMC5883L.

Complain to the eBay seller that the module is mislabeled.

I will do that.
Just to see what i mean.
I attach photos of the module.