Accelerometer and Gyro Integration

Accelerometer and Gyro Integration

Ok, lets start with a little bit more information on Gyro's and Accelerometers to find out why we might want to combine them to get a better tilt angle reading


We can use a gyro to calculate the current tilt angle by by taking a reading at a set frequency, calculating how many degrees we have turned in that period and then summing these values up. This is called integration

E.g. We decide to take a reading 10 times per second
We convert each reading we take into degrees per second (see Gyro Tutorial) and then divide by 10
This gives up the number of degrees we have turned in a particular direction in 1/10th of a second.
1/10th of a second later, we take another reading, calculate the number of degrees turned through and add it to our total.
Assuming we started on a level (0 degrees) then we can keep taking readings, adding them and we have a value for our current tilt angle

Time (seconds) Rotation in degrees Current angle
0 0 0
0.1 5 5
0.2 5 10
0.3 -2 8
0.4 3 11
0.5 0 11
0.6 -6 5
0.7 -2 3
0.8 7 10
0.9 10 20
1.0 -5 15


This is how we use gyro's to calculate tilt angle. It has a number of positive and negative attributes


  • Gyro's respond fast so they are good at producing a quick response to a change in angle


  • We are only taking readings at certain time intervals. We dont know what's happening between these periods
  • For better tilt angle accuracy we need to take more gyro samples which takes more processing time
  • Due to the inaccuracy of each gyro reading, the tilt angle calculated will drift over time


We have seen in the previous tutorial (Accelerometers) that we can use accelerometers to give us a tilt angle. Accelerometers also have a number of positive and negative attributes


  • If not moving, accelerometer will give accurate reading of tilt angle


  • Accelerometers are slower to respond than Gyro's
  • Accelerometers are prone to vibration/noise

If we apply lots of smoothing/averaging to the accelerometer readings we can iron out any noise due to vibration. The upside to this is accurate readings, the downside is a much slower response.

Integrating Gyro's and Accelerometer Readings

So we have Gyros which respond quickly, but drift over time, and we have accelerometers which respond slowly but are accurate over time. We can merge these two sensor readings to give use a quick response which is also accurate

There are two main methods for integrating gyro and accelerometer readings. The Kalman Filter and the Complimentary Filter. Extensive information on each can be found by searching on the internet. I have used both of them and find little difference between them. The Complimentary filter is much easier to use, tweak and understand. Also it uses much less code, so is the one i will use here.

Here is the code for the complimentary filter. It takes as inputs, the angle calculated from the accelerometer, and the rate of rotation in degrees/second from the gyro.
filterAngle is the calculated angle from the filter
dt is the time period between taking readings in seconds (e.g. dt=0.02 is a reading rate of 50 times per second)
timeConstant is a value which is used to determine how quickly the calculated angle is corrected by the accelerometer value. Play around with this value to get the best response/accuracy required.

* Complimentary Filter

float filterAngle;
float dt=0.02;

float comp_filter(float newAngle, float newRate) {

float filterTerm0;
float filterTerm1;
float filterTerm2;
float timeConstant;

timeConstant=0.5; // default 1.0

filterTerm0 = (newAngle - filterAngle) * timeConstant * timeConstant;
filterTerm2 += filterTerm0 * dt;
filterTerm1 = filterTerm2 + ((newAngle - filterAngle) * 2 * timeConstant) + newRate;
filterAngle = (filterTerm1 * dt) + filterAngle;

return previousAngle; // This is actually the current angle, but is stored for the next iteration

Testing the Tilt Angle

Ok, we have calculated the tilt angle, how do i know if it is working? The best way to play around with the filter and tilt angles is to send them to a PC via a serial port and plot them on a graph.

For Windows users there is a handy piece of software called serial chart which will chart data received on a serial port in real time. By diplaying all three values (gyro rate, accelerometer angle and filter angle) you can see in real time how they are all interacting. Download the software here