Rascally Rotary EncodersRascally Rotary Encoders
While extremely useful, rotary encoders can be a tad tricky.
November 22, 2022
I feel like an old fool (but where are we going to find one at this time of the day?). The reason for my discomfiture is that I only recently became aware of the proliferation of rotary encoders.
In the days of yore when I wore a younger man’s clothes, electronic equipment like radios and television sets flaunted a mixture of toggle switches, pushbutton switches, and knobs. The knobs themselves were typically attached to two types of devices: rotary switches for digital tasks and potentiometers for analog functions like volume control.
One aspect with respect to most of the potentiometers with which I came into contact is that they rotated only through a fixed amount, like 270°, for example. Another feature is that their operation was smooth and silent.
Sometime during the past few years, I vaguely noticed that a different type of rotary control was making an appearance. These new devices have no end-stops, which means you can turn them indefinitely if you wish, although doing so is pointless once you’ve reached the maximum or minimum value of whatever it is you are controlling. In the case of encoders intended for use by humans, the little scamps also provide haptic feedback in the form of a tactile (but inaudible) “click-click-click” sensation.
The strange thing is that I can remember noticing the presence of these devices when I ran across them without ever taking a mental step back to contemplate just how it was that they were performing their magic. This situation could have persisted indefinitely, but someone recently asked me a rotary encoder related question, the answering of which obliged me to actually start paying attention to what was going on around me.
When I did commence to cogitate and ruminate on this topic, I realized that these little rotary encoding rascals are everywhere. In my own case, they feature prominently on my toaster oven, they are on my home and car stereo systems, they control my car’s cooling and heating systems, and... the list goes on.
A little research reveals that these devices come in myriad manifestations. The two main types are absolute encoders, which report the current position of the shaft, and incremental encoders, which report any changes in the position of the shaft. The positional detection of both types can be implemented using a variety of technologies, including mechanical, optical, capacitive, and magnetic.
The most common type of these devices that most of us will run across during a typical day will be mechanical incremental quadrature encoders. These have a common (COM) pin, which we connect to ground, and two outputs called A and B, to which we attach pull-up resistors. Hang on; where did that “quadrature” qualifier come from in the last sentence but one? Well, this is the encoding scheme that is typically used for rotary incremental encoders. In the case of mechanical encoders, we might start by visualizing the underlying mechanism as being in the form of two rings populated by alternating conductive and non-conductive patterns. We also have two “brushes” attached to our A and B pins that we can use to detect the patterns on these rings.
Quadrature encoding scheme.
Observe that this is a gray code (only a single bit changes state at a time). A sequence of 00, 10, 11, 01, etc. reflects a clockwise rotation, while a sequence of 00, 01, 11, 10, etc. indicates an anticlockwise rotation.
I started to wonder how this mechanism was actually implemented in the real world, so I bounced over to Amazon to purchase several different types to play with, such as the EC11 flavor, which boasts 20 “clicks” for each 360° rotation (I found a 10-pack of these for only $10.99).
I also contacted my friend Joe Farr in the UK to chat with him about these devices. Joe said that he had used them before but—now I’d raised the issue—he also was interested to learn more, so he started to rummage through his treasure chest of bits and pieces.
I have to say that both of us were surprised by what Joe discovered (and what I confirmed once my own devices had arrived and I got to dissect a few samples). Take a look at the image below and contemplate it for a moment.
Three different quadrature rotary encoders.
The device at the top shows a wheel with 15 conductive “spokes.” On the left, the red lines highlight the common terminal in the center and the A and B terminals (along with their “brushes”) on the sides. This device boasts 30 “clicks” for each 360° rotation.
The image in the middle shows a wheel with 20 conductive areas. Once again, the red lines on the left highlight the common terminal in the center and the A and B terminals (and their “brushes”) on the sides. This device boasts 20 “clicks” for each 360° rotation.
Now look at the image on the bottom. This device also boasts 20 “clicks” for each 360° rotation. In this case, however, the metallization—which is located on the base on the left in this instance—features a single large area connected to the COM pin, along with two “10-spoke fans” that are connected to the A and B pins. The rotating part in the middle, which will be attached to the shaft, has four contacts connected in the center and mounted at 90° to each other. This arrangement means that one of these contacts is always touching the large metallization area, while two of the remaining contacts drive the A and B pins.
But wait, there’s more. I hadn’t really thought about this until Joe and I put these devices on a testbench and monitored them with an oscilloscope. It turns out that both of the 20-pin devices start off with their outputs in a 11 state. Rotating the device one “click” clockwise will cause the outputs to pass through the 01, 00, and 10 states before returning to 11. Alternatively, an anticlockwise “click” will cause the outputs to pass through the 10, 00, and 01 states before returning to 11.
So far, so good. Where things get tricky is with the 30 “click” device. In this case, assuming we start in a 11 state on the A and B outputs, one “click” clockwise will take us through the 01 state and leave us in the 00 state. A second “click” clockwise will take us through the 10 state and return us to the 11 state (I’ll leave it to you to work out what happens for each click anticlockwise).
By this point you might be thinking to yourself, “Well, things are a bit more complicated than we might have initially supposed, but—so long as we know whether our encoded passes through 2 or 4 states per click—it will be relatively easy to write a bit of software that monitors the progression of states and uses this information to determine the direction of rotation and the number of clicks.” To which I would respond... “Ah, I was young and innocent like you once deep in the mists of time, but have you considered the problem of noise in the form of switch bounce?”
Oh dear. Mechanical rotary encoders suffer from switch bounce.
Observe the noise on the A (yellow) signal near the center of the screen. Also observe that this noise spans the 0-to-1 transition on the B (blue) signal below. This is going to give your code a headache, and the amount of bounce only gets worse the faster you turn the knob.
Another case to consider is if you happen to turn the knob just past a “click” point, thereby causing the A and/or B signals to indicate the start of a new “click” cycle, but you then return the knob to its previous “click” position.
Now that I’ve started to pay attention to things, I’ve come to realize that many designers should have thought about this sort of thing themselves. In my car, which is a 2019 Subaru Crosstrek, for example, when I rotate the volume control on the radio, the display on the screen changes to show a segmented circle reflecting the relative volume. Much of the time, a single “click” clockwise will result in a single segment being added to the display accompanied by a corresponding small increase in the volume. More times than I would expect, however, a single “click” clockwise results in a 2 or 3 segment increase in volume or—occasionally—a 1 or 2 segment decrease in volume. Contrariwise for a single “click” anticlockwise.
“Eek! Is there a solution for this?” I hear you cry. Of course there is, but I’m afraid we will have to leave that for a future column. Until that frabjous day, as always, I look forward to receiving your comments and questions.
About the Author(s)
You May Also Like
Partnership Yields the Ducati Diavel for BentleyDec 08, 2023
Mercedes-AMG GT Aerodynamics: Form Follows PerformanceDec 07, 2023
Developing Raspberry Pi 5 Apps Using EclipseDec 08, 2023
Magna Supercharges ADAS with 5GDec 07, 2023