Constant HMC5883L Magnetometer output

Hi, I’m newbie in Arduino and I have just built my first sketch.

It’s very basic and I only want to get the values of the 3 axis of the magnetometer.

I’m using Arduino IDE version 1.8.8, the last one in this date.

The version of the Arduino is Duemilanove and it’s connected to PC using USB (I don’t use battery on Arduino board).

The wiring diagram is the following one:

The code I am using is this one:

#include <Wire.h>

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();
  
  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.write(0x02); //select mode register
  Wire.write(0x00); //continuous measurement mode
  Wire.endTransmission();
}

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

  //Tell the HMC5883L where to begin reading data
  Wire.beginTransmission(address);
  Wire.write(0x03); //select register 3, X MSB register
  Wire.endTransmission();
  
 
 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //X msb
    x |= Wire.read(); //X lsb
    z = Wire.read()<<8; //Z msb
    z |= Wire.read(); //Z lsb
    y = Wire.read()<<8; //Y msb
    y |= Wire.read(); //Y lsb
  }
  
  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  
  delay(250);
}

The module where the chipset is attached is called GY-273.

After several hours trying to run different codes I have found online, finally the above code was possible to compile, but the output of the magnetometer always shows:

The problem is even If I move the magnetometer, I always get the same output.

There are several possible problems.

The first is that the sensor board you have may use 3.3V I/O, in which case it can be destroyed by connecting to a 5V Arduino. Please post a link to the exact module you have.

To use a 3.3V module with 5V Arduino, a level shifter is usually required (there are exceptions).

The second is that many cheap "HMC5883L" modules are wrongly advertised, and use the QMC5883L chip instead. You need a different library for those chips.

jremington: There are several possible problems.

The first is that the sensor board you have may use 3.3V I/O, in which case it can be destroyed by connecting to a 5V Arduino. Please post a link to the exact module you have.

To use a 3.3V module with 5V Arduino, a level shifter is usually required (there are exceptions).

The second is that many cheap "HMC5883L" modules are wrongly advertised, and use the QMC5883L chip instead. You need a different library for those chips.

Thank you for the reply.

I have found in a website that it's possible to connect it to 5V. I know that not all models are possible to connect to 5V because it's needed to have a regulator to be possible to connect to 5V.

The exact module I have is called GY-273 http://qqtrading.com.my/3-axis-compass-magnetometer-sensor-gy-273-digital-hmc5883l

How can I determine if the chip is HMC5883L or QMC5883L?

Your model has a 5V regulator, meaning that it is possible to power it by 5V.

It does NOT have level shifters for the I/O, so it can be destroyed by connecting SDA or SCL to a 5V output.

If you are absolutely certain that the required pullup resistors on the I2C lines are connected to 3.3V, then it might be safe to use that module with a 5V Arduino.

Edit: I checked the schematic for the Duemilanove, which shows no pullups to 5V. So, you may be safe in connecting it to the module. However, with a level shifter, safety is guaranteed, and use of one is required for modules that do not use I2C for digital I/O, or I2C modules that lack pullup resistors.

To learn how to use the QMC5883L, I recommend to consult the link I posted in reply #1.

Sorry that you chose this project as your first! Bit of a brutal introduction to the Arduino world.

jremington: Your model has a 5V regulator, meaning that it is possible to power it by 5V.

It does NOT have level shifters for the I/O, so it can be destroyed by connecting SDA or SCL to a 5V output.

If you are absolutely certain that the required pullup resistors on the I2C lines are connected to 3.3V, then it might be safe to use that module with a 5V Arduino.

Edit: I checked the schematic for the Duemilanove, which shows no pullups to 5V. So, you may be safe in connecting it to the module. However, with a level shifter, safety is guaranteed, and use of one is required for modules that do not use I2C for digital I/O, or I2C modules that lack pullup resistors.

To learn how to use the QMC5883L, I recommend to consult the link I posted in reply #1.

Sorry that you chose this project as your first! Bit of a brutal introduction to the Arduino world.

Thanks for the answer.

As you know, I am very new to this and I have been reading some information on Google after reading your post.

The question #1 is: finally, it may be safe to connect the magnetometer module to my Arduino using 5V?

Question #2 is: you suggest me to learn how to use the QMC5883L, so, do I have a QMC5883L chip in the module?

Thank you!

1) It is probably safe to connect that module to the SDA and SCL outputs of your Arduino. It will not work, if there are no pullup resistors on the module to 3.3V. Since I don't have your module, I can't tell whether that is the case.

2) Have you still not bothered to look at this link? https://surtrtech.com/2018/02/01/interfacing-hmc8553l-qmc5883-digital-compass-with-arduino/

If you [u]have[/u] read through the material in that link and did not understand it, please let us know what is confusing you.

jremington: 1) It is probably safe to connect that module to the SDA and SCL outputs of your Arduino. It will not work, if there are no pullup resistors on the module to 3.3V. Since I don't have your module, I can't tell whether that is the case.

2) Have you still not bothered to look at this link? https://surtrtech.com/2018/02/01/interfacing-hmc8553l-qmc5883-digital-compass-with-arduino/

If you [u]have[/u] read through the material in that link and did not understand it, please let us know what is confusing you.

Hi again, after placing the pullup resistors and reading the link you provide I have finally obtained the information from the magnetometer.

The main problem was the chip is a fake one as it was described in the link you provide. The seller sells it as original one, but that is not true.

I have to say you thank you very much for all the help and the advice you provided to me.

Regards, Richard.

Glad you got it working! As I understand it, the QMC5883L chips are not fake, they are a different version and the design may even be licensed. I believe that the HMC5883L chips are no longer produced by Honeywell.

The real problem is that unscrupulous eBay and other on line sellers lie about their offerings.

Yes, the eBay seller doesn't tell about this is a different version of the chip. I have read again the offering in eBay and the seller always tells that the chip is HMC5883L.

Just one question, the QMC5883L chip is chinese one?

Yes, it is made by a Chinese manufacturer, but so are many chips designed by western countries.