Using Kalman filter for Analogue to Digital measurements

The Kalman filter is a very powerful tool to remove noise in data series that can be used in various engineering fields. I’ve used Kalman filter to smooth out sensor data before I display them  on LCD screen or use them to control something.

As I’ve realized the versatility of Kalman filter I wrote the  function below which uses  Kalman filter to remove noise from ADC (analog to digital) measurement. I was able to use this function successfully to smooth out my ADC measurement specially if I need to plot or display the measurement. Here is the function below.


This function takes signed int ADC value and spit out smoothed out unsigned int value. You can play with Q and R value to get desired smoothness. However beware when the results become less effected by the noise that also means your  function is less responsive to fast changes of the ADC as well.

The Kalaman filter keeps record of the previous states; hence you need to have separate function for each ADC channel in case if you need more than one ADC channel. In case if you know the initial value ADC you can  initialize X with that value. In case you don’t know the initial value of the ADC you can just load ADC value with number in the range(0-1023 for 10 bit) and initialize P with a  larger value. In the above function I’ve made P=6 and X=1023 as I don’t exactly know the initial value of the X. The Kalman filter will start converging once it starts get readings.

I am not a genius of mathematics so I will not be able to explain Kalman filter deeply. However there are quite good resources in the internet as to how Kalman filter works. I also recommended book “Kalman Filter for Begineers : with MATLAB examples by Phil Kim” for anybody who wants to learn how to apply Kalman filter for projects. I’ve got my copy recently.

You can call this function soon after you read the ADC.  Try this function and let us know how you’ve used it in your project.

Leave a Reply

Your email address will not be published. Required fields are marked *