How to use a rotary encoder in a microcontroller project

Added March 21, 2022 at 1:10 pm

Save or share

Learn how to use an incremental rotary encoder in an Arduino project.

A rotary encoder is an electromechanical device that converts rotary motion into digital or analog information. It is very similar to a potentiometer, but can be rotated indefinitely, either clockwise or counterclockwise. There are several types of rotary encoders. The two main types are absolute and relative (incremental) encoders. While an absolute encoder outputs a value proportional to the current shaft angle, an incremental encoder outputs the shaft's pitch and direction. Rotary encoders are becoming more and more popular in consumer electronics, especially as control knobs, in addition to applications in many other fields. They replace potentiometers and navigation buttons where fast navigation, setup, data entry and menu selection are required. Some encoders also include a built-in button that creates an additional input to the processor that can be used as another user command in the control interface. In the picture below you can see a typical incremental rotary encoder with a power button.


Incremental rotary encoder

In this article, we will show you how to use an incremental rotary encoder in an Arduino project. We will explain how to deal with contact bounce and interpret encoder signals in a microcontroller program using interrupts.

Incremental encoder quadrature output signal

An incremental rotary encoder produces two output signals as the shaft rotates, also called quadrature output. Depending on the direction, one signal is ahead of the other. Below you can see the output waveform of an incremental rotary encoder and the expected bit sequence.


Signals at the outputs of an incremental rotary encoder when the shaft rotates clockwise and counterclockwise

As can be seen from the figure, both outputs are initially in the logical one state. When the encoder shaft begins to rotate in a clockwise direction, the state at output A drops to logic zero first, and then output B follows with a lag. When rotating counterclockwise, the opposite happens. The time intervals on the signal diagram depend on the rotation speed, but the signal lag is guaranteed in any case. Based on this characteristic of an incremental rotary encoder, we will write a program for Arduino.

Buy on Aliexpress

Rating
( 2 ratings, average 5 out of 5 )
Did you like the article? Share with friends:
For any suggestions regarding the site: [email protected]
Для любых предложений по сайту: [email protected]