Skip to main content

Debouncer

Easydifficulty

A low-pass filter answers "what number should I believe?" A debouncer answers "did this actually happen?" It's a few lines of code and it stops the robot from reacting to one-loop glitches.

One press can look like many​

A mechanical switch doesn't close cleanly. Its contacts bounce between open and closed for a few milliseconds. Optical and magnetic sensors flicker too, from electrical noise, a shiny surface, or a part sitting right at the edge of their range. If your intake changes state on every raw change, one game piece can look like three.

Require an uninterrupted dwell time​

Keep two values: the raw input and the debounced output. When the raw input changes to something different from the output, start a timer. Only copy the new value to the output if the input stays that way for the whole debounce time TdT_d. Any bounce back resets the timer.

So the output lags the input by up to TdT_d, on purpose, and it never changes on a pulse shorter than TdT_d.

Raw inputHas it stayed changed for TdT_d?Output
false → true for 8 ms, then falseNostays false
false → true for 50 ms with Td=30T_d = 30 msYesbecomes true after 30 ms
already truen/astays true

Choose which transition needs proof​

WPILib defines three modes, named for which change of the output has to wait:

ModeWaits beforeChanges immediatelyFTC example
Risingfalse → truetrue → falseconfirm a beam break really sees a game piece
Fallingtrue → falsefalse → trueconfirm a released limit switch is truly released
Botheither transitionneithermake the displayed state stable in both directions

Debounce the change where a false alarm is expensive. For a beam break, rising-only is often right: wait before deciding a piece is there, but clear it as soon as it leaves. Only debounce both ways if the extra delay in both directions is safe.

Warn
A debouncer doesn't run things once

The output is a steady true or false. To run an action once, compare this loop's output with last loop's and act on the change. Don't start the action every loop while the output stays true.

A time-based FTC implementation​

Pass in a timestamp in seconds every loop. The constructor takes the sensor's current reading so the robot doesn't see a fake change at startup. Because it's based on time rather than loop counts, 30 ms means 30 ms even when the loop slows down.

FTCDebounce a boolean input with rising, falling, or both edges
public final class BooleanDebouncer {
public enum Mode { RISING, FALLING, BOTH }

private final double debounceSeconds;
private final Mode mode;
private boolean output;
private boolean candidate;
private double candidateSinceSeconds;

public BooleanDebouncer(boolean initialValue, double debounceSeconds, Mode mode) {
if (debounceSeconds < 0.0) {
throw new IllegalArgumentException("debounceSeconds must be nonnegative");
}
this.output = initialValue;
this.candidate = initialValue;
this.debounceSeconds = debounceSeconds;
this.mode = mode;
}

public boolean update(boolean input, double nowSeconds) {
if (input == output) {
// The candidate disappeared before it was accepted: cancel its timer.
candidate = output;
candidateSinceSeconds = nowSeconds;
return output;
}

if (input != candidate) {
candidate = input;
candidateSinceSeconds = nowSeconds;
}

if (!shouldWait(output, candidate)
|| nowSeconds - candidateSinceSeconds >= debounceSeconds) {
output = candidate;
candidateSinceSeconds = nowSeconds;
}
return output;
}

private boolean shouldWait(boolean from, boolean to) {
switch (mode) {
case RISING: return !from && to;
case FALLING: return from && !to;
default: return true; // BOTH
}
}
}

Use one debouncer per signal, create it during init, and call update once per loop. This beam break reads low when blocked, so !beamBreak.getState() means "a piece is present."

FTCTrigger once when a debounced beam break becomes true
private final ElapsedTime runtime = new ElapsedTime();
private DigitalChannel beamBreak;
private BooleanDebouncer pieceDebouncer;
private boolean hadPiece;

public void init() {
beamBreak = hardwareMap.get(DigitalChannel.class, "beamBreak");
beamBreak.setMode(DigitalChannel.Mode.INPUT);

boolean piecePresent = !beamBreak.getState(); // invert only if your wiring is active-low
pieceDebouncer = new BooleanDebouncer(
piecePresent, 0.030, BooleanDebouncer.Mode.RISING);
hadPiece = piecePresent;
}

public void loop() {
boolean rawPiecePresent = !beamBreak.getState();
boolean piecePresent = pieceDebouncer.update(rawPiecePresent, runtime.seconds());

if (piecePresent && !hadPiece) {
advanceIntakeState(); // runs once, after 30 ms of continuous detection
}
hadPiece = piecePresent;
}
ExerciseSize the delay from evidence

You log the raw input while triggering the sensor over and over. The longest false pulse is 12 ms, and real detections stay high for at least 80 ms. What debounce time do you start with, and what does it cost you?

Show solution

Around 20 to 30 ms. That's longer than the 12 ms glitch, so the glitch can't get through, and it still leaves 50 ms of margin before the shortest real detection ends. The cost is that the robot reacts 20 to 30 ms later than it could. Test it on your mechanism instead of copying a number that worked somewhere else.

Don't use it to hide a wiring bug​

Debouncing removes short flickers. It can't fix a loose connector, inverted logic, or a sensor that never settles. Put the raw value on telemetry first and check which physical situation reads true. Then pick the delay from the glitches you actually recorded. For safety limit switches, make sure the safe direction reacts fast enough; a long delay in both directions can be the wrong choice.

Sources & references
  • WPILib: Debouncer - the source for the rising, falling, and both-edge definitions used here.
  • Signals & Noise - identify whether a digital input is bouncing, electrically noisy, or being read with the wrong polarity before adding a filter.
  • Low-Pass Filter - use a numeric smoother for continuously valued sensor jitter; use this lesson's debouncer for a boolean event.