Rotary encoder adventures - Page 1
Forum section: electronics / projects

author: Peabody
author_rank: Super Contributor
timestamp: November 15, 2020, 11:49:41 pm
content: I've been working on and off on developing a very low-overhead servicing routine for rotary encoders without having to add hardware debouncing. What I've done so far is found in a new "Arduino" folder in an old Github repo I started some years ago:https://github.com/gbhug5a/Rotary-Encoder-Servicing-Routines/tree/master/ArduinoIncluded are two Arduino demonstration sketches using pin-change interrupts. The first uses the standard lookup table method where the previous and current pin states form an index into a table of all possible transitions. Interrupts are enabled on both encoder pin lines. In addition to blinking a CW or CCW LED for each tick, the sketch now prints to the serial monitor the total number of interrupts which have been serviced since the last tick, which provides an indication of how bouncy or noisy the encoder switches are.An alternate sketch gives the same output, but enables only one interrupt at a time. When an interrupt occurs, within the ISR further interrupts on that pin are disabled, and interrupts are enabled on the other pin. So none of the bouncing that may take place on the interrupting pin will trigger further interrupts because interrupts are no longer enabled on that pin. The other pin changed state some time ago and is presumed to be stable.There's a PDF file in that folder that provides more detail, but what I've found so far is that the standard method can produce a very large number of interrupts - dozens to hundreds - for a single step from one detent to the next. Ideally, it would be four interrupts for a typical encoder. But it deals very successfully with all that bouncing, and almost always comes up with the right result.In contrast, the alternate method produces far fewer interrupts, but is more error-prone, including producing some false direction ticks. The PDF includes some trial run information on both methods.I was surprised at how many interrupts might be serviced in the standard method. I don't see any ringing on my scope, but the test Nano is running at 16MHz, so it's possible I'm not seeing what's really going on. Only the internal GPIO pullup resistors are used. And I guess it's possible something is wrong with my code.If anyone has an Arduino and an encoder laying around, I'm hoping maybe someone with more expertise than me could take a run at these sketches. No libraries are required.The attached picture shows my "Minty" single-step encoder which I use to make sure my code produces the correct result for all possible transitions. Using this with the two sketches, I get a consistent run of 4's with the alternate sketch when emulating CW rotation, but with the standard sketch, I get this:52 53 130 73 234 46 15 42 75 30I would not have expected the minty switches to bounce that much. They are genuine Radio Shack switches, not cheap imitations. :-) So I'm a bit puzzled by this result.

author: Benta
author_rank: Super Contributor
timestamp: November 16, 2020, 12:14:28 am
content: You should be a bit more careful with terminology.What you're describing/decoding etc. arequadratureencoders.A rotary encoder is a much more complicated device.I do think you're overthinking the design a bit, though.