Seven whole days! That’s how long I had to work to finally get my rotary encoder to work with the Arduino!
The idea is to be able to quickly select a preset on a guitar pedal board using a rotary encoder instead of a couple of push buttons.
The rotary encoder I’m using uses pretty simple technology.
The parts are as follows (photo on the left, clockwise from top left):
- Complete switch
- metal tab holding everything together
- shaft assembly.
- plastic disk with a metal ring and tabs
- body of the switch, with metal key embedded
- Ball, to provide click feedback
The photo on the right shows how the switching logic is implemented. The white part is a plastic disk, with side dimples where the little ball rests to provide tactile feedback. On the disk, a little ring with four tabs provides an electric transfer path. Now, the blue part is the key to the mechanism. Studying the part (and testing with a multimeter), I found out that the central tab is linked to the bigger plate on the right. Each of the other two pins is linked to a perforated grill forming the matrix on the left. Each perforated grill contains five holes, or five bars (is it half full or half empty?).
Reading the datasheet for this switch I found that this is a 20-click per revolution binary gray code encoder. There are, effectively, 20 dimples on the white plastic ring. Now, looking at the metal parts, we can guess the logic driving the pin’s signals. I’ve made a drawing:
The blue patches in the orange disk represent the voids (holes) in the disk embedded into the blue plastic disk. The back circle with tabs is the metallic ring embedded into the white plastic disk. The red color represents the electrical connections to the three pins of the encoder, pin1, pin2 and center.
One or two black tabs are always connected to the center pin. Its the other two tabs that are providing the logic. The voids and bars of the two matrix are angled in such a way that four distinct logic states can be associated with the three pins:
- Center connected to pin1
- Center connected to pin2
- Center connected to pin1 and pin2
- Center connected to no pin
These four states will be repeated for each click in the wheel. In this case, 20 times per revolution. The beauty of this simple mechanism happens when another variable is observed: Timing.
Because of the angular relation between the two tabs contacting the bars on the left, the signal path actually is:
- Center pin connects to no pin. Ball rests in click dimple
- Center pin connects to pin1
- While 2 is still happening, Center pin connects to pin2
- While 3 is still happening, Center pin disconnects from pin1
- Center pin disconnects from pin2 and ball goes into click dimple: click!
The logic table associated with this is:
Interestingly, if you turn the dial the other way, the signal path is:
- Center pin connects to no pin. Ball rests in click dimple
- Center pin connects to pin 2
- While 2 is still happening, Center pin connects to pin 1
- While 3 is still happening, Center pin disconnects from pin 2
- Center pin disconnects from pin1 and ball goes into click dimple: click!
The logic table associated with this becomes:
With this simple logic, I will be trying to do only one thing: Add or subtract 1 each time a click happens, depending on direction of rotation, clockwise or counterclockwise (cw or ccw).
The simplest way to detect a click and increment or decrement the counter is:
if pin1 goes from 0 to 1 AND pin2 = 1
increment counter (shaft turning cw)
else
decrement counter (shaft turning ccw).
That’s it.
Now, to implement this is a nightmare! If you Google “rotary encoder”, you will be inundated by dozens of different ways of interpreting these simple logic tables and trying to implement them. Why?
These switches are cheap.
Let’s look at the one I’m using. I paid about one dollar for it, shipping included from China. I expected it to be flimsy, but actually, it is quite solid. Mostly metal parts, with plastic to provide the necessary insulation. But if you look at the photos at the top, you can spot a few problems. The tabs on the spinning disk are made of (very) thin springy copper. They cause the signal to flow from the center pin to pin1 and pin2. They make momentary contact with the bars on the base while being dragged by the rotary shaft. Dragging metal on metal to make a switch is about the worst way to implement a “switch”. Bouncing, and its effect on electric signal, can’t be made worse. Actually, it is close to being as bad as old blade switches, which, when old and rusted, tended to bounce continually, forever…
Also, the contacts are covered with “goo”. Probably some kind of slippery grease that prolongs the contacts life (they are rated for MTBF=100,000 clicks). Looks like petroleum gelly to me!
I’m a bit surprised that people try to read these switches “when they stop bouncing”. I’ve seen very elaborate hardware and software solutions for this. As far as I’m concerned, the only time I know they’re not bouncing is when they are in the “click” state, i.e. with no signal going to pins 1&2!
This guy really understands the implications of bouncy switches and even gives a good recipe to take care of it.
This datasheet for the ELM401 IC (unfortunately for a piece I can’t buy (and is too expensive!)) also gives a good explanation of switch bounce and its effects.
My goal is to be able to use rotary encoders in my pedal boards. This is the subject of the next post.
Leave a Reply