Go Down

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



I just finished reading the whole thread  XD
As a project for a university course, we are making a 3 axis camera stabilizer. We are using a 6DOF IMU MPU6050 accelerometer and gyroscope. I'm using your code to control the pitch and the roll of the camera (huge thanks by the way for the code and all those explanations you gave to everybody that helped me). As it is impossible to calculate the yaw with this code, I'm trying to combine it with the I2Cdevlib by means of the quaternions.

My idea would be to combine both and so to be able to control the corrected pitch and roll and the non-drifting yaw. But I'm having FIFO overflow problems.
Does anyone try this kind of combination?

Thanks in advance for your reply.

Jun 01, 2014, 08:24 pm Last Edit: Jun 01, 2014, 08:28 pm by ozzie2005 Reason: 1

I just updated the MPU-6050 code. The changes newest code can be found in the Github repository: https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU6DOF/MPU6050.

Hi Lauszus,

The link above seems to be broken, is there anyway you can fix the link?

I searched for your MPU6050 library in google and got the your code for the MPU6050 but I don't know if that is the most recent version. There is something I don't understand from your code:

 accX = ((i2cData[0] << 8) | i2cData[1]) + 2000.0;
 accY = ((i2cData[2] << 8) | i2cData[3]) + 3319.84;
 accZ = ((i2cData[4] << 8) | i2cData[5]) + 664.48;
Where did you get these values in red from? I checked the MPU-6050 datasheet and could not find any of these values. Is there any reason why you add them to the ACCs? and if so, why these values?

I really appreciate your help.
Thank you,


You obviously didn't read the thread very carefully,   because only a few posts ago,   it was stated that those values are to compensate for the zeroing error of his particular device.    Your device will be different.      If you think that the zeroing error,  or asymetrical response,  of the device matters,   you can do you own experiments to measure it,   and then put those numbers into the equation.

I must've missed that post but thank you for your time explaining the reason for these values.



I recently bought a 10 DOF IMU from Drotek (MPU6050 gyrometre & accelerometre + HMC5883 magnetometre + MS5611 altimetre )

I wanted to use the library with Kalman filter  you provided for this board (MPU6050_HMC5883L) with my Arduino Uno.
But I get the following error : i2cWrite failed 2.
So I suspect it's a problem of address (I changed the address of the MPU6050 to 0x69 as mentioned by Drotek without success).
Let's acknowledge that I can run the MPU6050 code without a problem setting the address to 0x69.

Does anyone experienced and solved that problem?

Thanks in advance for any reply.

We know that: The default sensitivity is high, and the sensor returnes 16 bits,a variation of 50 is just a very small variation.
So, when we calculate the angle base on the raw data, what is the resolution of sensor (smallest angle that the sensor can detect)?

Are you by any chance using a Due? There was a bug in the Wire library for the Due: https://github.com/arduino/Arduino/issues/1976 which will be available when a new release of the Arduino IDE is available - or simply compile the IDE yourself.
For now simply ignore the returned value.

It depends on what sensitivity you use. Please see these datasheets for more information: http://www.invensense.com/mems/gyro/documents/PS-MPU-6000A-00v3.4.pdf and http://www.invensense.com/mems/gyro/documents/RM-MPU-6000A-00v4.2.pdf.


No, I'm using a Arduino Uno. It seems that the problem lies in the fact that I can't access directly the magnetometer. Then I found that I should access it through the MPU6050 using it in the by-pass mode. But I'm not sure of the code I add.

Code: [Select]
  // Bypass mode
  while (i2cWrite(MPU6050, 0x6A, 0x00, true));
  while (i2cWrite(MPU6050, 0x37, 0x02, true));

Using that extra code, I'm able to get values of yaw, pitch and roll. But the yaw seems to be drifting. I don't know if it could be linked with the by-pass mode and if I can use another method to get those values.


It seems weird that you can't access both sensors on the same bus, but yes your code looks good to me if you want to use it in bypass mode.

You should be able to simply connect both sensors to SCL and SDA at the same time. Maybe try adding a pullup resistor to 3.3V and see if that solves the problem.


im using a different code
I like to use yours


mpu 6050


First sorry for my bad english. I want to know something from your project MPU6050_HMC5883L Kalman Filter https://github.com/TKJElectronics/Example-Sketch-for-IMU-including-Kalman-filter/tree/master/IMU/MPU6050_HMC5883L.

I recently bought a 10 DOF IMU GY-86 (MPU6050 gyrometre & accelerometre + HMC5883 magnetometre + MS5611 altimetre ) and use arduino mega 2560.

But I get the following error : i2cWrite failed 2. I attached Screen Shot for my problem.
I have changed the address of the MPU6050 to 0x69 as the comment, but didnt change anything.

Could everyone solved this? Or maybe have some experienced as me?

Thanks in advance for any reply.



Whats wrong with the wiring  michinyon?

Could you tell me more about this? I really need it.


Can this combination give me an accuracy of angle measurement upto 5minutes(0.083333degrees).

If not Which sensors/IMU do I use to achieve such high precision/accuracy ?


Hi Lauszus,

I am currently trying to read my digital IMU, the "6 Degrees of Freedom IMU Digital Combo Board - ITG3200/ADXL345" from Sparkfun with my Arduino Due and I am facing some trouble since it's my first time working with Arduino and Microcontrollers at all. My coding skills are the ones of a typical Mechanical Engineer: I know how code looks like.
I read through this blog and saw, that you and Kashif already solved that particular problem but that was a few years ago and I didn't become smarter by reading your exchange of replies.
I do get some data in the Serial Monitor by using the code from the late Fabio Versano (http://www.varesano.net/blog/fabio/my-first-6-dof-imu-sensors-fusion-implementation-adxl345-itg3200-arduino-and-processing) but it doesn't work 100% and I can't find the mistake. The newer FreeIMU Version doesn't work either, because the Due doesn't support EEPROM.
The code I like the best is yours but unfortunately it is for analog IMUs and even though I tried to combine the code of Versano and yours I couldn't get it work.
If there is an easy way to change your code to work with digital IMUs or even better, if you still have some working code for that problem I would be super appreciative if you could send it to me.

Thanks in advance and thanks for the great blog you're having here!

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