Sensors & Signal Processing
How to integrate a sensor reading into a running total
To integrate a sensor reading, add it to a running total on every sample. If the reading is a rate, how hard it's raining, how much power is flowing, how fast a wheel turns, the total is the amount: rain fallen, energy used, distance travelled. The total's slope is the rate; twice the rate fills it in half the time.
Rate in, total out
A rain gauge samples a rain-rate sensor every 100 ms. At a steady reading of about 512, the total climbs by 512 each sample and passes 10,000 after 20 samples, about 2 s. Halve the rate and it takes 4 s; double it and it takes 1. A short downpour and a long drizzle can fill the gauge to the same mark.
To get real units, convert the reading to its real rate and multiply by the sample time before adding. The total is then rate × time: watts × seconds is joules, litres per second × seconds is litres.
Noise cancels, bias doesn't
Each reading wobbles by about ±30. In the total those wobbles go both ways and mostly cancel. But a constant error goes the same way every time. A sensor that reads 5 too high, sampled ten times a second, puts the total 3000 off after a minute, and further every minute after. It's why a gyro's angle drifts and why odometers need a reset against something they trust.
Live experiment
Drizzle, rain, pour
Every 100 ms the gauge adds the knob's reading, the rain rate, to a total, and lights the lamp once the total passes 10,000. A drizzle of about 255 a sample takes 40 samples, about 4 s; rain at about 511 takes 20, about 2 s; a downpour near 1015 takes 10, about 1 s.
“Add it up” is lesson 6 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
A gauge with a reset
long total = 0;
void setup() {
pinMode(2, INPUT_PULLUP); // RESET button
pinMode(9, OUTPUT); // lamp
}
void loop() {
if (digitalRead(2) == LOW) total = 0; // RESET pressed
total = total + analogRead(A2);
if (total >= 10000) digitalWrite(9, HIGH);
else digitalWrite(9, LOW);
delay(100);
}
The reset button, on pin 2 with INPUT_PULLUP, empties the gauge. The lamp needs both halves: on above the line, and an else to turn it off again.
Where you'll meet it
Your electricity meter integrates power into kilowatt-hours. A car's odometer integrates speed into distance. A battery fuel gauge integrates current in and out, a technique called coulomb counting, and a robot estimating its position from its wheels integrates their speeds: dead reckoning, with the same drift problem.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Add it up” is lesson 6 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: Adding a stride length to a total on every step. Distance is the total of all the steps: an integrator. Every meter that reports an amount is adding a rate up.
- Why not “Taking the peak”? The peak is the single biggest value. Distance needs every step added together.
- Why not “Averaging”? An average gives a typical step, not how far you've gone in total.
- Why not “Reading the speed”? Speed is a rate. To get distance from it you still have to add it up over time.
Show the answer
Answer: Drifts further and further off. Adding up a small error every sample makes a big error eventually. It's why a gyro's angle drifts, which you'll meet in Dither Delta.
- Why not “Stays right”? Every sample adds the same small error, so the total moves further off with time.
- Why not “Falls to zero”? Adding never makes the total vanish. The error piles up instead.
- Why not “Cancels the error”? Random noise cancels in a sum because it goes both ways. A constant error always goes the same way, so it builds up.
Show the answer
Answer: The lamp stays on: nothing ever turns it off. The if only ever turns it on. With the total back at 0 the if doesn't run, and the lamp keeps whatever it had: on. An else turns it off.
- Why not “The lamp goes off”? Nothing turns it off. With no else, the lamp keeps whatever it was last given.
- Why not “Nothing”? Something does go wrong: the total is back at 0, but the lamp is still lit.
- Why not “The total resets twice”? The reset is the button's job and happens once per press. The lamp is what goes wrong here.
“Add it up” is lesson 6 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 5 before it first.
Questions people ask
What does it mean to integrate a sensor reading?
To add it up over time. If the sensor measures a rate, like power, flow or speed, the running total is the amount: energy, volume or distance. In code that's one line, total = total + reading × sample time, run every sample.
Why does my integrated value drift?
Because the sensor has a small constant error, an offset, and every sample adds it again. Random noise goes both ways and mostly cancels, but an offset always pushes the same way. Measure the offset at rest and subtract it, or reset the total against something you trust.
How do I get the total in real units?
Convert each reading to its real rate first, then multiply by the time between samples before adding. A device drawing 2 W, sampled every second, adds 2 joules a sample; over three hours that's 21,600 J, or 6 Wh.
Related topics
- How a robot knows where it is with dead reckoningAdd up every step in the direction you face, and you know where you are, roughly.
- How to calculate battery life from mAhRuntime = capacity ÷ current: mAh, watt-hours, C-rate, and why real batteries give a little less.
- How a moving average filter smooths a noisy sensorAverage 16 readings for a quarter of the noise, and know what it costs.
- How to detect a sudden change in a sensor readingWatch how fast the reading moves, not where it is, and a knock stands out from a drift.