Analog Smoothing Library for Arduino

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.

I'm available for contracting work. Check out my LinkedIn profile and my portfolio page for an overview of my skills and experience. If you are interested in working with me please use the contact form to get in touch.

Analog Smoothing Library for Arduino

5 thoughts on “Analog Smoothing Library for Arduino

  1. Dennis Planner says:

    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!

  2. Novák István says:

    Hey Michael!

    Thank you for your work. This library is very easy to use, and working flawless good! Many thanks!

  3. Andrew Morrow says:

    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!

  4. Aniansh Raj Singh says:

    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!

Leave a Reply

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