SimpleKalmanFilter: smoothing one noisy reading
SimpleKalmanFilter by Denys Sene is a one-dimensional Kalman filter in a few lines: you pass it each new reading, it returns a better estimate. It suits a single value that should change slowly but arrives noisy, such as a distance from an ultrasonic sensor, a pressure reading or a potentiometer on a long wire.
Built in on Mokxi: #include <SimpleKalmanFilter.h> and it builds, with nothing to install. Free, and no account needed.
What SimpleKalmanFilter does
Where a moving average treats every reading in its window the same, the filter weighs each new reading against what it already believes. The weight is the Kalman gain. When the filter is unsure of its estimate the gain is high and new readings move it a lot; as it settles the gain falls and noise moves it little. The process noise keeps the gain from falling to zero, so the estimate can still follow a real change.
The constructor takes three numbers: how much the measurement is expected to vary, the starting uncertainty of the estimate (often set the same), and the process noise, usually small such as 0.01. The examples start from 0, feed it readings around 25 or around 50, and show the estimate settle near the true value with the gain dropping below a half. The filter needs only three floats of state, so it fits easily on an Uno.
| Library Manager name | SimpleKalmanFilter |
|---|---|
| Author | Denys Sene |
| Include | #include <SimpleKalmanFilter.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 <SimpleKalmanFilter.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "SimpleKalmanFilter" by Denys Sene from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add SimpleKalmanFilter to lib_deps in PlatformIO. Mokxi follows the calls of release 0.2.0, 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.
SimpleKalmanFilter filter(2, 2, 0.01)
Measurement uncertainty 2 (in the units of your reading), estimate uncertainty 2, process noise 0.01.
filter.updateEstimate(analogRead(A0))
Feeds in one reading and returns the new estimate. Call it once per reading, at a steady rate.
float raw = analogRead(A0);
float smooth = filter.updateEstimate(raw);
Serial.println(smooth);filter.getKalmanGain()
The weight the last reading got, from 0 to 1. Near 1 the filter follows readings closely; near 0 it mostly ignores them.
filter.setProcessNoise(0.05)
Lets the estimate move faster. Raise it if the output lags behind real changes.
filter.setMeasurementError(4)
Tells the filter how noisy the sensor is. Raise it for heavier smoothing.
filter.getEstimateError()
The filter’s current uncertainty in its own estimate, which shrinks as it settles.
Example: Smoothing a noisy reading
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <SimpleKalmanFilter.h>
SimpleKalmanFilter filter(2, 2, 0.01);
void setup() {
Serial.begin(9600);
randomSeed(1);
float estimate = 0;
for (int i = 0; i < 200; i++) {
float noisy = 25 + random(-20, 21) / 10.0; // 25 give or take 2
estimate = filter.updateEstimate(noisy);
}
Serial.print("estimate ");
Serial.println(estimate, 1);
Serial.println(estimate > 24 && estimate < 26 ? "settled near 25" : "not settled");
}
void loop() {}Common errors and how to fix them
The output lags far behind when the real value changes
The process noise is too small, so the gain has dropped near zero and new readings barely count. Raise the third constructor argument or call setProcessNoise() with a larger value.
The output is barely smoother than the input
The measurement uncertainty is set too low for how noisy the sensor really is. Print a few raw readings, see how far they swing, and pass roughly that spread as the first argument.
The estimate starts at 0 and takes seconds to reach the reading
The filter starts with an estimate of 0. Feed it a few readings in setup() before you use the output, or start with a larger estimate uncertainty so the first readings carry more weight.
Questions and answers
Is this a full Kalman filter like the ones for an IMU?
No. It tracks one value with no model of how it moves. Fusing a gyroscope and accelerometer needs a multi-state filter, or a fusion library such as Madgwick.
Keep going
Try SimpleKalmanFilter without installing anything
Open the example, change the sketch, and keep your version in a free account.