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

rburnett at richieburnett.co.uk rburnett at richieburnett.co.uk
Mon Aug 10 14:57:37 CEST 2015

Totally agree.  Draw out a state diagram showing how the encoder 
sequences through the four possible states when you turn it clockwise 
and when you turn it anti-clockwise.  Then you can program your micro to 
only increment or decrement the variable being controlled for valid 
state changes, and not for invalid transitions or switch bounce.  If I 
remember correctly I wrote my code to only inc or dec the variable after 
two successive state changes in the clockwise or anti-clockwise 
direction.  You actually get two state changes (for a perfect 
"un-bouncy" encoder for every click) so this requirement for "two 
clockwise state changes" or "two anti-clockwise state changes" in a row 
gets rid of the bouncing problem, and still makes the value being 
controlled inc or dec by one for every perceived click of the encoder.  
Hope this helps.

Best regards,


On 2015-08-10 13:06, Hugh Blemings wrote:
> Hiya,
> On 10/08/2015 21:45, Rick Jansen 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?
> I haven't done this for a loong time, but back on the 68HC11 when I
> did a similar thing it had to be a polling system as there weren't
> sufficient interrupt pins for each encoder.
> Fed the outputs of each encoder into parallel input pins.  Four
> encoder per 8 bit port, but one could presumably multiplex them too.
> I seem to recall a scan rate of a kHz or so, a state machine that
> changed based on the previous and current state of each encoders
> quadrature output pins did the trick and would deal with the debounce
> as well.
> With a little trickery the same state machine code could be used for
> each encoders' pair of pins, some bit shifting to turn it into a two
> bit value that the state machine logic could use with the assembly
> equivalent of a C switch() statement.
> Above is a bit rambly, but might point you in the right direction, if
> not I'm sure smarter minds than mine will interject :)
> Cheers,
> Hugh
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at dropmix.xs4all.nl
> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy

More information about the Synth-diy mailing list