Hi,

I am using an MPU-6050 on a GY-521 breakout board connected to an Arduino UNO R3 to calculate the tilt angle from the MEMS accelerometer and the corresponding angular velocity from the MEMS gyroscope. Then, these two data are fed into a complementary filter to generate an accurate and reliable tilt angle without drift.

**Problem:** I cannot understand why the following code works. I don't understand the negative signs in front of atan2 function and in front of the gx variable. The variables ay and az contain the horizontal and vertical components of the gravity vector when the IMU is tilted in the Y-Z plane. The variable gx contains the angular velocity along the X axis.

**Attempt:**

I tried to visualise it using this figure from the datasheet:

I looked at my MPU-6050 GY-521 hardware and i found some orientation markings on there. The MPU-6050 is being tilted in the Y-Z plane, and at the same time, there is rotational motion along the X-axis. But i can't understand the negative signs.

My guess is that the tilt is being assumed to be forwards in the Y-Z plane, based on the figure, so it is in the opposite direction to the default polarity of rotation on the X-axis, meaning, clockwise. In that case, the negative in front of the gx makes sense. However, the negative in front of atan2 is still a mystery.

**Solution:** (can't understand why it works)

```
void accelgyro(int16_t ax, int16_t ay, int16_t az, int16_t gx, int16_t gy, int16_t gz)
{
accelAngle = -atan2(ay, az) * (180/ PI);
gyroAngularVelocity = -gx / 131.0;
complementaryFilter(accelAngle, gyroAngularVelocity);
}
```