I've removed the rotary encoder from the scroll button of a defect mouse. Let's see how we can use that in a microcontroller design.
On the outside, a rotary encoder looks like a potentiometer. On the inside, it's a different beast though.
Instead of a resistor, it has a number of contacts in there that open and close while you're turning the device.
As a result, you get two signals on the output that are 90° out of phase (hence the name quadrature encoder).
If you turn clockwise, B is lagging on A. If you turn counter-clockwise, A lags on B.
Drawing adapted from the Texas Instruments RM46x microcontroller TRM, Enhanced QEP (eQEP) Module documentation
The encoder in my mouse turned out to be a EC101102X2E-VAX. It's a jellybean part that's found back in many devices.
I'll be feeding the signal into a few different peripherals. All of them will want a debounced block signal from the encoder.
What we'll have to do is provide two pull-up resistors so that we get a high pulse when the switch is open, and debounce capacitors to keep the signal reasonably free from glitches.
Literature advices to use .1µF caps. Just because I happened to have .5µ ones on my desk, I used those.
They work well. The signal rises fast enough (the slope isn't really steep because the 10K resistor in series slows down the charging). And as you can see on the captures below, there's no trace of bounce.
You can recognize the excerpt of that TI reference manual drawing in the captures.
Interpreting the Signal
There are a number of possibilities. Some microcontrollers use dedicated peripherals to translate rotary action into data and interrupts that can be used in your firmware.
For others you have to build the logic yourself. That can be done using edge interrupts or other approaches.
With the small test bed I have built here, I can explore some of those mechanics.
That's something for the next posts.