[sdiy] Rotary encoders: is there a clever way to handle many?

rsdio at audiobanshee.com rsdio at audiobanshee.com
Tue Aug 11 10:17:14 CEST 2015

On Aug 10, 2015, at 4:45 AM, Rick Jansen <rick.jansen at xs4all.nl> wrote:
> Well, that was fun, or was it. Handling a rotary encoder with an Arduino can be done, but there's a lot to take care of! The signals from my (simple) €1,50 encoder bounce all over the place. My current implementation uses one of the two interrupt pins of the Arduino, although a polling solution is possible as well. I'm not sure if it is because of the "quality" of this encoder, but at times there are nearly as many pulses clockwise as anti-clockwise..  I ended up counting both clockwise and anti-clockwise pulses, the greater of which determines actual direction. Even an "acceleration" is detected, if you turn the rotary fast the value will change more dramatically.
> It works quite well, but at the same time I wonder how other machines work, that track 8 or 16 rotary encoders.. An Arduino Mega has many more interrupt pins than the measly Arduino Uno, but still. Is there a clever trick to track many rotary encoders that I am missing?

A few comments:

1) The biggest issue with rotary encoders is missing one or more states. There are only 4 states, and it's reliable to determine the direction if every state is handled, but missing just one state means that the direction is unknowable. In such a situation (missing a state), the decoder must wait until the next state occurs to discover the direction. If two states are missed then the decoder will incorrectly assume that the encoder was turned in the opposite direction.

Judging by the sheer number of commercial products that will get confused and spin the data value in the opposite direction of the knob, my judgement is that it must be rather easy to miss a state. Your interrupt-based solution is possibly saving you from this particular failure mode.

There are rotary encoders with 4 and 5 bits, making it (hopefully) impossible to become confused about the direction, even when missing 1 or 2 states. Unfortunately, these encoders cost way more than the 2-bit versions. I was unable to find any 3-bit encoders, which was surprising.

2) Check out the schematic for the Oberheim Matrix-12, a famous synth with 6 rotary encoders and a hopelessly slow 8-bit processor. They use a D-type flop flop for each encoder, such that one line provides the clock, and the other line provides the data. In this circuit, the Q output of the flip flop is always '1' when turning one direction, or '0' when turning the opposite direction. The same two raw encoder bits are sent to a NOR gate and a second flip-flop that sets the output high when both lines are low. Presumably, this is the "start" of a click. The flip-flops can also be reset by the CPU. The whole thing is described in the Service Manual. You can either use discrete logic chips or a small PLA.

3) The Freescale K6x family of ARM Cortex-M4 processors have FlexTimer units with external inputs that can be put into a rotary encoder mode. The K64 chip has four of these FTM units, but I'm not sure they all implement the rotary encoder option. Even if they did, that would only handle 4 encoders. I haven't seen this peripheral feature working yet, though, so I can't vouch for it.

4) If you run out of interrupts, you can always combine them with external gates and feed fewer pins, or even just a single pin. When your processor responds to the interrupt, it can read several rotary encoder pins (preferably on a single port) to determine which one actually changed, and still be able to handle changes to multiple at once.

Brian Willoughby
Sound Consulting

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://synth-diy.org/pipermail/synth-diy/attachments/20150811/5d62eaaf/attachment.htm>

More information about the Synth-diy mailing list