# Adventures in Science: Rotary Encoders

It's Monday, and that means it's time for Adventures in Science! This week, we dive into the mysterious world of rotary encoders.

There are two types of rotary encoders: incremental and absolute. Incremental encoders are useful for determining how many times a shaft has rotated or even counting fractions of a rotation. Absolute encoders, on the other hand, can determine the exact position of a shaft (depending on the resolution and precision of the encoder disc). For the video, I focus mainly on incremental encoders, how they can be used in wheeled robots to measure distance traveled, and how quadrature encoders work to determine the direction a shaft is spinning.

Incremental encoders, in their basic form, detect light, magnetic or physical changes on a disc that rotates with the shaft. We show an optical encoder in the video as a demo (the build for which can be found on Hackster). We then add two photocells to the encoder demo to show how a quadrature encoder works. We can use a simple magnetic encoder to count the number of rotations on a shaft, which we can use to determine how far a robot has traveled by placing one on each motor. Code for making our robot (lovingly named "Fred") move in a straight line for one meter can be found here.

Many of the top competitors in AVC use a combination of rotary encoders and a magnetometer (compass) to create a dead reckoning system that allows their robot to navigate around the course. Additionally, many self-balancing robots rely on encoders along with an inertial measurement unit (IMU) to determine if the robot is leaning one way or the other.

What are some other useful, fun or unusual uses for rotary encoders that you've seen?

• Shawn, here is an example of "worst dimmer ever." I'll take your dimmer over that any day!

• The N64 controller's analog stick used rotary encoders instead of potentiometers... The PlayStation 1 "dualshock" controllers used potentiometers... I'm not so sure about the Sega Saturn's "3d controller" though...

• Interesting....I didn't know that. I would have thought they all used potentiometers!

• Hi Shawn!

Great video! I just wish you'd posted it last Monday! FWIW, Tuesday of last week I was at an Amateur (Ham) Radio club meeting, and another member was bemoaning the fact that the "tuning dial" on his (\$1000+) radio had quit working -- turns out it's coupled to an optical encoder, which is used to control the frequency of the radio. He was totally unfamiliar with rotary encoders, and it would have been convenient for me to point him at your video.

• Forgot to mention another place rotary encoders are/were found: those of us who remember "mechanical" mice, they had one quadrature detector to measure X movement, and another to measure Y movement. (There would be two axles that would be rotated by the ball, with a detector on each.) In my experience, these were usually "notch" type optical detectors, and from time to time I'd have to take them apart and clean lint & dirt from the detectors.

Also, most modern automobiles have a variety of rotation encoders: they're used for anti-skid brakes, so-called "stability control", even engine ignition timing.

• Thanks! Sad that we missed it by a week!

I do remember those old mice....I was always having to clean out the ball cavity. I didn't have an old one to tear apart, but that would make sense that they used encoders, as I remember when "optical" mice were the new thing.

• Yeah, I remember the original "optical" mice. I really hated these things -- to the point when the mechanical mouse on my Sparkstation died (bad switch under one of the buttons) and I had to switch to using an optical one, the IT director got Sun to "next-morning" a new mechanical one to me. The big thing I hated about them was that they had to be used on a special mouse pad -- the mechanical ones could be used on just about any relatively flat surface.

In 2003, CU student Nate Seidle fried a power supply in his dorm room and, in lieu of a way to order easy replacements, decided to start his own company. Since then, SparkFun has been committed to sustainably helping our world achieve electronics literacy from our headquarters in Boulder, Colorado.

No matter your vision, SparkFun's products and resources are designed to make the world of electronics more accessible. In addition to over 2,000 open source components and widgets, SparkFun offers curriculum, training and online tutorials designed to help demystify the wonderful world of embedded electronics. We're here to help you start something.