GY87 calibration and data reading

Hi, I'm using a GY87 module (only the gyro and HMC5885 module) and my code is working, I can read the datas in the Serial Monitor, but I think there is something wrong with them. The HMC 5883 datas are always increasing instead of showing the new result. Example: in the first data x=4500, but for the 10th measure it's like 75456. There is my code, thank u in advance!

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

//MPU6050 Accelerometer 
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

//HMC5883L Digital Compass
const int hmc5883Address = 0x1E; //0011110b, I2C 7bit address for compass
const byte hmc5883ModeRegister = 0x02;
const byte hmcContinuousMode = 0x00;
const byte hmcDataOutputXMSBAddress = 0x03;

int x, y, z; //triple axis data from HMC5883L.
int mX, mY, mZ;

void setup() {
    Wire.begin();
    Serial.begin(9600);

    // eszkoz inicializalasa
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // kapcsolodas megerositese
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    accelgyro.setI2CBypassEnabled(true);  //This sets the bypass so the HMC5883L gets a look in.

    //Magnetometer inicializalasa
    Wire.beginTransmission(hmc5883Address);  //Begin communication with compass
    Wire.write(hmc5883ModeRegister);  //select the mode register
    Wire.write(hmcContinuousMode); //continuous measurement mode
    Wire.endTransmission();
}

void loop() {
    // gyorsulas es szogsebesseg beolvasasa az eszkozrol
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // gyorsulas es szogsebesseg kijelzese
   Serial.println("gyorsulas es szogsebesseg:\t");
    Serial.print("x iranyu gyorsulas: ");
    Serial.print(ax); Serial.println("\t");
    Serial.print("y iranyu gyorsulas: ");
    Serial.print(ay); Serial.println("\t");
    Serial.print("z iranyu gyorsulas: ");
    Serial.print(az); Serial.println("\t");
    Serial.print("x iranyu szogsebesseg: ");
    Serial.print(gx); Serial.println("\t");
    Serial.print("y iranyu szogsebesseg: ");
    Serial.print(gy); Serial.println("\t");
    Serial.print("z iranyu szogsebesseg: ");
    Serial.print(gz); Serial.println("\t");

    //Accessing the HMC5883L Digital Compass  
    //Tell the HMC5883L where to begin reading the data
    Wire.beginTransmission(hmc5883Address);
    Wire.write(hmcDataOutputXMSBAddress);  //Select register 3, X MSB register
    Wire.endTransmission();

    //Read data from each axis of the Digital Compass
    Wire.requestFrom(hmc5883Address, 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    
    }

    mX = (mX + x) ;
    mY = (mY + y) ;
    mZ = (mZ + z) ;

    // Print out values of each axis
    Serial.println("Magnetometer tengelyek:\t");
    Serial.print("x: ");
    Serial.print(mX); Serial.println("\t");
    Serial.print("  y: ");
    Serial.print(mY); Serial.println("\t");
    Serial.print("  z: ");
    Serial.println(mZ); Serial.println("\t");
    int angle = atan2(-y, x) / M_PI * 180;
    if (angle < 0) {
        angle = angle + 360;
    }

    // Reporting the Compass data to the Serial port
    Serial.print("Irany(fok):\t");
    Serial.print(angle); Serial.println("\t");
}

Hi, @breka and welcome. I am not familiar with these modules. If I understand correctly
the logic of your program, that is exactly what is expected. You read the data from the HMC5885, then accumulate it in a variable prefixed with 'm' and print that sum. For example, you read Z, add it to mZ, and print mZ to the serial monitor.

That's because your are printing mX instead of x. And mX is accumulating the values of x at each loop. The same for mY and mZ.

I don't know what should be the use of mX, mY, mZ, maybe averaging the directions of the last reads? but then some logic to calculate the average values is missing. Or just use x directly.

Thank you so much, I'm so stupid
I used mx, my and mz in the previous version of my code (for averaging) and forgot to delete it

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.