I wrote a Arduino library to smoothen jitter in analog output signals. This can be very useful when used with potentiometers, temperature sensors or similar devices.
The analogReadSmooth() function averages consecutive output readings. You can define how many readings you want to average (window size). Choosing a large window size will smoothen the output considerably but will also slow down detection of actual signal changes. You need to adjust the window size according to your needs when instantiating the AnalogSmooth object.
Here a brief usage example:
#include <AnalogSmooth.h> int analogPin = 1; // Defaults to window size 10 AnalogSmooth as = AnalogSmooth(); // Window size can range from 1 - 100 AnalogSmooth as100 = AnalogSmooth(100); void setup() { Serial.begin(9600); } void loop() { // Regular reading float analog = analogRead(analogPin); Serial.print("Non-Smooth: "); Serial.println(analog); // Smoothing with window size 10 float analogSmooth = as.smooth(analog); Serial.print("Smooth (10): "); Serial.println(analogSmooth); // Smoothing with window size 100 float analogSmooth100 = as100.analogReadSmooth(analogPin); Serial.print("Smooth (100): "); Serial.println(analogSmooth100); Serial.println(""); delay(1000); }
The library can be downloaded on GitHub and then simply needs to be placed in the Arduino libray folder.
Hello Michael.
This code is just what I’ve been looking for! You’re objected oriented approach has made my current program less
of an eyesore to look at! Certainly easier to smooth multiple analog inputs, Cheers!
Hey Michael!
Thank you for your work. This library is very easy to use, and working flawless good! Many thanks!
ALSO thanks here. I’ve got a stupidly complex program that just needs a bit of analogread love, and all of the averaging techniques I’ve dealt with have been a pain in the but when it comes to cluttering up my code. This is so clean that I don’t have to sweat it. MUCH appreciated for sharing!
Thank You for the wonderful library, Michael! This saved my project from the erratic signals of a potentiometer. I was trying to control an led interface using a potentiometer and I needed stable values for that and your library did the trick!
Just what I needed. Thanks for the help.
That’s just great!
I have been trying for weeks to smooth the signal without messing up the sensor values. Unfortunately unsuccessful. Til today! Thank you for your work and that you share it with us.
Best regards
Thank you for making this. I don’t know much about programming and my smoothing attempts are either slow or weird. This helps a great deal.
hey how are ya?. I have a dumb question as I am a newby. What is being referred to when you say window size?. is it something to do with the actual window size of some sort for refreshing, or is it the number of samples to use for smoothing??. Looks like an awesome library once I am clear on how to use it!!!. Thanks much
The window size refers to how many values are being averaged. So if you have a window size of 10 the 10 last measured values are being averaged. Therefore the bigger the window size the less jittery the signal but also the slower the output corresponds to actual signal changes. You need to determine what window size works best for your specific application.