Arduino code for simplified Kalman filter. Using a 5DOF IMU (accelerometer and gyroscope combo)
January 22, 2010
This article introduces an implementation of a simplified filtering algorithm that was inspired by Kalman filter. The Arduino code is tested using a 5DOF IMU unit from GadgetGangster - Acc_Gyro . The theory behind this algorithm was first introduced in my Imu Guide article.
The Acc_Gyro is mounted on a regular proto-shield on top of an Arduino Duemilanove board.
Parts needed to complete the project:
- Arduino Duemilanove (or similar Arduino platform)
- Acc_Gyro 5DOF IMU board
- Protoshield (optional)
- Breadboard
- Hook-up wire 22AWG
The hook-up diagram is as follows:
Acc_Gyro <---> Arduino
5V <---> 5V
GND <---> GND
AX <---> AN0
AY <---> AN1
AZ <---> AN2
GX4 <---> AN3
GY4 <---> AN4
Once you have completed the hardware part, load the following sketch to your Arduino.
Run the project and make sure you are receiving an output on your serial terminal (you can start the terminal from your Arduino IDE).
To analyze the data I have developed a small utility called SerialChart. It is open-source so feel free to customize it for your own needs.
Here is the output from SerialChart software:

The test was performed as follows:
- first I was tilting the board slowly (marked "smooth tilting" on the screenshot)
- next I continued tilting the board, but I also started applying some vibration - by tapping the board quickly with my finger (marked "Titlting with vibration noise")
As you can see from the chart the filtered signal (red line) is indeed more immune to noise than the accelerometer readings alone (blue line). The filtered signal was obtained by combining the Accelerometer and Gyroscope data. Gyroscope data is important, because if you would simply average the Accelerometer data you would get a delayed signal. Given the simplicity of the code and of the algorithm I am satisfied with the results. One feature that I would like to add is compensation for the drift effect that you might encounter with some gyroscopes. However the Acc_Gyro board proved to be very stable in this respect, since it has built-in high pass filters.
If you'd like to experiment on your own, I recommended first reproducing this testing setup , then shift slowly towards your application needs. For example you may take the C code and port it to PIC's C18/C30 or AVR-GCC (it shouldn't be too dificult).
Below are some useful resources and their descriptions.
SerialChart executables can be downloaded from here:
Once you start SerialChart application you will need to load the imu_arduino.scc configuration file for this project(included in the imu_arduino.zip) archive.
In this configuration file make sure to update the 'port' settings to Arduino's COM port. On my computer Arduino was detected on COM3, on yours it might be different.
For more information on configuration file syntax see:
http://code.google.com/p/serialchart/wiki/ConfigurationFileSyntax
You can also download and compile SerialChart from Google Code:
http://code.google.com/p/serialchart/source/browse/#svn/trunk
You will need a SVN client to checkout the code (I use RapidSVN for Windows).
SerialChart was developed using Qt SDK from Nokia: http://qt.nokia.com/downloads
UPDATE 2010-03-18
Many people ask me what about the other 2 axis, here is the code that outputs 3 axis, including the SerialChart configuration script.
Imu_Arduino_3axis_output_2010-03-18.zip
I also removed some overhead code that Alex pointed out in the comments, this reduced the interval between samples.
In the example below I rotate the board around the X axis(blue) which is parallel to the ground.I do it by hand so X is not exactly 0, but close. The axes that change are Y(red) and Z(green). Please note the relationship X^2+Y^2+Z^2 = 1. The dashed cyan, magenta and lime lines are unfiltered signals coming from accelerometer alone (RwAcc).

//starlino//






