Go Down

Topic: Guide to gyro and accelerometer with Arduino including Kalman filtering (Read 568570 times) previous topic - next topic


Hi, thanks for your explanation. i still have one question, does the complementary filtering solve drift issues "forever"? I mean lets say i let the device run for 2 days straight and move it up and down and turn it all the time and then i put it down on my table. Will i measure the same values as i did when i first turned it on two days ago and put it in the exact same space? or is there a remaining integration error that got bigger over time and now i read 2° offset on my x-axis angle where there should have been 0° or something like that? i hope you understand my question.

thanks in advance for your replies.



I actually do not know. I have never tried to let it run for that long, but I believe it is able to do it.
Sorry for the delayed reply!


Hi Lauszus,
thank you for your answer. I tried it, it works.
I have another question:
Is it possible, with the MPU6050 or with any other sensor/sensor-fusion, to measure an exact angle of an object relative to the earth's coordinate system when the object is always affected by a changing acceleration.
For example: a bicycle that drives up a hill. The acceleration of this system is never just the gravity, but always (gravity) + (acceleration produced by the rider). So there would alwys be an incorrect calculated angle (by the accelerationsensors-signals) -> i cant correct the integrated gyro signal -> i always have a false inclination angle. It has to work somehow...
Please share your thoughts and ideas

best regards



Great code, it is very helpful for our project.

The print data I get for the Complimentary filter and the Kalman Filter are both angles.  Is it possible to get linear data as well?
We are trying to build a tracking system with GPS.

All help will be appreciated!


That is the point of the sensor fusion. This example: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/MPU6050 shows how to do it.

It is very difficult to get linear data, as you will need to integrate the accelerometer, so the error will also be integrated.


Hello Lauszus,
i tried to get an accurate angle based on a sensor fusion of accelerometer and gyroscopes using a complementary filter which should, to my understanding, output a signal similiar to a kalman filter output. the formulas im using to calculate the x angle is also in the image, gyro_y is the present gyro-rate.
i rode a bicycle on a completely flat surface (which should return 0 degree relative to earths coordinate system on the x axis) with the MPU6050 attached to it. my calculated angles look horrible, as you can see in the attached image. while i was accelerating the bicycle, of course the acceleromtere readings were biased by that (yellow) so the calculated accelerometer angle gets wrong (up to 10 degrees). Problem is, the filtered angle follows, so i dont get a "true" angle, but my sensor sees a hill where everything is flat. At 13 seconds i hit the brakes and the angle goes negative (again like 10 degrees). after that i accelerate pretty hard and the signals get even worse as you can see. Do you have any idea what i am doing wrong or why the x-angle doesnt stay at 0 degrees, as it is in reality? maybe there is something you can read out of these signals and help me with.

thanks in advance



Hi Lauszus,

I downloaded your code and I'm able to get raw data which to be honest means very little to me. Also tried to run Graph but no data shown, I am not sure where to find instructions about relevant .ino and/or other configuration.

As mentioned, my application is quite simple, I would like to know the displacement in X and Y in 1000/mm, Z would be a nice bonus. The application is a servo platform which I am trying to follow it's location very precisely for a long period of work, say hours.

As a new Arduinoist with no math/physics background I am having difficulties establishing the starting point.

Thanks in advance for your (other?) help.



Hi Lauszus,

Thank you for all the demo and explanation. It is exactly something I'm looking for right now. However the Github site seems to be down from my place. Would you mind send me the codes you wrote for collecting gyro data and balance the robot through email?

My email is : ceciliazy13@gmail.com

Thank you in advance!



we are doing our project (self balancing robot ) using  arduino uno and MPU6050 . we got struck with the programming of i2c interfacing [ http://www.bajdi.com/page/3/ ]   , we are encountering the following errors in our  programme . so we request you to help us solve the errors or to provide us with a new complete programme for interfacing .. we would be glad if u can help us  =( =( :smiley-red: :smiley-slim:

sketch_feb07a.ino: In function 'void setup()':
sketch_feb07a:55: error: 'i2cWrite' was not declared in this scope
sketch_feb07a:56: error: 'i2cWrite' was not declared in this scope
sketch_feb07a:58: error: 'i2cRead' was not declared in this scope
sketch_feb07a:67: error: 'i2cRead' was not declared in this scope
sketch_feb07a.ino: In function 'void dof()':
sketch_feb07a:140: error: 'i2cRead' was not declared in this scope


Try to use the Kalman filter I have provided and see if that helps. You could also try to implement a low-pass filter of the accelerometer reading, to get rid of some of the noise.

This graph code: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/Graph is for these codes: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/ITG3205_ADXL345 and https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/LPR530AL_LY530ALH_ADXL335.

While this one: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/MPU6050/Graph is for the MPU-6050.

You can find the code for my balancing robot here: https://github.com/TKJElectronics/Balanduino.

You need to download the I2C.ino and Kalman.h files in this directory: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/MPU6050 as well. Then simply click on the file name MPU6050.ino.


Thanks for the graph details.
Is there a way to get the axis displacement in mm?



Once started, the sensor is located in X,Y,Z = 0,0,0 and as it moves in space, the serial output shows relative physical location in space measured in millimeters.


You will need some other sensor like a GPS sensor to get the position. See my previous reply to Ked85: http://forum.arduino.cc/index.php?topic=58048.msg1551502#msg1551502.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131