Side Project: understanding cheap Rotary encoders

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:

  1. Center connected to pin1
  2. Center connected to pin2
  3. Center connected to pin1 and pin2
  4. 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:

  1. Center pin connects to no pin. Ball rests in click dimple
  2. Center pin connects to pin1
  3. While 2 is still happening, Center pin connects to pin2
  4. While 3 is still happening, Center pin disconnects from pin1
  5. 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:

  1. Center pin connects to no pin. Ball rests in click dimple
  2. Center pin connects to pin 2
  3. While 2 is still happening, Center pin connects to pin 1
  4. While 3 is still happening, Center pin disconnects from pin 2
  5. 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.

This entry was posted in Arduino, Pedal board and tagged , . Bookmark the permalink.

11 Responses to Side Project: understanding cheap Rotary encoders

  1. Pingback: Linkdump: Arduino and JeeLabs « EdVoncken.NET

  2. Evan says:

    Thanks for the guide, very helpful! What model is this encoder and where could I buy one?

    • rt says:

      They are the “cheap” model. Very standard. Easy to find on EBay for less than 2$, and should be available at your local electronics store.
      Here’s a similar one from digikey.com: P12336-ND. Less than 2$ in quantity.

  3. Muhammad says:

    Thanks for your explanation. I was searching on the encoder used in a computer mouse. It seems that they use similar electrical connections to generate a 2-bit gray code.

  4. theo says:

    Great info buddy, would you know how i can make the clicks stiffer when i turn my 5 pin encoder? like a native instruments X1?
    My encoders from china feel a bit looser.

    Thanks bro.

    • rt says:

      In some encoders, the little ball that provides the click and tactile feedback is pushed in place with a small metal leaf. If you completely dissemble the encoder, you could bend it a bit more. Risky though, as that metal is a bit fragile.

      • theo says:

        Hi RT, thanks for getting back to me, my encoder looks the same as the one in your photo. Are you talking about the 4 metal prongs that come off the round white disc?

        • rt says:

          Look at the picture on the left at the top of this post: on the right side of that picture, you can see the underside of the top part of the encoder (with the shaft). There is a leaf spring there (just a metal sheet, about 2 mm wide by 15mm long). It can be bent a little by prying with a small screwdriver.

  5. Kees says:

    I had the same problem with the encoders you show on the foto. Cheap worthless shit from ebay. The contact is dragged over a brushed metal , not a flat one. Its full of grooves. Even with cappacitors it still has bouncing.
    I also tested another brand of encoder , withoud capacitors. This one has nearly none bouncing.
    I bought these at Ledsee. Cheap and good. Brand is Alps.

  6. Alex says:

    I was really curious on how these cheap chinese encoders work! Thanks for the time writing this up!

    • rt says:

      You’re welcome! I still use them regularly in projects. They are very reliable when you know their limitations.

Leave a Reply

Your email address will not be published. Required fields are marked *