Many sensor features eventually become a condition:
if (value > threshold) {
alarm = true;
}
This looks reasonable, but real devices usually need more.
Sensor values jitter. Environments drift. Samples hit spikes. User actions may stay near the boundary. With only one threshold, the device can bounce between triggered and not triggered.
A better model is:
raw value
-> filtered value
-> threshold check
-> hysteresis, debounce, confirmation
-> state machine
-> product action
The threshold is only one part of state detection.
Why One Threshold Bounces
Suppose a water leak sensor alarms when conductivity value exceeds 1000.
If the reading moves around the boundary:
990, 1010, 995, 1005, 998, 1012
A single-threshold rule gives:
dry, wet, dry, wet, dry, wet
That does not mean water really appears and disappears every sample. It may be droplet contact, electrode contamination, ADC noise, or boundary jitter.
The same problem appears in temperature control, current protection, distance detection, Hall switches, PIR triggers, and presence detection.
Hysteresis Separates Enter And Exit
Hysteresis uses two thresholds:
above high threshold: enter alarm
below low threshold: exit alarm
For example:
value > 1000 -> wet
value < 800 -> dry
The range between 800 and 1000 is a hold region. If the device is already wet, it stays wet until the value drops below 800. If it is dry, it only becomes wet above 1000.
This prevents boundary bouncing.
Hysteresis works well for values with physical inertia, such as temperature, humidity, battery voltage, liquid level, and light. It also helps switch-like states such as Hall proximity and water leak probes.
The gap must be larger than normal noise and short-term variation. Too small still bounces. Too large makes recovery slow.
Debounce Handles Short Instability
Debounce is common for buttons, but sensor states need similar treatment.
It handles short unstable periods. A water probe first touching liquid, a Hall switch near magnetic threshold, a PIR trigger starting, or a door sensor mechanically shaking can all toggle briefly.
Common rules are:
3 consecutive wet samples -> enter alarm
5 consecutive dry samples -> exit alarm
Or:
wet for 500 ms -> enter alarm
dry for 2 s -> exit alarm
Enter and exit timing can be different. Alarm entry often needs to be fast. Recovery can be slower to avoid flicker.
Confirmation Reduces Random False Triggers
Confirmation is close to debounce, but the focus is avoiding one bad sample.
A distance sensor may occasionally return a very close value. A current sample may produce a spike. A gas sensor may jump due to interference or internal compensation. One sample should not always become a product event.
A rule can be:
at least 4 of the latest 5 samples exceed the threshold
This is more tolerant than requiring 5 consecutive hits, and it survives one missing sample.
The cost is latency. If the sampling period is 200 ms and the logic needs 5 samples, confirmation may take close to 1 s. That may be too slow for protection, but acceptable for user notification.
State Machines Beat A Boolean
Many device states are not just true or false.
For a leak alarm, useful states may include:
dry
suspected_wet
wet
recovering
fault
suspected_wet means wet signals were seen but not confirmed. wet means confirmed alarm. recovering means readings look dry, but the alarm waits before clearing. fault can represent probe open, short, or invalid values.
A state machine gives each state clear entry conditions, exit conditions, and actions. It is easier to debug than scattered if statements, and easier to explain in field logs.
Thresholds Need Physical Boundaries
Thresholds should not be only numbers found by trial.
They should reflect sensor physics and product cost:
- Temperature thresholds depend on placement, self-heating, and response time
- Current thresholds depend on bandwidth, inrush, and protection action
- Water leak thresholds depend on liquid conductivity, contamination, and low-power sampling
- Hall thresholds depend on magnet direction, air gap, temperature drift, and mechanical tolerance
- Distance thresholds depend on blind zone, target material, angle, and occasional bad measurements
A number tuned on a desk can fail in the field.
A Better Template
Sensor state detection can follow this path:
raw value
-> filter or outlier rejection
-> high / low thresholds
-> confirmation time or count
-> state transition
-> action and recovery
Log the same layers during debugging. When false triggers happen, this makes it easier to locate whether the issue is raw sampling, filtering, thresholding, confirmation, or state-machine design.
A threshold is not one if. It is a conversion rule from continuous readings to discrete states. Hysteresis, debounce, confirmation, and state machines are often what make a sensor feature stable.