A mechanical rotary encoder turned one full revolution in a simulation and a simple Arduino-style counter reported anything from 12 to 39 clicks, depending on the random contact bounce. A different decoder, fed exactly the same noisy signal, counted the correct 96 quadrature steps every single time across twenty trials. The difference is not filtering or timing. It is a lookup table that takes advantage of how quadrature signals are built. That table is the reason a knob on a 3D printer or a car stereo can be made from two cheap switches and still count reliably.
Two switches, a quarter cycle apart
What is inside the knob
A common mechanical encoder, such as the Bourns PEC11R series, contains two switch contacts, usually labelled A and B, that open and close as the shaft turns. The PEC11R datasheet describes the output as a “2-bit quadrature code” and lists resolutions of 12, 18 or 24 pulses per 360° rotation, with or without detents, the small clicks felt when turning the knob.
The two contacts are positioned so their on and off periods overlap by half. When A switches, B is in the middle of its state, and vice versa. Each full electrical cycle passes through four states, and only one bit changes between neighbours:
| Step | A | B |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
| 4 | 0 | 1 |
Direction comes free
Turning one way walks down the table: 00, 10, 11, 01, 00. Turning the other way walks up it. Because each state has exactly one neighbour on each side, the order in which the bits change reveals the direction, with no extra wiring. That single-bit-change property is the defining feature of a Gray code, and it turns out to be the key to handling noise as well.

Counting steps, pulses and detents
Four steps per pulse
Each “pulse” in the datasheet is one full trip through the four states, so a 24-pulse encoder produces 96 state changes per revolution. Whether one physical click equals one state change, two, or four depends on the detent option. The PEC11R datasheet lists variants with 24 detents and 24 pulses, with 12 detents, and with no detents, which is why code written for one part number can count two or four times too fast on another.
How fast the signals arrive
The same datasheet lists a maximum operating speed of 60 RPM. For a 24-pulse part at that speed:
24 pulses per revolution × 1 revolution per second × 4 states per pulse = 96 state changes per second
That is one change about every 10.4 milliseconds. Human hands rarely sustain one revolution per second for long, but a fast flick can get close, and a decoder has to keep up with it.
Contact bounce
What the datasheet allows
Mechanical contacts do not switch cleanly. They chatter for a short time as they open or close. The PEC11R datasheet specifies contact bounce of 2.0 ms maximum, measured at 15 RPM. During those 2 ms, the switching line can flip between its old and new value many times before it settles.
The simulation
To see the effect, a Python model generated one revolution of a 24-pulse encoder: 96 state changes, each held for 10.4 ms. At every transition it inserted random bounce on the switching bit, lasting either 0.5 ms or the datasheet’s full 2.0 ms, with the line flipping randomly between old and new values for that period. Each case ran with twenty different random seeds, and two decoders read every signal.

Two decoders, very different results
The naive way
The common beginner approach watches for A rising from 0 to 1 and checks B at that moment: B low means one direction, B high means the other. With clean signals it counted the correct 24 pulses. With bounce, it fell apart:
| Bounce | Naive decoder, true count 24 | State-table decoder, true count 96 steps |
|---|---|---|
| None | 24 | 96 |
| 0.5 ms | 17 to 32 across 20 runs | 96 in every run |
| 2.0 ms | 12 to 39 across 20 runs | 96 in every run |
The naive counter fails because bounce on A’s falling edge creates extra rising edges. When A chatters while falling from 1 to 0, B is already high, so each spurious rise is counted as a step in the wrong direction. The errors do not cancel neatly, and the total drifts by a third or more.
The state-table way
The robust decoder remembers the last valid state and, whenever the inputs change, looks up the pair (old state, new state) in a small table. A move to the next state in one direction counts +1; a move in the other direction counts −1; anything else, a jump across two states, is flagged as illegal and ignored.
Bounce on one contact only ever toggles between two neighbouring states, because the other contact is steady at that moment. So a chattering edge produces +1, −1, +1, −1 and finally settles on +1. All the spurious steps cancel exactly. Across all forty noisy runs, the table decoder counted 96 every time and never saw an illegal jump. The Gray-code layout of the signal does the debouncing. That is a rare case in electronics where the cheapest fix is also the most reliable: no timers, no delays, no tuning, just a rule about which moves are allowed.
Hardware filtering still matters
The datasheet’s suggested circuit
Software that cancels bounce in the count still has to process every bounce edge, and on an interrupt-driven microcontroller hundreds of spurious edges can steal real processing time. The PEC11R datasheet suggests a filter on each line: a 10 kΩ resistor and a 0.01 µF capacitor. It also notes that devices are tested with standard noise reduction filters and that designers should use them.
Sizing the filter
That resistor and capacitor form an RC time constant of 10 kΩ × 0.01 µF = 0.1 ms. It smooths the fastest chatter without slowing the real transitions, which arrive about 10 ms apart even at the datasheet’s maximum speed. The two numbers are a hundred times apart, which leaves plenty of room. A filter with a time constant near 10 ms would start to blur real steps together and lose counts at speed.
For breadboard projects with the part used in this example:
- PEC11R-4220K-S0024 2.7oz 24PPR Incremental Rotary Mechanical Encoder: a Bourns PEC11R with 24 pulses per revolution and detents, the same series as the datasheet used here. See it on Amazon (affiliate link)
As an Amazon Associate I earn from qualifying purchases. A commission may be earned if you buy through this link, at no extra cost to you.

Writing the decoder
Sixteen entries
The full table has 16 entries, one for every pair of old and new 2-bit states. Four pairs mean “no change,” four mean +1, four mean −1, and four are illegal jumps. Packing the old and new states into a 4-bit index, (old << 2) | new, turns the decoder into a single array lookup that runs in a few instructions, small enough for an interrupt handler on any microcontroller.
Polling instead of interrupts
The table also works with a polling loop instead of interrupts, as long as the loop samples faster than the states change. At the datasheet’s 60 RPM limit on a 24-pulse part, states change every 10.4 ms, so reading the two pins once per millisecond gives about ten samples per state, enough to see every step. If the loop is too slow and misses a whole state, the decoder sees a jump of two positions at once. The table flags that as illegal rather than guessing, which is the right behaviour: with a two-state jump there is no way to know which direction the knob turned. A growing count of illegal jumps in a log is a clear sign the polling rate, not the encoder, is the problem.
Turning steps into clicks
Because the decoder counts all four states per pulse, the result usually needs dividing by the number of steps per detent, often 4 on parts with one detent per pulse. Doing the division only when the count lands on a detent position, rather than rounding every step, avoids a knob that sometimes moves a menu by one and sometimes by zero for the same click.
Electrical limits
The PEC11R is a signal device, not a power switch. Its datasheet rates the contacts at 10 mA at 5 VDC, with a rotational life of 30,000 cycles minimum. Pull-up resistors on A and B, plus the suggested RC filter, keep currents far below that rating and give the decoder clean, slow-enough edges to work with.
Sources
- Bourns, PEC11R Series 12 mm Incremental Encoder datasheet: https://www.bourns.com/docs/Product-Datasheets/PEC11R.pdf
