Sensors & Signal Processing
How to detect a failed sensor in code
A dead sensor doesn't fall silent: it keeps handing your code numbers, and the code believes them. Plausibility checks catch most failures in a few lines: a range check for readings the physics can't produce, a repeat counter for a sensor that has frozen, and a rate limit for jumps faster than the real thing can move.
Out of range
A working sensor stays inside the range its physics allows. A wire shorted to ground reads near 0. A broken wire on an input with a pull-up reads the very top, 1023 on a 10-bit Arduino input. Neither is a real reading, so check both ends:
if (raw < 20 || raw > 1000) fault = true; // shorted, or wire broken
Set the limits just outside anything the sensor can really read. Too tight and a working sensor raises false alarms; too loose and failures slip through.
Frozen
Some failures stay in range: the sensor stops updating and repeats its last value. Real readings always wobble a little, so the same value many times running is the giveaway:
if (raw == last) same++; else same = 0;
last = raw;
if (same > 20) fault = true; // frozen
At 100 readings a second, twenty repeats take 0.2 s. One repeat happens by chance; twenty in a row on a noisy sensor effectively never do.
Live experiment
Break the wire, freeze the sensor
A sensor wanders between about 370 and 630, then fails at 5 s: its wire breaks and it reads 1023, or it freezes on its last value. A low-only check misses both; a range check catches the break at once but misses the freeze; counting repeats catches the freeze 0.2 s in. None raises a false alarm.
“Fault detection” is lesson 5 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.
Too fast to be real
A room thermometer that jumps from 21 °C to 85 °C between two readings 10 ms apart hasn't seen a fire: rooms don't heat that fast. Set a largest believable change per reading and flag anything bigger. That catches glitches, loose connectors and electrical spikes that stay in range.
Which check catches what
| Failure | Range check | Repeat counter | Rate limit |
|---|---|---|---|
| Broken wire, pulled up | Yes, at once | Eventually | Yes, at the jump |
| Short to ground | Yes, at once | Eventually | Yes, at the jump |
| Frozen value | No | Yes | No |
| Single glitch | Sometimes | No | Yes |
A second opinion
The strongest check is another sensor. Two that disagree flag a fault; three can vote and outvote the broken one. Aircraft compare three airspeed sensors, and cars compare two accelerator-pedal sensors every few milliseconds. Whatever the check, decide in advance what the system does when it fires, and make that the safe thing.
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.
“Fault detection” is lesson 5 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 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: Its wire has broken and the input floats high. 1023 is the top of the converter: what an open input pulled up reads. No real reading gets there.
- Why not “The code is wrong”? Code that worked before doesn't suddenly start reading 1023. A pulled-up input with a broken wire does.
- Why not “It's working perfectly”? A sensor that normally reads 370 to 630 can't really reach 1023 and stay there.
- Why not “It got very hot”? A real temperature change would pass through the values in between. A jump to the converter's top, held there, isn't physical.
Show the answer
Answer: A glitch: no room heats 64 degrees in 10 ms. A change faster than physics allows is a third plausibility check: a rate limit. A real fire would show a rise over seconds, not one jump.
- Why not “A hot day”? Weather changes over hours. One reading 64 degrees higher, 10 ms later, is a fault or a glitch.
- Why not “A fire”? Even a fire takes seconds to heat a room. A 64-degree jump in 10 ms is faster than physics allows.
- Why not “The calibration changed”? Calibration doesn't jump between two readings. The rate of change is the clue: too fast to be real.
Show the answer
Answer: Gives false alarms: noise repeats one value now and then. With 61 possible noise values, two equal readings in a row happen about once every 60 steps.
- Why not “Is perfect”? A noisy sensor gives the same value twice in a row by chance, so one repeat would trip it falsely.
- Why not “Misses the freeze”? It does catch the freeze, but only after firing on chance repeats long before.
- Why not “Never fires”? It fires all too easily, on the first chance repeat.
“Fault detection” is lesson 5 of 5 in Dither Delta, Sensors & Signal Processing World 2. Free with an account. Lessons unlock in order, so you’ll do the 11 before it first.
Questions people ask
How can I tell if an analog sensor's wire has broken?
Give the input a pull-up or pull-down resistor, so a broken wire drives it to a known rail instead of floating. With a pull-up, a break reads the top of the range, 1023 on a 10-bit Arduino input, which a range check catches on its first reading.
How many repeated readings mean a sensor has frozen?
Enough that chance can't produce them. With a few dozen counts of noise, two equal readings in a row happen now and then, but twenty in a row effectively never do. A very quiet sensor needs a higher count, or a different check.
What should code do when it detects a sensor fault?
Stop trusting the value and fail safe: turn off the heater or motor it controls, flag the fault, and fall back to a spare sensor or a safe default. Never keep acting on the last reading as if nothing happened.
Related topics
- How Arduino analogRead worksHow analogRead turns 0–5 V into 0–1023, and how to scale it for analogWrite.
- What is a pull-up resistor?Why an input pin needs a pull-up, and why a pressed button reads LOW.
- How a median filter removes spikes from a sensorSort three readings and keep the middle one: a lone spike never gets a say.
- How to calibrate a sensor with two known pointsTwo known readings fix a straight line: where it starts and how steep it is.