RunningAverage: a moving average of the last N readings
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.
Built in on Mokxi: #include <RunningAverage.h> and it builds, with nothing to install. Free, and no account needed.
What RunningAverage does
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 name | RunningAverage |
|---|---|
| Author | Rob Tillaart |
| Include | #include <RunningAverage.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: 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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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() {}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.
Keep going
Try RunningAverage without installing anything
Open the example, change the sketch, and keep your version in a free account.