Go Down

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


I'm pretty sure the boards which only got a gyro on them don't even convert the gyro readings into degrees, but uses the rate as the input to there PID loop.

You can find plenty of firmware for you board. There is no need to write your own. For instance I found this using Google: https://github.com/DaneGardner/KKMulticontroller

You should ask at a quadrocopter forum or something like that instead.


Hi Lauszus
when i try to compile your sketch for the mpu6050  i get the error Kalman does not name a type and line 4 is flashing , i am new to Arduino and not shure how to correct it,

thanks Garwalt


Thanks Lauszus  i had thet file but it didnt work, so i downloaded it again and all is well.
I want to add servo controll to this sketch could you give advice on how i might do this?.



Simply use the output from the Kalman filter as you input to your servo. See: http://arduino.cc/en/Reference/ServoWrite


Thanks Lauszus ,  sorry to be a pain but this is very new to me, the only thing i have a problem with is, is where in the sketch, is the best place to put the servo write command, at the moment i am using this line from another post.


i have tried it in various positions within the loop function, and all it does is send the servo full one way , i am getting data on the serial moniter.



Simply add:
Code: [Select]


Just below:
Code: [Select]
timer = micros();



Hi Laszus the mpu6050 is working fine with your sketch, is there any way to match the angle of the servo to the angle of the gyro, at the moment if the gyro tilts to say 45 degrees the servo only moves to approx 30 degrees.


Just multiply the angle by a constant, as all Servos are a little different.
If you want more control use the writeMicroseconds function: http://arduino.cc/en/Reference/ServoWriteMicroseconds.

For instance you could fine the maximum and minimum values and then use map (http://arduino.cc/en/Reference/map) to map the angle to these values.


The post is very informative. Thank you so much.
I am working on something similar to it.

I am working on a self-balancing bicycle which is my final year project.The main idea is to run the bicycle,powered by a motor, in balanced position by measuring the tilt when ever the cycle tend to fall and using steering control to balance it. In my project, I am using two sensors gyro meter

and accelerometer

to measure the tilt angle.

roll angle of the gyro along the front wheel and 'x-axis' of the accelerometer perpendicular to roll angle,towards the right side of the cycle,parallel to the ground so the roll angle will give the degree/sec and accelerometer will give the displaced angle or tilt angle when the bicycle tends to fall. 'y-axis' of the accelero also comes along the roll angle(I m not using it).

so, firstly I want to know whether the program you used to measure the tilt angle (in your case front and back) is applicable to sensors to accurately measure the tilt angle? (I m not using a imu). It would be of great help if you can make me figure out working of my sensors properly.

nowhere i could get the information about how you placed the axis of gyro and accelero....can you please tell me

please help me


very interesting post, a bit offtopic but I d need to build a 3 axis angle sensor, which should pass me the pitch and yaw of the sensor in real time. I understand that part of this project iwll do exactly that but I am concerned about multi-turn sensing, so to say. I see the sensor will report easily the angles o a "floating" platform where angles go positive and negative starting for a zero/center, but how will it react when going over 360 degrees? Is i tpossible to sense this kind of rotations? Any hint is much appreciated.


I'm not totally sure what you question is? Are you talking about the physical placement of the gyro and accelerometer or something else?
Btw an IMU is simply a device that reports a crafts velocity, angle etc. In this context consist of the accelerometer and gyro that is used to calculate the angle of the device. See wiki for more information: http://en.wikipedia.org/wiki/Inertial_measurement_unit.

I had the exact same problem with my Balancing robot. This is how I solved it: https://github.com/TKJElectronics/Balanduino/blob/711ef4484173694815e27a78f7cbf0dec4911c90/Firmware/Balanduino/Balanduino.ino#L201-L207.
Also check out the Kickstarter for more information about the project: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit/posts.


May 05, 2013, 10:32 am Last Edit: May 05, 2013, 01:08 pm by tamayaytam Reason: 1
Hello guys. I have been following this post for quite a time. Now that i had the gears , i have begun to play with mpu6050 . And i have recently  run the code successfully given in this post. Thanks for sharing.
Eventually what i am trying to do with mpu6050 is to measure sea wave data. An make a wave buoy. Since i am only a newbie in electronics and coding. I made some research on the web and found a scientific article about it. In this article they use an accelerometer to measure wave height and period. Main problem is that while buoy drifts with the wave , wave changes its position relative to the ground. So that acc data can not be used to measure wave accurately. They managed to find a way to compensate directional forces applied to buoy and measure z axis . But off course neither the code  nor how to do info are given. But equations in this project are shared fortunately. I am adding the image to the post. I am now trying to integrate these equations to the mpu6050 's acc readings. First equation is interesting i think i can measure the acc data with it  regardless of tilt or yaw. But i am getting 16xxx something number instead of g. According to mpu 's sensitivity level i am supposed to divide the number to get the g number. If you are interested. Your help would be much appreciated. Thanks in advance.


The MPU-6050 will output it's values as a 16-bit signed number (-32768 to 32767). Depending on the which scaling you use 1g is a certain value. The default is +/-2g. Then 1g would be equal to 16384. Simply divide your readings by 16384 if you want to convert the reading into g's and are using the default setting.
For more information see the datasheet: http://www.invensense.com/mems/gyro/documents/PS-MPU-6000A.pdf at page 13.

Go Up