GY-91 Angle Readings

Hello,
I am building a project using the GY-91 where I want to collect readings of the gyro's current angle, except when I try running the MPU9250 Gyro code for this, it makes a slight change and resets back to 0, showing that it doesn't measure the angle as I want it to. How can I measure the GY-91's current angle position instead of just reading the angular rate and letting it bounce up and back to 0?

Another issue is when I am using my current code, it just blanks out after a certain number.

Here is my code

#include <MPU9250_asukiaaa.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;
MPU9250_asukiaaa mpu;
float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection;
uint16_t mX, mY, mZ;

#define SDA_PIN 4
#define SCL_PIN 5

void setup() {
  
  Serial.begin(115200);
  Wire.begin();
  mpu.setWire(&Wire);

  bmp.begin();
  mpu.beginAccel();
  mpu.beginGyro();
  mpu.beginMag();

}

void loop() {
  if (mpu.accelUpdate() == 0) {
    aX = mpu.accelX();
    aY = mpu.accelY();
    aZ = mpu.accelZ();
    aSqrt = mpu.accelSqrt();
    Serial.print("accelX: " + String(aX));
    Serial.print("\taccelY: " + String(aY));
    Serial.print("\taccelZ: " + String(aZ));
    Serial.print("\taccelSqrt: " + String(aSqrt));
  }

  if (mpu.gyroUpdate() == 0) {
    
    gX = mpu.gyroX();
    gY = mpu.gyroY();
    gZ = mpu.gyroZ();
    Serial.print("\tgyroX: " + String(gX));
    Serial.print("\tgyroY: " + String(gY));
    Serial.print("\tgyroZ: " + String(gZ));
  }


  Serial.println("");
  delay(100);
  }

Please reach out to me as soon as possible. Thank you :)))

The gyro measures rates of rotation, not angles. You have to numerically integrate the rates to get angles.

You can measure two tilt angles (pitch and roll), with the Z axis approximately vertical, using the accelerometer and the following code.

// minimal MPU-6050 tilt and roll (sjr). Works with MPU-9250 too.
// works perfectly with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
//
// Tested with 3.3V eBay Pro Mini with no external pullups on I2C bus (worked with internal pullups)
// Add 4.7K pullup resistors to 3.3V if required. A4 = SDA, A5 = SCL

#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;

void setup() {

  Wire.begin();                                      //begin the wire communication
  Wire.beginTransmission(MPU_addr1);                 //begin, send the slave adress (in this case 68)
  Wire.write(0x6B);                                  //make the reset (place a 0 into the 6B register)
  Wire.write(0);
  Wire.endTransmission(true);                        //end the transmission
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr1);
  Wire.write(0x3B);  //send starting register address, accelerometer high byte
  Wire.endTransmission(false); //restart for read
  Wire.requestFrom(MPU_addr1, 6); //get six bytes accelerometer data
  int t = Wire.read();
  xa = (t << 8) | Wire.read();
  t = Wire.read();
  ya = (t << 8) | Wire.read();
  t = Wire.read();
  za = (t << 8) | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
  roll = atan2(ya , za) * 180.0 / PI;
  pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied

  Serial.print("roll = ");
  Serial.print(roll,1);
  Serial.print(", pitch = ");
  Serial.println(pitch,1);
  delay(400);
}

I appreciate your help. I might need your help again. I tried your code out and the pitch and roll are at consistent numbers now. Roll always stays at -135 and Pitch is always at 35.3. What could be the issue here?

Lots of things could be wrong: wiring incorrect, I2C address not correct, 5V to 3.3V logic level mismatch, etc.

For help, please read and follow the instructions in the "How to get the best out of this forum" post, and post the required information.

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