Arduino Forum

Using Arduino => Project Guidance => Topic started by: Cricri on Jan 19, 2018, 06:36 pm

Title: HMC5883L GY-273 does not work as compass
Post by: Cricri on Jan 19, 2018, 06:36 pm
Hi,some days ago I received this (https://m.ebay.it/itm/arduino-brujula-GY-273-HMC5883L-Triple-Axis-electronica-placa-raspberry-orienta-/221849465440?txnId) module but I have some trouble with it.
I connected SDA to 20 pin ( I have an Arduino Mega 2560), SCL to 21 and VCC to 3.3V.
I want to use this code:

Code: [Select]
#include <Wire.h> //I2C Arduino Library

#define addr 0x0D //I2C Address for The HMC5883

void setup()
{
 
  Serial.begin(9600);
  Wire.begin();
 
 
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  delay(100);
  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
  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);
}


But it always show 0. I also tried to print  Wire.endTransmission() , which give 0 (success) and the I2C scanner (https://playground.arduino.cc/Main/I2cScanner) that show
Quote
I2C device found at address 0x0D  !
Anyone has some idea for why the test finds it but the sketch does not work?
Thanks in advance!
Title: Re: HMC5883L GY-273 does not work as compass
Post by: spycatcher2k on Jan 19, 2018, 07:08 pm
try 0x1E for the address!
Title: Re: HMC5883L GY-273 does not work as compass
Post by: spycatcher2k on Jan 19, 2018, 07:16 pm
Code: [Select]
/* Include the standard Wire library */
#include <Wire.h>

/* The I2C address of the module */
#define HMC5803L_Address 0x1E

/* Register address for the X Y and Z data */
#define X 3
#define Y 7
#define Z 5

void setup()
{
  Serial.begin(9600);
  /* Initialise the Wire library */
  Wire.begin();
 
  /* Initialise the module */
  Init_HMC5803L();
}

void loop()
{
  /* Read each sensor axis data and output to the serial port */
  Serial.print(HMC5803L_Read(X));
  Serial.print(" ");
  Serial.print(HMC5803L_Read(Y));
  Serial.print(" ");
  Serial.println(HMC5803L_Read(Z));
 
  /* Wait a little before reading again */
  delay(200);
}


/* This function will initialise the module and only needs to be run once
   after the module is first powered up or reset */
void Init_HMC5803L(void)
{
  /* Set the module to 8x averaging and 15Hz measurement rate */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(0x00);
  Wire.write(0x70);
         
  /* Set a gain of 5 */
  Wire.write(0x01);
  Wire.write(0xA0);
  Wire.endTransmission();
}


/* This function will read once from one of the 3 axis data registers
and return the 16 bit signed result. */
int HMC5803L_Read(byte Axis)
{
  int Result;
 
  /* Initiate a single measurement */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(0x02);
  Wire.write(0x01);
  Wire.endTransmission();
  delay(6);
 
  /* Move modules the resiger pointer to one of the axis data registers */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(Axis);
  Wire.endTransmission();
   
  /* Read the data from registers (there are two 8 bit registers for each axis) */
  Wire.requestFrom(HMC5803L_Address, 2);
  Result = Wire.read() << 8;
  Result |= Wire.read();

  return Result;
}
Title: Re: HMC5883L GY-273 does not work as compass
Post by: Cricri on Jan 19, 2018, 07:22 pm
try 0x1E for the address!
It does not work obviously and the Wire.endTransmission() prints 2, that is for "received NACK on transmit of address" because the port is not correct, the right is 0x0D..!

Edit: I also tried to execute the code that you posted but does not work! Anyway, thanks for the answer.
Title: Re: HMC5883L GY-273 does not work as compass
Post by: spycatcher2k on Jan 19, 2018, 07:32 pm
The data sheet says its 1E - Best contact the person you bought it from to confirm the actual address.
Title: Re: HMC5883L GY-273 does not work as compass
Post by: Cricri on Jan 19, 2018, 07:37 pm
I asked it to he yesterday but he has not answered yet :(
Title: Re: HMC5883L GY-273 does not work as compass
Post by: Grumpy_Mike on Jan 19, 2018, 08:00 pm
Caution, if you are buying a HMC5883L module, there are some fake modules about on eBay that claim they are HMC5883L devices but are in fact a QMC5883, which while they are indeed magnetic sensors, have a very different internal structure, different capabilities, and will not work with the software here. The way to tell a real HMC5883L is that the chip is marked L883 where as the "fake" chip is marked DA5883.
Title: Re: HMC5883L GY-273 does not work as compass
Post by: Cricri on Jan 19, 2018, 08:20 pm
@Grumpy_Mike thanks for the answer, I think you are right! As soon as I can read the number on the chip I'll do it!( I need a microscope, the characters are too small).
Just now I tested a QMC sketch I taked on this (http://wiki.epalsite.com/index.php?title=QMC5883L_Electronic_Compass) site, but it shows a different number everytime I close and reopen the serial monitor and while it is open it does not change..!

Edit: I found this (http://www.esp8266.com/viewtopic.php?f=13&t=15445) post and using the exemple on this (https://github.com/mechasolution/Mecha_QMC5883L) github library it works! I only have to convert the values in degrees! Thanks for the "hint" !!!!
Title: Re: HMC5883L GY-273 does not work as compass
Post by: montotox on Feb 23, 2018, 04:36 pm
Caution, if you are buying a HMC5883L module, there are some fake modules about on eBay that claim they are HMC5883L devices but are in fact a QMC5883, which while they are indeed magnetic sensors, have a very different internal structure, different capabilities, and will not work with the software here. The way to tell a real HMC5883L is that the chip is marked L883 where as the "fake" chip is marked DA5883.
Thanks Grumpy Mike!!!  :D
I have already tried all the options and I never realized that they had sold me a QMC5883  >:(
Title: Re: HMC5883L GY-273 does not work as compass
Post by: fayzanhr on Mar 14, 2018, 12:46 pm
Edit: I found this (http://www.esp8266.com/viewtopic.php?f=13&t=15445) post and using the exemple on this (https://github.com/mechasolution/Mecha_QMC5883L) github library it works! I only have to convert the values in degrees! Thanks for the "hint" !!!!
hi Did you get it working? I'm using the same library as well, and I'm getting values. How do you plan on converting the values to degrees? Have you already?
I don't know where to begin because I don't know what these values are coming up. What about calibration?
Please let me know

High Regards
Title: Re: HMC5883L GY-273 does not work as compass
Post by: LeobotElectronics on Apr 21, 2019, 07:34 pm
If anyone is still experiencing problems trying to get the HMC5883L GY-273, which is actually actually the QMC5883L version then have a look at https://leobot.net/viewproduct.aspx?id=2940 (https://leobot.net/viewproduct.aspx?id=2940) for links to the libraries and tutorials on how to use it.