mpu6050

Hey guys! I am using a mpu5040 and a arduino uno to measure angles. I an using the code given below.

The pitch and roll angles work perfectly, however the values i get from the yaw makes no sense, it seems random. can someone help me.

#include <Wire.h>
const int MPU_addr = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int minVal = 265; int maxVal = 402;
double x;         //A4 (SDA), A5 (SCL)
double y;
double z;

void setup() {

  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
}

void loop() {

  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 14, true);

  AcX = Wire.read() << 8 | Wire.read();
  AcY = Wire.read() << 8 | Wire.read();
  AcZ = Wire.read() << 8 | Wire.read();

  int xAng = map(AcX, minVal, maxVal, -90, 90);
  int yAng = map(AcY, minVal, maxVal, -90, 90);
  int zAng = map(AcZ, minVal, maxVal, -90, 90);

  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); 
 
  Serial.println(z);

  delay(100);
}

Yaw is random, because the MPU-6050 has no yaw reference (usually a magnetometer is used).

The equations you are using for pitch and roll are not standard. Here are the conventional ones: How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot

Actually i am trying to make a drone, so if i want to calculate yaw i need to use a magnometer ?

Is there any other way?

To measure yaw in the standard system, you need a North reference.

Integrating the rate gyro can give you a relative yaw angle, with zero measured from the startup orientation, but it will drift.

Thanks, that cleared a lot of my doubts. +karma