Tutorial 23: Smoothing Data

Arduino Course for Absolute Beginners

Smoothing Data

Have you ever sat down and tried to figure out your finances and come up with a budget? Let’s say you’re looking at last year’s expenditures and determine that you spent $642 of your hard-earned cash on ice cream and beer – that’s roughly $53 dollars per month.

Even though one month you might have spent $75 and the next month only $30, the average over the year gives you the “big picture” of what you spent overall. This average is useful to help determine the budget for those items next year.

Sometimes sensor data varies like this too. One reading is 75 and the next is 30, but if you average the inputs over time you get a number that falls around 53. This variability may be due to a rapidly changing environment or an imprecise sensor. Since Arduino can sample sensors super fast – hundreds of times a second – averaging inputs can help smooth this variability.

Smoothed output is helpful when you are collecting trend data i.e., “Is a value increasing or decreasing over time?”

Smoothed data creates a more stable output. Suppose you are using the input to drive a small DC motor – controlling its speed with smoothed data keeps the acceleration fluid – not jumpy.

There is a trade off here of course, the more you smooth the data the less detailed it becomes. If you smooth too much, instead of getting accurate information you may eliminate the useful variability you are trying to capture. It’s a lot about trial and error. What works with the given set of restrictions you have? The degree of smoothing necessary will vary with each application.

To help us smooth inputs we revisit an awesome data structure – the array. As you may recall, an array is simply a list of items. The array is useful for smoothing data because we can store multiple sensor readings in an array and then perform simple arithmetic to calculate an average.


If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.

You Will Need

  1. Potentiometer (1) any resistance
  2. Jumper Wires (3)

Step-by-Step Instructions

  1. Place the potentiometer into the breadboard.
  2. Run a jumper wire from the 5V pin of the Arduino to either one of the outside pins of your potentiometer.
  3. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer.
  4. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer.
  5. Plug the Arduino into your computer.
  6. Open up the Arduino IDE.
  7. Open the sketch for this section.
  8. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished.
  9. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished.
  10. Open the Serial Monitor window.
  11. Adjust the knob of the potentiometer and see the resulting smoothed data on the Serial Monitor window.

The Arduino Code

/*

  Smoothing

  Reads repeatedly from an analog input, calculating a running average
  and printing it to the computer.  Keeps ten readings in an array and
  continually averages them.

  The circuit:
    * Analog sensor (potentiometer will do) attached to analog input 0

  Created 22 April 2007
  By David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe
  http://www.arduino.cc/en/Tutorial/Smoothing

  This example code is in the public domain.


*/


// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}

void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println(average);
  delay(1);        // delay in between reads for stability
}

Discuss the Sketch

We begin the adventure of smoothing data by declaring and initializing some variables:

const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input

int index = 0; // the index of the current reading

int total = 0; // the running total

int average = 0; // the average

int inputPin = A0;

Wonder why that first variable is a constant? To explain this take a look at the next variable declaration, it is the array we will use to store our sensor data. Notice that the size of this array is determined by the constant numReadings. Arduino will not allow any variable other than a constant integer to be used for array size declaration (otherwise, you get an error). The reason for this is because every variable that is declared is allotted a specific amount of space in the microcontroller’s memory. The same is true for arrays. If you could change the size of the array while the program is executing then nothing would stop you from accidentally running out of memory.

You can also hard code a number for the array size declaration – but I prefer a constant variable anyway.

The rest of the variables are straightforward, well named and commented.

The setup() of this sketch is much like any other, the standard Serial.begin() to start serial communications, but what is interesting is that we use a for loop to set all the elements in the array equal to zero.

for (int thisReading = 0; thisReading < numReadings; thisReading++) {

  readings[thisReading] = 0;

}

Take a look at for loop in this setup(). Do you remember the three components of the for loop header? [The header is the term that refers to the code inside parentheses following the word for.]

The first is the counter variable, then the condition and finally the incrementation. Each time through the loop we are setting an element in the array equal to zero – until all 10 elements are equal to zero and the array is initialized.

The real action happens in the loop(). Here is an overview of what the loop() will do (beware – this is going to sound like a lot, but we will be covering it step-by-step).

The loop() averages the readings that are stored in the readings[] array. To average the readings, the variable total keeps a running tally of all the sensor values that are collected. Every time through the loop we replace one reading in the array with a new reading. To do this, we subtract the old reading from the running total and then record and add the new reading. Finally, we divide the total by the number of elements in the array to determine the average. Sound confusing? Let’s take a look at the code:

// subtract the last reading:

total= total – readings[index];

     0    = 0    -   0

The first time through the loop(), the variable’s total and index are both equal to zero. We haven’t even taken any sensor readings yet, so the readings[] array is filled with zeros. The calculation at this line of code boils down to:

0 = 0 – 0

Not very exciting. For example, let’s pretend that we already have ten readings in our array:

{5, 6, 8, 10, 6, 7, 7, 7, 9, 6}

If you add all of these values they equal 71. Let’s also imagine the total variable is equal to 71.

If we look at that first line of code again, it basically says “take the current total and subtract the first element in the array, now save this as the new total”:

// For demonstration

total= total – readings[index];

71    = 71    -   5 // the total variable now equals 66

Since the number 5 is the first value in the array the total now equals 66. Now let’s record a new sensor reading to replace the value (5) that we just subtracted:

// read from the sensor:

readings[index] = analogRead(inputPin);

The index variable is still equal to 0, this line of code places a new reading from the analog pin into the first element of the array.

Now say the number 7 is the new value recorded by analogRead(). Therefore, the number 5 is replaced by the number 7. The array now contains these elements:

{7, 6, 8, 10, 6, 7, 7, 7, 9, 6}

The next step is to add this new sensor value (7) to the total variable.

// add the reading to the total:

total = total + readings[index];

66   = 66   +    7 //total now equals 73

We take the total variable and add the 7 we just recorded and saved in the first element of the array. This step updates the total variable. This updated total variable needs to be divided by the number of elements in the array to determine the average.

Before we calculate the average let’s increment the index variable by one. Now the next time through the loop(), the index variable will point to the second element in the readings[] array:

// advance to the next position in the array:

index = index + 1;

To prevent the index variable from exceeding the number of elements in the array we use an if statement:

if (index >= numReadings)    {

  // ...wrap around to the beginning:

  index = 0;

}

This statement says “If the value of index is equal to or greater than numReadings then set index back to zero.” Recall that numReadings happen to be the size of the array – by enforcing this condition and resetting the count back to zero, we prevent exceeding the index of the array.

Can we average already?! Yes – finally we calculate and send the average to the serial monitor:

// calculate the average:

average = total / numReadings;

 // send it to the computer as ASCII digits

Serial.println(average);

Now, instead of seeing the raw values flash across the serial monitor, you should see a more consistent, non-jumpy value.

Let’s recap the steps in the loop():

  1. Subtract a value from the total
  2. Record a new sensor value and put it in the array
  3. Add the new sensor value from the array to the total
  4. Increment the array index variable
  5. Make sure the array index is within bounds with an if statement
  6. Divide the total variable by the number of elements in the array to calculate the average
  7. Print the average to the serial monitor

That’s the scoop! These steps provide a smooth output. Increasing the elements in the array will increase the smoothing effect.

Likewise, reducing the number of elements in the array will reduce the smoothing effect.

Try On Your Own

  • Adjust the variable numReadings and monitor the output – anticipate some problems with increasing or decreasing this value.
  • Use the smoothed output to adjust the brightness of an LED at pin 9.

Further Reading

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code