Rotary encoder

A rotary encoder is an electronic component, that can replace a potmeter or other kinds of knob-like functionality. Here is a good Youtube video explaining how a rotary encoder works. Some rotary encoders also have a push button functionality when pushing the shaft.

Example of a rotary encoder with push button switch.

Detecting whether the rotary encoder is rotated left or right can be somewhat tricky, especially with fast rotations. However, the code from Buxtronix is bulletproof provided that the MCU still has breathing space.

Another thing to look out for is that some rotary encoders are half step and others are full step. If you don’t know which step type of rotary encoder you have at hand, you will have to try the different S/W solutions and look out for missing/double steps.

Finally, the detection can be either interrupt driven or by scanning for changes. Which one to use depends on the actual application.

Detecting the push button is not special to the rotary encoder, but is like any other push button, when it comes to detection and debouncing. Please see the push button tutorial.

Half step and interrupt

Full step and scanning