Arduino library · Data and time

RunningAverage: a moving average of the last N readings

Yes. You can use the RunningAverage library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "The average and spread of the last ten readings", runs on the Arduino Uno R3.

Built in on Mokxi: #include <RunningAverage.h> and it builds, with nothing to install. Free, and no account needed.

What RunningAverage does

RunningAverage by Rob Tillaart keeps the last N values you give it in a ring buffer and tells you their average, so a jumpy analogRead() or a temperature that flickers by a tenth of a degree settles into a steady number. Each new value pushes out the oldest one, so the average follows real changes after N readings and ignores single spikes.

Beyond the average it keeps the statistics you would otherwise write by hand: the standard deviation (how much the readings spread), the smallest and largest value still in the buffer, the count of values so far and the sum. getAverage() works out the average from the whole buffer; getFastAverage() keeps a running sum and is quicker for large buffers.

Choosing N is a trade. Ten readings at one a second smooth well but take ten seconds to follow a real step; four readings follow fast but smooth less. The two examples show both: one feeds 30 values swinging 4 either side of 50 into a buffer of 10 and gets an average of 50 with a spread of 4.2, the other feeds six rising values into a buffer of 4 and prints how the average lags behind.

Library Manager nameRunningAverage
AuthorRob Tillaart
Include#include <RunningAverage.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <RunningAverage.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "RunningAverage" by Rob Tillaart from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RunningAverage to lib_deps in PlatformIO. Mokxi follows the calls of release 0.4.9, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

RunningAverage readings(10)

A buffer for the last 10 values, allocated on the heap. Keep N modest on an Uno; each value is a 4-byte float.

readings.clear()

Empties the buffer and resets the count. Call it in setup() and whenever old readings no longer apply.

readings.addValue(analogRead(A0))

Adds a value, dropping the oldest once the buffer is full.

readings.getAverage()

The average of the values in the buffer, or NAN if it is empty.

readings.addValue(analogRead(A0));
Serial.println(readings.getAverage(), 1);

readings.getStandardDeviation()

How far the values in the buffer spread from their average. A large number means a noisy or changing signal.

readings.getMinInBuffer()

The smallest value still in the buffer. getMaxInBuffer() gives the largest.

readings.getCount()

How many values the buffer holds, up to its size. Until it is full, the average is over fewer values.

readings.fillValue(512, 10)

Fills the buffer with a starting value so the first averages are not pulled toward the early readings.

Example: The average and spread of the last ten readings

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "RunningAverage.h"

RunningAverage readings(10);

void setup() {
  Serial.begin(9600);
  readings.clear();
  // a reading that swings 4 either side of 50
  for (int i = 0; i < 30; i++) {
    readings.addValue(50 + ((i % 2) ? 4 : -4));
  }
  Serial.print("average ");
  Serial.println(readings.getAverage(), 1);
  Serial.print("spread ");
  Serial.println(readings.getStandardDeviation(), 1);
  Serial.print("kept ");
  Serial.println(readings.getCount());
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

getAverage() prints nan

No value has been added yet, or the buffer could not be allocated. Add at least one value before reading, and keep N small enough for the RAM on boards like the Uno.

The average reacts too slowly to real changes

The buffer is too long for how often you add values. Shrink N, or add values more often, so the buffer covers a shorter stretch of time.

getMin() and getMinInBuffer() give different answers

getMin() is the smallest value ever added since the last clear(), while getMinInBuffer() looks only at the values still held. Use the buffer form for a moving minimum.

Questions and answers

RunningAverage or a Kalman filter?

A moving average is easy to reason about and treats every reading in the window the same. A simple Kalman filter weighs each new reading against its estimate and uses less memory.

Try RunningAverage without installing anything

Open the example, change the sketch, and keep your version in a free account.