# Complementary filter with the MPU6050

**URL:** <https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303>\
**Category:** General Guidance\
**Created:** [February 6, 2014, 9:47pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303 "2014-02-06T21:47:29Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 15, 2014, 8:50pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/21 "2014-02-15T20:50:31Z")

</div>

HELP!!!!!

I think that my MPU-6050 is wrong... when it outputs the values flicker between two, even though I am vigorously shaking the IMU.  
Here is some output values using just the MPU-6050 code from arduino playground:

MPU-6050  
Read accel, temp and gyro, error = 2  
accel x,y,z: 747, 512, 12545  
temperature: 24.459 degrees Celsius  
gyro x,y,z : 520, 4557, 396,

MPU-6050  
Read accel, temp and gyro, error = 2  
accel x,y,z: -5374, 2, 305  
temperature: 37.212 degrees Celsius  
gyro x,y,z : 2050, -13039, -29695,

MPU-6050  
Read accel, temp and gyro, error = 2  
accel x,y,z: 747, 512, 12545  
temperature: 24.459 degrees Celsius  
gyro x,y,z : 520, 4557, 396,

MPU-6050  
Read accel, temp and gyro, error = 2  
accel x,y,z: -5374, 2, 305  
temperature: 37.212 degrees Celsius  
gyro x,y,z : 2050, -13039, -29695,

MPU-6050  
Read accel, temp and gyro, error = 2  
accel x,y,z: 747, 512, 12545  
temperature: 24.459 degrees Celsius  
gyro x,y,z : 520, 4557, 396,

Can anybody help?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 16, 2014, 12:31am UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/22 "2014-02-16T00:31:18Z")

</div>

> stratjnd:  
> I tried the complementary filter using the code that you provided. It looked quite good when I ran it but every time I moved the IMU around, the number increased and then stayed there when the IMU was still. Every time I moved the IMU, the number increased but would not go down. Can anybody help??
> 
> Thanks!  
> StratJnd

Please share your full code you used so we can check for possible errors.

> stratjnd:  
> HELP!!!!!
> 
> I think that my MPU-6050 is wrong... when it outputs the values flicker between two, even though I am vigorously shaking the IMU. Here is some output values using just the MPU-6050 code from arduino playground:
> 
> MPU-6050  
> Read accel, temp and gyro, error = 2  
> accel x,y,z: 747, 512, 12545  
> temperature: 24.459 degrees Celsius  
> gyro x,y,z : 520, 4557, 396,
> 
> MPU-6050  
> Read accel, temp and gyro, error = 2  
> accel x,y,z: -5374, 2, 305  
> temperature: 37.212 degrees Celsius  
> gyro x,y,z : 2050, -13039, -29695,  
> ....

It looks similar to an issue I had with one of my chips, it would return random values for both accelerometer and gyroscope regardless of being moved or not. Temperature however was returned just fine. Turned out it was a bad chip, replacing it fixed my issue.  
But i'm not sure this is your issue since you state in the previous post that moving the mpu6050 did cause of change in angle in your possibly 'broken' complimentary filter.

Which MPU6050 breakout board are you using?  
Are you supplying the correct voltage to your MPU6050? (depends on which breakout board you have, some have a voltage regulator which allows for 5V VCC, some others do not in which case you need to power it from 3.3V otherwise your MPU6050 will be damaged)  
Are you always getting the same repeating values or do they change every time you run the sketch?  
Are you still getting these repeating values when you change the MPU6050 address (0x68) to something not present on your I2C bus?  
When you comment out the line "MPU6050\_write\_reg (MPU6050\_PWR\_MGMT\_1, 0);" in the setup routine does the sketch now return 0 for all the accelerometer and gyro axis?

---

<div class="post-metadata">

**Author:** ![exitedelektron](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@exitedelektron](https://forum.arduino.cc/u/exitedelektron)\
**Post date:** [May 1, 2015, 1:39pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/23 "2015-05-01T13:39:12Z")

</div>

Hi, I am using MPU6050 for self balancing unicycle. I am using single axis of sensor. I am getting my tilt angle from following code.

double accXangle=(atan2(AcY,AcZ)+PI)_RAD\_TO\_DEG;  
// Convert gyro raw vlues to degrees!  
double gyroXrate=(double)GyX/131.0;  
// Complementary Filter!  
double compAngleX = (0.97 \* (compAngleX + (gyroXrate \* dt))) + (0.03 \* accXangle);  
 [//Serial.println](https://Serial.println)("AcX = ");  
float a=31.142_(compAngleX-5.35);  
float ang=a-90;  
return ang;

But the problem is that due to sudden movements of sensor, it gives random values which have nothing to do with sensor movement. For example when sensor is suddenly moved in positive direction of angle measurement, i gives -ve values for angle which then effect on pwm and thus direction of motor.

Can any one tell me where i am going wrong or why this is happening.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [May 1, 2015, 3:27pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/24 "2015-05-01T15:27:17Z")

</div>

Always post all your code, using code tags ("\<\>" button).

> where i am going wrong or why this is happening.

You are assuming that the accelerometer readings can be translated into the angle that gravity makes with respect to the Z axis. This works only when the accelerometer is not being subjected to other forces, and the gravity vector is in the Y-Z plane.

The following two lines are probably wrong. From the second line, it appears that "a" is supposed to be in degrees, but it is not.

```arduino
float a=31.142*(compAngleX-5.35);
float ang=a-90;

```

---

<div class="post-metadata">

**Author:** ![mohsin175](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mohsin175](https://forum.arduino.cc/u/mohsin175)\
**Post date:** [June 18, 2015, 8:28pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/25 "2015-06-18T20:28:05Z")

</div>

I have been working on the MPU6050 Complementary filter as well. i have managed to get very stable values except for Roll angle which seems to drift due to gyro and takes time to return back to 0. does anybody know the reason and solution for this.

I have attached the graph after plotting the values on Excel

 ![Graph X.png](https://europe1.discourse-cdn.com/arduino/original/4X/1/4/a/14ae440a534f5f3d0da952ce780cca925751d3c5.png)

 ![Graph Y.png](https://europe1.discourse-cdn.com/arduino/original/4X/b/2/2/b224e1086785633120eccbb010cea1880e2c8c7e.png)

 ![Graph Z.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/0/a/d0af6024e9ab4748365d7f4f97b704e762b6d36c.png)

---

<div class="post-metadata">

**Author:** ![Dragal](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Dragal](https://forum.arduino.cc/u/Dragal)\
**Post date:** [November 12, 2015, 2:25pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/26 "2015-11-12T14:25:39Z")

</div>

> racquemis:  
> Yeah, you can just initialize the variables used for the complimentary filter to zero. It will automatically settle down to the correct angles.If your sampling fast enough you might not even notice it.
> 
> No, you don't need to add a line like.
> 
> ```arduino
> Last_Filter_Angle = Filter_Angle
> 
> ```
> 
> The complimentary filter already takes care of that.  
> Have another look at the code example i gave
> 
> ```arduino
> float pitch=0;
> 
> ```

float pitchAcc;  
float P\_CompCoeff= 0.98;  
void ComplementaryFilter(int ax,int ay,int az,int gy,int gz) {  
&nbsp; long squaresum=(long)ay_ay+(long)az_az;  
&nbsp; pitch+=((-gy/32.8f)\*(delta\_t/1000000.0f));  
&nbsp; pitchAcc =atan(ax/sqrt(squaresum))_RAD\_TO\_DEG;  
&nbsp; pitch =P\_CompCoeff_pitch + (1.0f-P\_CompCoeff)\*pitchAcc;  
}

> ```arduino
> 
> First, notice that *pitch* is declared outside the filter function. It will keep it's value even after the filter has been executed. After the first filter execution *pitch* will contain your current Angle. However, when the filter is executed for the second time *pitch* actually becomes your Previous Angle. (pitch+= in line 6 of the code above)
> 
> Does this clear things up a bit? Let me know how it goes.
> 
> ```

after applying this code I have got this error:

a function-definition is not allowed here before '{' token  
expected ',' or '...' before '.' token

Here is my code:  
void loop()  
{  
float pitch = 0;  
float pitchAcc;  
float coef = 0.98;  
void ComplementaryFilter (int accel.readX\_G(),int accel.readY\_G(), int accel.readZ\_G(), int gyro.readY\_DegPerSec(),int gyro.readZ\_DegPerSec())  
long squaresum = (long)accel.readY\_G()_accel.readY\_G() + (long)accel.readZ\_G()accel.readZ\_G();  
pitch += ((-gyro.readY\_DegPerSec()) dt);  
pitchAcc = atan(accel.readX\_G()/sqrt(squaresum))_ 180 / M\_PI;  
pitch = coef\*pitch + (1.0-coef)\*pitchAcc;

// вывод угловой скорости относительно оси Y  
Serial.print(pitch);  
Serial.println("");  
delay(300);  
}

---

<div class="post-metadata">

**Author:** ![GrooveFlotilla](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grooveflotilla/32/213680_2.png) [@GrooveFlotilla](https://forum.arduino.cc/u/GrooveFlotilla)\
**Post date:** [November 12, 2015, 2:47pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/27 "2015-11-12T14:47:21Z")

</div>

You cannot embed one function definition inside another.  
Move all the code from and including "void Complimentary" out of loop()

---

<div class="post-metadata">

**Author:** ![gautam1997](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gautam1997](https://forum.arduino.cc/u/gautam1997)\
**Post date:** [May 5, 2019, 5:39pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/28 "2019-05-05T17:39:36Z")

</div>

Hello, I am trying to build a four wheel drive robot and i want it to follow straight path. The robot is a prototype and i intend to drive it on rough surfaces (like offroads). I am thinking of using mpu 6050 and complimentary filter to fuse gyroscope and accelerometer to get accurate heading and compare it with reference (i.e 0 degree ) and accordingly supply power to the motors.  
I also read a bit about the Jeff Rowberg library on mpu 6050 and I think the DMP6 example of the library gives accurate angles. Can i use it to get angles and then just get the difference between them and reference and then control motors.

I am using arduino uno.  
I am a little new to arduino.  
Will it work or will i need to do something more?

And one more thing.  
Since my robot will be driving on pretty much flat surafces, i think i'll need to keep an eye on only one angle.  
Am i right?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 5:05pm UTC](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303/29 "2021-05-05T17:05:07Z")

</div>

Closed on May 5, 2021, 5:05pm UTC



[Previous page](https://forum.arduino.cc/t/complementary-filter-with-the-mpu6050/210303.md?page=1)
