Go Down

Topic: HMC5883L GY-273 does not work as compass (Read 2567 times) previous topic - next topic

Cricri

Hi,some days ago I received this 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 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!

spycatcher2k

Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

spycatcher2k

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;
}
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

Cricri

#3
Jan 19, 2018, 07:22 pm Last Edit: Jan 19, 2018, 07:30 pm by Cricri
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.

spycatcher2k

The data sheet says its 1E - Best contact the person you bought it from to confirm the actual address.
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

Cricri

I asked it to he yesterday but he has not answered yet :(

Grumpy_Mike

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.

Cricri

#7
Jan 19, 2018, 08:20 pm Last Edit: Jan 19, 2018, 08:30 pm by Cricri
@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 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 post and using the exemple on this github library it works! I only have to convert the values in degrees! Thanks for the "hint" !!!!

montotox

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  >:(

fayzanhr

Edit: I found this post and using the exemple on this 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

LeobotElectronics

#10
Apr 21, 2019, 07:34 pm Last Edit: Apr 21, 2019, 07:34 pm by LeobotElectronics
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 for links to the libraries and tutorials on how to use it.


Go Up