Los rotary encoders are electronic components that have gained popularity in DIY projects and in the Arduino ecosystem. These devices allow the reading of the motion of an axis, which translates into the possibility of tracking speed and angular position of different systems. However, as useful as they can be, they are often confused with other devices such as potentiometers, which can lead to misunderstandings about its use and functionality.
In this article we will explain in detail what are the rotary encoders, how they work, and how you can use one with Arduino. We'll also go over connections, code usage, and some practical examples that will help you integrate it into your projects in an effective way. But before we get into the technical details, it's important to understand what makes this device a favorite in electronics projects.
What is a rotary encoder?

Un rotary encoder It is an electromechanical device that converts the rotational motion of an axis into a series of signals, usually digital. These signals can be used by a system such as Arduino to interpret information related to the spin speed, the posición of the axis and even the direction of rotationUnlike potentiometers, which have a limit on the number of turns, encoders can turn indefinitely.
There are different types of rotary encoders, but they can be divided into two broad categories: absolute e incrementalAbsolute encoders provide an accurate reading of the shaft position at all times, no matter how many revolutions it has made. Incremental encoders, on the other hand, only record the relative motion from an initial position, without having an absolute reference of the angle.
The most common type of encoder in DIY or Arduino projects is the incremental encoderThey are inexpensive, easy to find, and can be used for a wide range of applications such as volume control, LCD brightness adjustment, and more. Additionally, many of them include a push button, which helps integrate additional control actions into your projects.
How a rotary encoder works
The heart of a rotary encoder is made up of a series of interruptions which are activated every time the shaft rotates. This generates a digital signal that can be read by an electronic device. This type of encoder usually has two main outputs, which generate signals in quadrature phase. This means that there is a 90-degree phase shift between both signals, which allows not only counting the turns, but also determining the direction of rotation (clockwise or counterclockwise).
The device provides a number of pulses per turn, and depending on the number of pulses, we can more accurately determine the movement of the shaft. A standard encoder can have from 20 to more than 256 pulses per revolution, depending on the model. pulses are counted using a quadrature system, which also allows determining whether the rotation has been clockwise (CW) or counterclockwise (CCW), making it very practical for systems of control and navigation.
In addition, these devices may include a push button that is activated by pressing the shaft. This push button allows an action to be recorded as if it were a button, which expands the possibilities of use. In short, the encoder not only allows the rotation of the shaft to be recorded, but also its steer axle truck y real time position.
Differences between a rotary encoder and a potentiometer
It is very common to confuse a rotary encoder with a potentiometer due to their similar appearance, but there are fundamental differences that separate them. A potentiometer adjusts a resistance value according to the angle of rotation, providing an analog output. In contrast, a rotary encoder generates a series of digital pulses that represent the rotation of the shaft. Another key difference is that rotary encoders can rotate indefinitely, while potentiometers have a physical limit on the number of turns.
For these reasons, encoders are ideal for situations where continuous and precise control of rotation is required, such as in motor control systems, while potentiometers are commonly used to adjust static parameters such as volume or brightness.
Connecting an encoder to Arduino
Connecting a rotary encoder to a Arduino It's relatively simple. Depending on the model, the encoder will have three main outputs: two for channels A and B, and one for the push button. The push button connects to an Arduino digital input, while channels A and B connect to two other digital inputs.
To get an accurate reading of the encoder movement, it is recommended to use pins. interruption on the Arduino for channels A and B. However, this is not always necessary. If you only need single or double precision, you can get readings by polling these outputs periodically, although this will affect the efficiency of the code.
The connection for a typical project would be something like this:
- Channel A connected to D9
- Channel B connected to D10
- Push button connected to D11
It is important to make sure that the pins selected for interrupts are the correct ones, as not all pins on Arduino boards support interrupts. In short, you can connect an encoder without having interrupts, but the accuracy will be lower.
Code to use a rotary encoder with Arduino

The code to read an encoder with Arduino is simple. Below we show a basic example to read the number of pulses generated in channel A, also determining the direction of rotation with the help of channel B. This is a simple approximation using the method Polling, but if you need more precision, you can modify the code to work with interruptions.
const int channelPinA = 9;
const int channelPinB = 10;
int prevStateChannelA = 0;
int value = 0;
unsigned long currentTime = 0;
unsigned long prevTime = 0;
void setup() {
Serial.begin(9600);
pinMode(channelPinA, INPUT);
pinMode(channelPinB, INPUT);
prevStateChannelA = digitalRead(channelPinA);
}
void loop() {
currentTime = millis();
int currentStateChannelA = digitalRead(channelPinA);
if (currentStateChannelA != prevStateChannelA) {
if (digitalRead(channelPinB) != currentStateChannelA) {
value++;
} else {
value--;
}
Serial.println(value);
prevStateChannelA = currentStateChannelA;
currentTime = millis();
}
}
This code detects the change of state on channel A and evaluates channel B to determine the direction in which the encoder has been turned. Each time the value changes, the code will update the number of pulses recorded and print the value to the serial monitor.
Examples of common use
Los rotary encoders They are used in a wide range of professional and home applications. In the field of Arduino projects, they can be used to adjust parameters in real time, such as controlling the direction of a mobile robot, regulating the volume of a speaker or adjusting the brightness of an LCD screen. They are also widely used in control systems for stepper motors or servos, where precise control of the axis position is required.
One of the most interesting applications is the creation of interfaces with visual or auditory feedback, where the encoder not only adjusts a parameter, but also provides the user with physical feedback by turning it, something that cannot be achieved with traditional buttons.
Furthermore, encoders are extremely useful as a replacement for potentiometers in systems that require a higher degree of durability, since they have no rotation limits, which reduces mechanical wear.
El use of encoders in the programming of robots and autonomous systems is also growing. They can be connected directly to the wheels of a robot to measure precise movement, thus optimizing displacement and direction calculations, which improves the efficiency and navigation capability of robots.
Rotary encoders have also found their way into the development of medical devices and industrial control panels, where constant motion control is required to achieve millimeter precision in machinery.
Overall, encoders are a versatile and economical option for projects where accurate measurement of rotation is key.
If you are looking for an economical and efficient solution for your Arduino projects, the use of rotary encoders can provide you with an excellent alternative to other analog control methods, such as potentiometers. Their ability to provide precise control over a wide turning range makes them ideal for a wide variety of applications.