Skip to content

Sensors & Signal Processing

How to detect a sudden change in a sensor reading

To detect a sudden change in a sensor reading, subtract the previous reading from the new one and test the difference, the delta, against a threshold. At rest the delta is just noise; when something knocks the sensor, one sample's delta jumps by hundreds. The jump is in the delta, not in the reading, so a slow drift to the same place never sets it off.

This reading minus the last

delta = reading − last

Readings of 204, 210, 198, 614, 609 give deltas of 6, −12, 416, −5. Only one is big: that's the knock. The readings before and after don't matter; only how far it moved in one step.

Use abs(delta) so a sudden drop counts as well as a rise.

Pick the threshold

The line has to sit above the noise and below the jump you want:

  • Noise: each reading wobbles by up to ±30, so two in a row can differ by up to 60.
  • A slow turn: a knob turned 20 counts every 50 ms, sampled every 10 ms, makes deltas of at most about 80.
  • A knock: one delta of about 420.

A line at 100 lets the noise and the slow turn through and catches the knock. At 40 the noise alone trips it; at 500 the knock slips under.

Live experiment

Knock it, or turn it slowly

Every 10 ms the program takes delta = reading − last and lights the lamp for half a second when abs(delta) passes the line. Knock the knob from 20 % to 60 % and one delta is about 420; turn it there slowly and none gets past about 80. A line at 100 tells them apart; 40 trips on noise; 500 misses the knock.

Try it yourself: free lesson

“Catch the change” is lesson 3 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.

The code

int last;
int knocks = 0;

void setup() {
  Serial.begin(9600);
  last = analogRead(A2);           // a real reading to start from
}

void loop() {
  int reading = analogRead(A2);
  int delta = reading - last;
  if (abs(delta) > 100) {
    knocks = knocks + 1;
    Serial.print("knock ");
    Serial.println(knocks);
  }
  last = reading;                  // every pass
  delay(10);
}

Sample rate matters

A delta is a change per sample, so it depends on how often you sample. Sample a slow drift once a second instead of every 10 ms and each delta is a hundred times bigger, big enough to look like a knock. Keep the sample time fixed, and pick the threshold for that rate.

Phones detect a fall from a sudden change in acceleration, and a car's airbag fires on the jolt of a crash, not on how fast the car was going. Both are delta rules with a carefully chosen line.

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.

Try it yourself: free lesson

“Catch the change” is lesson 3 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The knob turns slowly, 20 counts every 50 ms, with ±30 of noise. Sampling every 10 ms, the biggest delta you'll see is about…
    Show the answer

    Answer: 80. A 20-count step plus the noise difference between two readings, up to 60: about 80, under a line at 100.

    • Why not “4”? The knob alone moves 20 counts per step, and noise adds more, so the biggest delta is far bigger than 4.
    • Why not “20”? 20 is the knob's own step. Noise on each of the two readings can add up to 60 more.
    • Why not “400”? 400 is the size of a sudden knock. A slow turn never moves that far between two samples.
  2. Question 2 of 3A temperature rising slowly all day gives deltas that are…
    Show the answer

    Answer: Tiny every sample, even though the total change is big. Slow change is many small deltas. A delta rule ignores it, which is what you want for a knock detector and not for a thermometer.

    • Why not “Negative”? A rising temperature gives positive deltas. Negative ones would mean the reading fell.
    • Why not “Zero”? Each delta is small but not zero: the reading is still rising, just slowly.
    • Why not “Huge”? The total change is big, but it's spread over thousands of samples, so each delta is tiny.
  3. Question 3 of 3last starts at 0 and the first reading is 204. On the first pass…
    Show the answer

    Answer: delta is 204, over the line: a false knock. 204 − 0 is 204, so the detector fires once before anything has happened. Read the sensor once in setup() and put it in last.

    • Why not “delta is 0”? last is still 0 on the first pass, so delta is 204 minus 0, not 0.
    • Why not “The program waits”? Nothing in the rule waits. It compares, finds 204 over 100, and fires.
    • Why not “Nothing happens”? 204 is over the line of 100, so the rule fires and counts a knock.
Keep practising: free lesson

“Catch the change” is lesson 3 of 6 in Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.

Questions people ask

Is the delta the same as the derivative?

Nearly. The derivative is the rate of change, delta divided by the time between samples. With a fixed sample time the two differ only by a constant, so a threshold on delta works as a threshold on the rate. Both make noise look bigger, which is why the threshold must sit above it.

Why does my change detector trigger when nothing happens?

The threshold is inside the noise. Two readings that each wobble by ±30 can differ by up to 60, so a line at 40 fires at rest. Raise it above the worst noise delta, or smooth the readings first, at the cost of a little delay.

How do I catch a slow change instead?

Compare with a reference, not with the last sample. Store a baseline, or the reading from a minute ago, and test the difference from that. A slow drift adds up there even though each step is tiny.

Every topic