MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › How do edge detection, debouncing and filtering work together?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

How do edge detection, debouncing and filtering work together?

Edge detection catches "the instant of change", but field signals bounce: buttons chatter for tens of milliseconds, sensors loiter at the threshold, inverter noise sprays glitches. Bounce manufactures a stream of false edges — counts double, states flip madly. The cure is one sentence: filter first, then take the edge — debounce and filtering wash the signal, edge detection only works on clean signal. The sections below cover bounce sources, debouncing, filtering choice and the chain order.

1. Where bounce comes from

SourceTypical durationVictims
Mechanical button bounce5~20 msCounting, one-button toggling
Relay contact bounceSeveral to ~15 msFeedback-signal edges
Proximity switch at the edge of detectionVaries with part swayPart counting, in-position logic
Analog alarms hovering at thresholdVariesAlarm edges firing repeatedly
EMI glitchesMicroseconds to millisecondsAny fast edge detection

A PLC scans in milliseconds, so during a bounce it samples a string of ON/OFF alternations — the edge detector reports an edge for every one of them. That is the mechanism behind "one press, three counts".

2. Debouncing: the clean software confirmation

Debouncing is a "confirmation window": after the signal changes, it must hold for a period (usually 20~50 ms) to count. In ladder: X0's rising edge starts timer T0 (K20); when T0 completes and X0 is still ON, set the "valid ON" flag M10; X0 falling likewise debounces before clearing M10. All downstream logic uses M10's edges, never X0's.

Signal typeSuggested debounceBasis
Panel buttons20~30 msBounce ceiling plus margin
Relay feedback20~50 msContact bounce + operate time
Proximity in-position10~30 msSteady without blunting the takt
Fast process signals≤ 10 ms or hardware filteringResponse must not be sacrificed

High-speed signals (encoders, high-speed counters) go through hardware channels with their own filters — never software-debounce them; the software timers cannot keep pace and pulses are lost wholesale.

3. Filtering: "confirmation window" for bits, "average" for analogs

Digital filtering is the debounce timer above; analog values (or anything judged against a threshold) use numeric filtering: a sliding average over the last N samples, or a first-order lag. The key for alarm edges: judge the edge on the filtered value, and give the threshold hysteresis (alarm at 90 °C, clear at 85 °C) — then the alarm edge never flutters at the boundary.

4. The chain order: one look at the pipeline

Field signal → input filter (hardware) → debounce (software timer) → edge detection → business logic

The order cannot be shuffled: taking the edge before filtering hands every glitch a pass; filtering before the edge dissolves the glitch at the filter stage. Two canonical combinations:

① Counting: sensor → 20 ms debounce → rising edge → counter. Counting goes from luck to deterministic. ② One-button toggle: button → debounce → rising edge → toggle. One press, one flip, no double-jumps ever.

5. Common errors

SymptomCauseFix
One press counts two or threeEdge detection straight on the buttonAdd 20 ms debounce before the edge
Part counts occasionally highPart loitering at the sensing edge, multiple edgesRelocate the sensor + debounce + sample in the stable zone
Alarm fires repeatedly near thresholdNo filtering, no hysteresis on the alarm edgeFiltering + hysteresis band
High-speed pulse counts lowSoftware debounce on a fast signalMove to the high-speed counter hardware channel
Debounced response too dullDebounce time picked arbitrarily largeFollow the type table, verify the takt live
Glitches still trigger occasionallySoftware fine, wiring and shielding badFix the hardware layer: shield earthing, cable separation

6. Notes

Filtering, debouncing and edge detection are three layers of one job: filtering makes the signal clean, debouncing makes the state confirmed, edge detection catches the change. The order is always filter → debounce → edge. Wrap the whole chain in one standard function block (input + debounce-time parameter + edge output) and reuse it project-wide — bounce-class faults then vanish for good.

7. References

Edge instructions and timer usage follow the PLC's programming manual; input filter parameters follow the hardware manuals; signal cabling and interference control follow GB 5226.1 / IEC 60204-1; drawing counting chains to GB/T 4728 symbols eases design review.

8. Cautions

⚠️ Three traps to avoid:
① Software debouncing a high-speed signal — pulses lost in strings, counts forever short;
② Debounce set at 100 ms "to be safe" — fast stations complain the machine is sluggish;
③ Perfect software filtering over an un-earthed shield — the glitches march in regardless.

Remember the division of labour: electrical noise is fixed by wiring, mechanical bounce by debouncing, duplicate logic by edge detection — software is the last line, never asked to repay wiring's debts.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL