I'm currently working with an GY-521 and an Arduino Uno. My goal is to calculate the roll and pitch angles using the MPU6050 accelerometers and gyros. I’m not using any MPU6050 related library in my own code.

My setup :

MPU6050 VCC to Arduino 5v

MPU6050 GND to Arduino ground

MPU6050 SCL to Arduino A5

MPU6050 SDA to Arduino A4

MPU6050 ADO to Arduino ground

MPU6050 INT to Arduino I/O 2

I don’t have any resistors on the SCL and SDA lines.

I’ve watched and read a lot of tutorial on how to setup, calibrate, get the raw data and process those data to get roll and pitch angle. My code is working very great at this point as I get accurate values for my pitch and roll angles. You can find my code at the bottom "MPU6050 Own Code v.1" , I couldn't post it here directly.

The “G total” is equal to 1 (which is to be expected) and my “Angle_x” and “Angle_y” are close to 0 degrees (my desk is not 100% leveled) which is what I expect. I’m satisfied with those results so far.

```
Gyro calibration started, do not move IMU
Gyro calibration done
Gyro X raw error : 0.03
Gyro Y raw error : 0.00
Gyro Z raw error : 0.00
G total=1.00 | Angle_x: 0.01 | Angle_y: 0.01 |
G total=0.99 | Angle_x: 0.01 | Angle_y: 0.01 |
G total=1.00 | Angle_x: 0.02 | Angle_y: 0.00 |
G total=0.99 | Angle_x: 0.03 | Angle_y: 0.02 |
G total=1.01 | Angle_x: 0.03 | Angle_y: 0.03 |
G total=1.00 | Angle_x: 0.03 | Angle_y: 0.02 |
G total=1.00 | Angle_x: 0.02 | Angle_y: 0.03 |
G total=1.00 | Angle_x: 0.03 | Angle_y: 0.03 |
G total=0.99 | Angle_x: 0.03 | Angle_y: 0.04 |
G total=1.00 | Angle_x: 0.04 | Angle_y: 0.04 |
G total=1.00 | Angle_x: 0.03 | Angle_y: 0.04 |
```

The issue is that I don’t get those values at the start (when I power the Arduino and the MPU6050 and open the serial monitor). I get those values only after completely uploading another sketch, in my case the MPU6050 calibration (linked at the bottom, I don't remember where I got this code from). I have to upload the MPU6050 calibration sketch, wait for it to finish completely (wait until is done calculating the offsets), then upload my own code and only then is my code working as shown above.

Here you can see the values a get from my code if don’t upload the MPU6050 calibration sketch first. As you can see the “G total” is not equal to 1 ( =2.06) and my “Angle_x” and “Angle_y” are 2.79 and -58.30 although they should be equal to 0 (or close to 0), since the setup is at the position on my desk. Those values tell my that something is not working correctly.

```
Gyro calibration started, do not move IMU
Gyro calibration done
Gyro X raw error : -1.36
Gyro Y raw error : -0.23
Gyro Z raw error : -2.73
G total=2.07 | Angle_x: 2.79 | Angle_y: -58.30 |
G total=2.06 | Angle_x: 2.80 | Angle_y: -58.30 |
G total=2.06 | Angle_x: 2.80 | Angle_y: -58.30 |
G total=2.06 | Angle_x: 2.80 | Angle_y: -58.30 |
G total=2.06 | Angle_x: 2.79 | Angle_y: -58.30 |
G total=2.06 | Angle_x: 2.79 | Angle_y: -58.31 |
G total=2.06 | Angle_x: 2.79 | Angle_y: -58.32 |
G total=2.06 | Angle_x: 2.79 | Angle_y: -58.32 |
G total=2.07 | Angle_x: 2.78 | Angle_y: -58.31 |
G total=2.06 | Angle_x: 2.78 | Angle_y: -58.31 |
```

I don’t understand why I have to first upload a different code to make my code work. Is the MPU6050 calibration sketch changing something on the Arduino or the MPU6050? If so, what should I add to my code to make it work correctly without having to upload the other sketch first ?

Could someone please help my figuring out what the issue is?

Any help appreciated. Thanks.

MPU6050_Own_Code_v.1.ino (5.31 KB)

MPU6050_calibration.ino (8.02 KB)