PROBLEMS WITH ROTARY ENCODERS?
CLICKS (DETENTES) AND PULSES PER REVOLUTION
Some users have reported problems when using rotary encoders with detents (“clicks”). The detents are typically specified as for example:
- 15 pulses per revolution / 30 detents
- 24 pulses per revolution / 24 detents
15 pulses per revolution / 30 detents means “two detents per pulse” ; 24 pulses per revolution / 24 detents means “one detent per pulse”
The problems that have been reported is that there are two changes per click.
The rotary encoder routine used in the current version of the s/w is very simple: you generate an interrupt when there is a change in signal A and immediately compare the value of signal A with the value of signal B. The current version of the software specifies the interrupt handler like this:
attachInterrupt(0, rotEncoder, CHANGE);
This means that for every pulse (or cycle) in the rotary encoder there are two interrupts: rising edge of the pulse (one change) and falling edge of the pulse (another change). If your rotary encoder has one detent per pulse (like the image below), then you will be processing two interrupts and therefore making two changes. See figure below.
In order to make a single change per detent, change the code to:
attachInterrupt(0, rotEncoder, FALLING);
attachInterrupt(0, rotEncoder, RISING);
attachInterrupt(0, rotEncoder, LOW);
The above change will cause the interrupt handler to generate an interrupt only with the rising or the falling edge of the pulse but not both. The “LOW” option triggers the interrupt when the signal goes low.
The current code considers CCW (counter clock-wise) direction when signal A = signal B. CW direction is when signal A is not = signal B. This means that you could use either RISING, FALLING or LOW. For best results you can try each and see what works best for you.
WHAT WORKS BEST FOR ME
I have been using rotary encoders that are “two detentes per pulse”. Recently I’ve used a rotary encoder that is “one detente per revolution”. I tried all three settings for the attachinterrupt routine (FALLING, RISING and LOW). “LOW” worked best for me. FALLING and RISING did not work as well: some clicks did not register, some clicks were read in the opposite direction (increasing motion was read as decreasing and vice versa)
If you have a rotary encoder with no detents, then making the change will cut the number of changes per revolution in half. If you feel your rotary encoder is “too sensitive”, then you can use the above change to make it
For more info see the Arduino reference on attachInterrupt.
CHOICE OF ROTARY ENCODER
I have two rotary encoders that I have tested the code with: one with detents and one without detents. The One with detents is a discontinued (over 10 years ago!) Panasonic model (Panasonic EVQ-WTEF2515B) available in eBay for $6-$7 (Used to cost $1.50 at Electronics Goldmine, but sold out over there). I purchased this long time ago when I started to write code for the WM8741 and had forgotten how this rotary encoder behaves.
According to the datasheet, the Panasonic encoder has 15 pulses per revolution and 30 detents. This means two detents per pulse. That is why I had to use “CHANGE” as the condition for the interrupt handler.
I find ~30 clicks per revolution to be a good number for this application. Whether they are 1 or 2 clicks per pulse it probably doesn’t matter; you just have to make sure you use the interrupt routine properly as shown above
Stock of encoders change frequently so basically you need to look for a “mechanical encoder” with built-in switch and 30-40 detents.
BOURNS has two suitable encoders:
ALPS also has suitable encoders: 30 detents/15 pulses (with switch) Model EC11B15242AE.
Since availability varies, you will need to search for the current stock. A recent search for Alps mechanical encoders with switch, 30-36 detents turned numerous different models [link]. You can compare the different models with this listing [link] from Alps and take note on how you want to mount the encoder. For example:
- The 11B series is a horizontal configuration and threaded shaft (so you can attach it to a panel)
- The 11E and 11K series is a vertical configuration and not threaded (cannot be easily attached to a panel)
Notice the location of the detents with respect to the pulse and compare with the diagram above. There we have the pattern for rotary encoders that are “two pulse per detente”.
ALPS Model EC11B15242AE
HOW TO SELECT A ROTARY ENCODER
(For the Hifiduino code)
- Having a switch
- Having detents (or clicks). 24 to 36 clicks work well
- Threaded if you intend to mount it on a panel
- Either single or double detent per pulse works well and can be adapted by the code