Rotary Encoders – The Complete Tutorial – Part 1

I have to admit that the rotary encoder has to be one of the most useful and, when implemented right, intuitive devices for manual control of electronics. It has become such a common control device for car radios that using a different device for manual volume and on/off control would confuse the hell out of somebody using it for the first time.

 

Uses for Rotary Encoders

 

The combination of rotary encoder with integrated push button seems to be the norm for an easy and intuitive user interfaces for on/off control via the push button and volume control for the rotary knob. Another function might be push button for selecting a menu item and the rotary knob for navigating the menu. It could even replace several simple rotary encoders where the push button cycles between functions that the rotary knob can control. I could go on….but I won’t.

As much as I like these input devices, I have never actually used or integrated one into any of my designs before. This bugged me. I finally got the urge to grab one and use it. For something. Anything!

I just don’t have a project going right now that calls for one, so I’ll do the next best thing (which is debatable),  I will dive deep into the inner workings of one and create a tutorial on how to use one, write firmware that responds to one, but more importantly how they work.

Don’t freak out and run away just yet. This could be a lengthy tutorial, but for your convenience and my sanity I will be making this a multi-part tutorial series. Lets get started!

 

 

Rotary Encoder

The almighty Rotary Encoder. Purchased from ubiquitous Adafruit electronics online store.

 

The Pinout

 

If you are following along, the above part is the PEC11 Pro Audio Series rotary encoder. The first thing to notice are the 5 pins (technically 7 pins). The two larger ones are meant to be soldered to a board for structural stability to take the strain off the IO pins. As pictured above, there are 3 pins on the right which are:

  • Encoder output A
  • Ground
  • Encoder output B

from left to right respectively. The 2 pins on the left of the rotary encoder are:

  • Momentary switch A
  • Momentary switch B

The momentary switch is a normally open switch whose contacts are closed when the rotary encoder button is pushed for a momentary contact. I think the switch function speaks for itself, no magic there. It’s simply like your standard momentary or tact style switch. Lets look into the encoder outputs A and B.

 

The Internals – Gray Code

 

The internal mechanical arrangement of the encoder to the encoder outputs A & B are that of two switches. These switches work in tandem to form an output that follows a binary gray code system. The reason for this is that gray code only allows for one switch state to change at once. If a group of switches followed a standard binary system a state such as ‘0111’ transitioning to ‘1000’ involves all four switches changing their state at once. This is a problem with switches as, in electronics time, switches are very slow and don’t change state instantaneously or all at the same time. This can cause a high chance for false states if the outputs are sampled during a transition.

Lets look at the 2 bit gray code states:

rotencoder

 

Detecting States

 

Ok, so we don’t need to worry about false states using the gray code system. Now we need to determine which way we are rotating the encoder. It is important to know if, in the case of a volume knob, the software should increase or decrease the volume. That all depends on the direction the knob is being rotated, so somehow the software has to detect this. This is accomplished by comparing previous states and current states.

Say an encoder has an initial state on output A & B of ’01’, and by continually sampling then we find that the next value on the output becomes ’11’. In this case it becomes obvious (following the above diagram) that the encoder has been rotated clockwise. If we had gone to ’00’ then we know it is counter clockwise and can react accordingly in software.

 

 

Ok, that should be enough to digest for now. Next time we will look into some features of a rotary encoder such as resolution and detents, as well as how to handle the encoder in software. It will also be very interesting to look at the actual outputs of an encoder on an oscilloscope.

Read part 2 here:

Rotary Encoders – The Complete Tutorial – Part 2

 

TronicsLab!

Comments

Leave a Reply

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