# Rotary Encoders!

Do you get a lot of use out of the scroll wheel on your mouse? How about that Griffin PowerMate you got for your birthday? If so, chances are that rotary encoders are responsible for some of the joy in your life.

A 15-period code wheel for a rotary encoder. Believe it or not, this hypnotic image is an important part of an analog-digital converter! Generated using the script at the end of this post.

Being the nerd that I am, rotary encoders fascinate me. What follows might be a little dry, but I’ll try to keep it interesting. I’m even considering a more intuitive (and probably more entertaining) YouTube-based explanation, but I make no promises. Anyway, here goes.

A rotary encoder does one job and does it well: It turns rotation into information. Any rotary encoder can be used to tell you how far something has turned (like an odometer, e.g. in total revolutions) or how fast something is turning (like a tachometer or single-gear speedometer, e.g. in revolutions per minute). With a little planning, they can also tell you which direction. (Surprisingly enough, this isn’t always important, and even when it is the information is often available elsewhere.)

Predating the rotary encoder, a rotary potentiometer is one analog device that can be used to turn an angle into information. But a potentiometer is inherently limited; it has low and high extremes. Most potentiometers have a brake at each end, but even if those were removed, the transition from one end through the limit to the other would be problematic.

Here’s an example that I hope is intuitive enough: A simple playing card and your ears (provided you’re not deaf or altogether tone-deaf) can be combined to make a tachometer for a bicycle!

First, we fix a playing card to click against the spokes of a bicycle wheel. There is a fixed number of spokes, n, in the wheel, and so every full turn of the wheel the card can be heard flipping n times. If the wheel turns once per second (60RPM) then the card will click n times per second, or n Hz. If it starts moving twice (120RPM) or three times (180RPM) that fast, the clicks will increase to 2n Hz or 3n Hz, respectively.

Above around 20Hz, the human ear begins to notice the individual clicks less and instead hear them as an audible wave. (For example, if the clicks are produced at 440Hz, your ear hears the A above middle C.) Lower speeds mean lower pitches and higher speeds higher pitches. This sensation happens on a roughly logarithmic scale, so if the speed doubles, the pitch rises one octave, and if the speed halves, it lowers one octave.

If you have good pitch (i.e. you can figure out roughly the frequency of what you’re hearing) and know the number of strikable spokes on the wheel (i.e. the ratio of clicks to revolutions), you can figure out how fast the bike is going without even looking!

This mechanism demonstrates a rotary encoding: The initial information from the source (the rotation speed) is encoded in a tangible form (a discrete click emitted every 1/n rotations) that is more intuitive or useful to the target (the human ear senses the clicks and perceives a pitch).

Actually, this encoding isn’t too different from what a computer (or digital electronic system in general) might need to gather the same information. If we were to fit (or replace) the playing card with some sort of sensor that briefly emits a pulse every time a spoke passes, this information could be passed to a counter circuit that would then be able to tell you how fast the bicycle is going (a tachometer/speedometer, totaling only the pulses from the past second or other amount of time) or how far it has gone (an odometer, totaling all of the pulses after a reference point).

You might have noted something else: This system behaves the same in reverse as forward. If your bike is (somehow) moving backward, there is no negative click registered; for our purposes the click sounds the same as before. (If it doesn’t, that’s one way you can retrieve the direction information!) If you move the bike forward for a quarter of a mile, then move it backward for a quarter of a mile, the odometer has measured half of a mile rather than zero miles. For many applications, that’s as it should be (e.g. to prevent odometer tampering on a used car) and for many others it doesn’t matter too much.

One application where it does matter is the computer mouse. The old mechanical/ball variant typically used two rotary encoders, one for each axis. An optical mouse replaces this with an LED and a low-res imaging mechanism, but last time I opened one up there’s still an encoder—on the scroll wheel.

A very large scale model of a Logitech scroll wheel. Note the spokes!

The scroll wheel on a modern mouse, once you open it up, actually has similar spokes to the ones we’ve already discussed! Instead of a flicking card, however, it uses optics: There’s an infrared LED on one side of the wheel and a phototransistor on the other. The spokes alternately block and pass light between the two, allowing the hardware to count the distance. However, at least on the one I cracked open, the encoder doesn’t distinguish directions on its own. Instead, there’s a very light-touch switch that moves to whichever direction the wheel was most recently spun. This provides the extra bit of information to tell the system which way it’s going.

That’s not the end of the story, though. It’s possible for the encoder itself to supply direction information on its own—it’s even done that way on some mice! I’m hoping to come back to that in a future post. If I don’t, or if you’re just itching to know, the mechanism of interest is the “quadrature encoder” and it involves using two sensors instead of one but requires some extra considerations when making the code wheel (i.e. the spoke pattern).

Until next time!

The code wheel in the image at the beginning was generated using this Perl script.