Reduce Transient Signals by Using Debouncing Logic

Why Debounce Signals

When a switch opens and closes, the switch contacts can bounce off each other before the switch completely transitions to an on or off state. The bouncing action can produce transient signals that do not represent a true change of state. Therefore, when modeling switch logic, it is important to filter out transient signals by using debouncing algorithms.

If you model a controller in a Stateflow® chart, you do not want your switch logic to overwork the controller by turning it on and off in response to every transient signal it receives. To avoid this, design a Stateflow controller that uses temporal logic to debounce your input signals and determine whether a switch is actually on or off.

How to Debounce a Signal

There are two ways to debounce a signal by using Stateflow:

  1. Filter out transient signals by using the duration temporal operator.

  2. Filter out transient signals by using an intermediate graphical state. Use intermediate graphical state for advanced filtering techniques, such as fault detection.

The duration operator is supported only in Stateflow charts in a Simulink® model.

Debounce Signals with the duration Operator

The model sf_debouncer_using_duration illustrates a design pattern that uses the duration operator to filter out transient signals.

The Debouncer chart contains this logic.

The debouncer design uses the duration(n) statement to implement absolute-time temporal logic.

The initial state for this model is Off. Using the duration operator, you can control what state the model is in based on how long the switch signal, sw, has been greater or less than zero. Once sw has been greater than or equal to zero for longer than 0.1 seconds, the switch moves from state Off to state On. Then, if sw has been less than zero for longer than 0.01 seconds, the switch moves from state On to state Off.

State Logic

The debouncer has two states, On and Off. The duration operator controls which state is active. The logic works as described in this table.

Input SignalStateResult

Retains positive value for 0.1 second

On

Switch turns on

Retains negative value for 0.01 second

Off

Switch turns off

Run the Debouncer

  1. Open the sf_debouncer_using_duration model.

  2. Open the Stateflow chart Debouncer and the Scope block.

  3. Simulate the chart.

    The scope shows how the debouncer isolates transient signals from the noisy input signal.

Debounce Signals with Fault Detection

The model sf_debouncer illustrates a design pattern that uses temporal logic and an intermediate state to isolate transient signals. With this design pattern, you can also include logic to detect faults and allow your system time to recover.

The Debouncer chart contains this logic.

The debouncer design uses the after(n, sec) statement to implement absolute-time temporal logic. The keyword sec defines simulation time that has elapsed since activation of a state.

State Logic

The debouncer chart contains an intermediate state called Debounce. The Debounce state isolates transient inputs by checking whether the signals retain their positive or negative values, or fluctuate between zero crossings over a prescribed period. The logic works as shown in this table.

Input SignalStateTransitionsResult

Retains positive value for 0.1 second

Debounce.On

On

Switch turns on

Retains negative value for 0.1 second

Debounce.Off

Off

Switch turns off

Fluctuates between zero crossings for 0.3 second

Debounce

Off.Fault

Note

The Debounce to Off.Fault transition comes from a higher level in the chart hierarchy and overrides the transitions from the Debounce.Off and Debounce.On substates.

Chart isolates the input as a transient signal and gives it time to recover.

Run the Debouncer

  1. Open the sf_debouncer.

  2. Open the Stateflow chart Debouncer and the Scope block.

  3. Simulate the chart.

    The scope shows how the debouncer isolates transient signals from the noisy input signal.

Use Event-Based Temporal Logic

As an alternative to absolute-time temporal logic, you can apply event-based temporal logic to determine true state in the Debouncer chart by using the after(n, tick) statement. The keyword tick specifies and implicitly generates a local event when the chart awakens.

The Error Generator block in the sf_debouncer model generates a pulse signal every 0.001 second. Therefore, to convert the absolute-time temporal logic specified in the Debouncer chart to event-based logic, multiply the n argument by 1000, as follows.

Absolute Time-Based LogicEvent-Based Logic
after ( 0.1, sec )after ( 100, tick )
after ( 0.3, sec )after ( 300, tick )
after ( 1, sec )after ( 1000, tick )

See Also

Related Topics