Switch bounce with mechanical switches can be handled in either hardware using a resistor/capacitor RC circuit to constrain the bounce period and/or by handling it with logic.
From the manufacturers specifications the contact bounce at 15rpm is 5.0ms maximum, lets see what its like when connected to the MCP23S17 with one end earthed and being biased toward the 3.3V supply rail via the chips internal 100KΩ pull-up resistors –
As can be seen both switch contacts are noisy and that switch B in particular shows a lot of noise -between the cursors it shows a bounce time of 5.7ms!
Since the GPIO chips are Low-Voltage CMOS – LVCMOS the logical threshold from a logical 0 to a logical one is around 1.65V with a 3.3V Vdd supply rail, so these noisy transitions are easily enough to cause the GPIO pin to register multiple changes.
As mentioned previously these spikes can be tamed using techniques ably described by Jack Ganssle and other methods exist in software such as waiting say 20ms before sampling the switch, by which time the signal should have settled to it’s new value.
The issue with this is that both techniques introduce a time delay, the former by slowing down the voltage rise-time of charging a capacitor to rail voltage via the GPIO chip ‘s 100kΩ resistor and the risk is that if the rotary switch is turned fast then we may miss real transitions and this does not make for a comfortable or intuitive user experience – we humans expect when we turn something or press a switch that we get some feedback that something has happened!
There are lots of real-life examples where designers get this wrong -a common one I’ve experienced is in office elevators. If of the type with in-lift number pads to select your desired floor sometimes you press the button and await stupidly expecting the doors to close and the lift to depart, when you find that either you pressed the button too quickly and the de-bouncing circuit decided that such a momentary change was in fact noise, or sometimes your button press gets ignored.
What’s the solution?- well a mixture of both but with a twist – remember these rotary switches are quadrature encoders- one switch pair leads and the other follows 90° out of phase. We can code for this because if we know the switches current (or prior) state we can test for the expected valid state whether the switch is rotated clockwise or counter-clockwise.
Below shows what the same switch waveform looks like after putting 12nF capacitors across each switch, as you can see this has slowed the rise time of the 0-3.3V transition because the capacitor has to charge up via the GPIO chips internal 100KΩ pull-up resistor and has generally cleaned up the waveform.