QMC5883L compass sensor

Hello everyone! I'm doing a Smartwatch with GY-87 module sesnor. This module includes, among others, the QMC5883L compass sensor.

I use 2 different libraries; QMC5883LCompass and MechaQMC5883L.

The first one doesn't work, prints 0 in all directions (azimuth, bearing, etc.). But the second one "works".
There are 2 examples, azimuth and raw.

Azimuth example:

#include <Wire.h>
#include <MechaQMC5883.h>

MechaQMC5883 qmc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  qmc.init();
  //qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}

void loop() {
  int x, y, z;
  int azimuth;
  //float azimuth; //is supporting float too
  qmc.read(&x, &y, &z,&azimuth);
  //azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" y: ");
  Serial.print(y);
  Serial.print(" z: ");
  Serial.print(z);
  Serial.print(" a: ");
  Serial.print(azimuth);
  Serial.println();
  delay(100);
}

In azimuth the serial monitor prints:

x: 20735   y: 180  z: 20480  a: 0

While the serial monitor (from Arduino IDE) is open, neither a,y,z,a move. If I close the serial monitor and move the GY to another site and I open the serial monitor again, de y changes, but the others no.

I don't know why if I move the GY-87, the numbers are not changed instantly.

The raw example:

#include <Wire.h>
#include <MechaQMC5883.h>

MechaQMC5883 qmc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  qmc.init();
  //qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}

void loop() {
  int x,y,z;
  qmc.read(&x,&y,&z);

  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" y: ");
  Serial.print(y);
  Serial.print(" z: ");
  Serial.print(z);
  Serial.println();
  delay(100);
}

The serial monitor prints:

x: 18771  y: 21024  z: -18170

The x,y,z are different from azimuth example. I don't understand it.

If someone can help me, please!

Which controller are You using? Show us the wiring.

I use 2 different libraries; QMC5883LCompass and MechaQMC5883L.

For both you forget to provide links. The first one is available in the Library Manager, the second has multiple locations on github. Both should work if the current version is used. Magnetic field sensors (a compass is nothing else) tend to show erratic results inside buildings and have to be calibrated before use.

Railroader:
Which controller are You using? Show us the wiring.

I'm using Arduino NANO connected to a laptop. I connected:

NANO GY-87

GND GND
A5 SCL
A4 SDA
3.3V 3.3V

pylon:
For both you forget to provide links. The first one is available in the Library Manager, the second has multiple locations on github. Both should work if the current version is used. Magnetic field sensors (a compass is nothing else) tend to show erratic results inside buildings and have to be calibrated before use.

Sorry!

The first one: GitHub - mprograms/QMC5883LCompass: QMC5883L Compass is a Arduino library for using QMC5583L series chip boards as a compass. Supports: - Getting values of XYZ axis. - Calculating Azimuth. - Getting 16 point Azimuth bearing direction (0 - 15). - Getting 16 point Azimuth bearing Names (N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW) - Smoothing of XYZ readings via rolling averaging and min / max removal. - Optional chipset modes
The second one: GitHub - keepworking/Mecha_QMC5883L: Arduino lib for QMC5883

But all the sensors prints 0 in the first one, is it normal?
So I have to calibrate outside?

The other problem is when I move the GY-87, the direction not change. The normal is that change at the same time, no?

If the raw data in x, y and z don't change as you move the magnetometer about, something is not working correctly.

Run the I2C scanner program to make sure that the I2C connection is working and that the I2C address is correct.

The second one: GitHub - keepworking/Mecha_QMC5883L: Arduino lib for QMC5883

The original repository seems to be GitHub - mechasolution/Mecha_QMC5883L: Arduino lib for QMC5883. Your version seems to have a slightly elaborated error handling. I don't like it if someone forks a project without telling the reason for it nor linking back to the original.

But all the sensors prints 0 in the first one, is it normal?

You might be using it incorrectly. Post the code you used!

So I have to calibrate outside?

Yes, to get good results keep a distance of at least 50m to the next building or other metal object.

NANO GY-87

GND GND
A5 SCL
A4 SDA
3.3V 3.3V

The QMC5883L is a 3.3V device, it's not 5V tolerant, so you might have fried it by connecting it directly to the 5V of the SDA/SCL pins of the Nano (although these devices usually tolerate that for some time).

As you wrote that you're using the GY-87 board (you failed to provide a link to schematics of it) it seems that at least some version of the board have a level converter on-board. But these boards have to be powered by 5V as the have their own 3.3V voltage regulator and if you give only 3.3V to it the output will be below 3V which in turn isn't enough for the Nano to detect.
That board carries a HMC5883L which seems to very similar to the QMC5883L but not identical. I haven't checked the datasheet in-depth to find all differences.

The HMC5883L (discontinued) has a different I2C address and won't work with QMC5883L code.

With a 5V Arduino, you need logic level shifters unless they are included on the compass module.

I strongly recommend the Pololu compass modules, which have better performance and the necessary interface for 3.3 or 5V operation. In addition, you get responsible product support.

Once you get it working, compass calibration MUST be performed in its final installed location. Otherwise, the influence of nearby current carrying conductors, magnetic or iron containing objects will usually make the compass readings useless.

pylon:
The original repository seems to be GitHub - mechasolution/Mecha_QMC5883L: Arduino lib for QMC5883. Your version seems to have a slightly elaborated error handling. I don't like it if someone forks a project without telling the reason for it nor linking back to the original.

You might be using it incorrectly. Post the code you used!

Yes, to get good results keep a distance of at least 50m to the next building or other metal object.

The QMC5883L is a 3.3V device, it's not 5V tolerant, so you might have fried it by connecting it directly to the 5V of the SDA/SCL pins of the Nano (although these devices usually tolerate that for some time).

As you wrote that you're using the GY-87 board (you failed to provide a link to schematics of it) it seems that at least some version of the board have a level converter on-board. But these boards have to be powered by 5V as the have their own 3.3V voltage regulator and if you give only 3.3V to it the output will be below 3V which in turn isn't enough for the Nano to detect.
That board carries a HMC5883L which seems to very similar to the QMC5883L but not identical. I haven't checked the datasheet in-depth to find all differences.

I bought the GY-87 in amazon and it says "QMC5883L" not "HMC5883L". https://www.amazon.es/gp/product/B07HMLRD8Q/ref=ox_sc_saved_image_1?smid=A3U3NV7MT8POQS&psc=1

I connected the Gy to NANO with 3.3V, still get 5V??

The code from the example that prints 0:

#include <QMC5883LCompass.h>

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);
  compass.init();
  
}

void loop() {
  int x, y, z;
  
  // Read compass values
  compass.read();

  // Return XYZ readings
  x = compass.getX();
  y = compass.getY();
  z = compass.getZ();
  
  Serial.print("X: ");
  Serial.print(x);
  Serial.print(" Y: ");
  Serial.print(y);
  Serial.print(" Z: ");
  Serial.print(z);
  Serial.println();
  
  delay(250);
}

If I download the mecha_QMC library from your link, it works?

jremington:
The HMC5883L (discontinued) has a different I2C address and won't work with QMC5883L code.

With a 5V Arduino, you need logic level shifters unless they are included on the compass module.

I strongly recommend the Pololu compass modules, which have better performance and the necessary interface for 3.3 or 5V operation. In addition, you get responsible product support.

Once you get it working, compass calibration MUST be performed in its final installed location. Otherwise, the influence of nearby current carrying conductors, magnetic or iron containing objects will usually make the compass readings useless.

Yes, I know! But I'm using QMC5883L (https://www.amazon.es/gp/product/B07HMLRD8Q/ref=ox_sc_saved_image_1?smid=A3U3NV7MT8POQS&psc=1)

But the Polulu is not incorporated in the GY, and for my Smartwatch it is very important that keep it all small