Atlas Robotics
Motor ControlBasic

What is an encoder? Measuring motor speed and position

Atlasbot · 16/8/2026 · 11 min read

What is an encoder? Measuring motor speed and position
Incremental encoder and A/B phase square signal

Setting the same PWM value for the motor when the robot runs on a flat floor and when climbing a hill, the actual rotation speed will be different — PWM only controls the average voltage applied to the motor, does not guarantee the actual rotation speed. To know exactly how many revolutions per second the motor is rotating, or how many degrees the shaft has rotated, you need a separate sensor: encoder.

Key concepts

The encoder is attached directly to the motor shaft (or wheel shaft), emitting a pulse signal every time the shaft rotates a small fixed angle. Counting the number of pulses in a period of time indicates rotation speed; Counting the total number of pulses shows how much has rotated from the original position.

There are two main types:

  • Incremental encoder — only knows how many times has been rotated since power on, power failure means loss of reference position, more popular and cheaper
  • Absolute encoder — always knows the exact current rotation angle even after a power outage, more complicated and expensive

Two A/B channels — know both speed and direction of rotation

Most relative encoders used in robots have 2 signal channels (A and B), set 90° out of phase with each other. Only counting pulses on 1 channel indicates speed but not direction of rotation; Comparing order which channel goes high first (A before B, or B before A) will indicate whether the axis is rotating forward or backward — a technique called quadrature decoding.

In short: The encoder is the "eyes" of the control loop motor — without it, the MCU only knows how much it * ordered* to rotate, but not how much the motor * is actually* rotating.

How it works

Channel A: ┐ ┌──┐ ┌──┐ ┌──┐ (forward rotation: A goes before B)
        └──┘  └──┘  └──┘  └──
Channel B: ┐ ┌──┐ ┌──┐ ┌──┐
          └──┘  └──┘  └──┘  └──

Many MCU lines (like STM32) have Timer Encoder Mode — the Timer hardware automatically counts and quadrature decodes without consuming the CPU to process each interrupt pulse:

// Turn on the Timer in encoder mode — the hardware automatically counts and recognizes the direction of rotation
HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);

int32_t count = __HAL_TIM_GET_COUNTER(&htim2);
// count increases when rotating forward, decreases when rotating backward — no need for interrupt handling code

The number of pulses per revolution is called resolution (PPR/CPR — Pulse/Count Per Revolution) — the higher the resolution the encoder has, the more accurately it can measure the rotation angle.

Technical details

Formulas

Since most quadrature decoders count all 4 edges (up/down of both channels A and B) instead of just 1 edge, the actual count per revolution is 4 times the original PPR — this is called x4 decoding:

Actual count per round = PPR × 4
Angular resolution (degrees/count) = 360° / (PPR × 4)
RPM = (Δcount / Δtime(s)) / (PPR × 4) × 60

For example: encoder 13 PPR (before the reducer), decode x4
Count/round = 13 × 4 = 52
In 0.1s, 26 counts are counted
RPM = (26 / 0.1) / 52 × 60 = 300 rpm

Parameters

PPR resolution (before multiplying by x4) varies greatly depending on encoder type and mounting location (before or after the reducer):

Encoder typeTypical PPR
Cheap magnetic encoder on gear reduction motor (mounted on motor shaft, before gearbox)11 – 13
Universal optical encoder100 – 600
High resolution encoder (used for industrial manipulators)1000 – 4096+

For motors with gear reduction gears (e.g. ratio 1:50), the actual resolution measured at the wheel shaft (behind the gearbox) is many times higher than the original PPR at the motor shaft — see the calculation example in the Common Errors section.

Register / Bit / Byte

Timer Encoder Mode on STM32 is enabled via register TIMx_SMCR (Slave Mode Control Register) — choose between 3 decoding modes: edge counting only of TI1 (x1), only TI2 (x1), or both signals (x2 or x4 depending on detailed configuration). The current count value is in register TIMx_CNT — this is the register that function __HAL_TIM_GET_COUNTER() reads, automatically incremented/decremented entirely by the Timer hardware without needing to interrupt the software.

Practical applications

The encoder data of both the left and right wheels is the most basic input to calculate odometry — estimating the distance the robot has traveled by converting the encoder count into actual distance: distance = (count / counts per revolution) × wheel circumference. This is the first source of location data that almost every AMR has available, used as input for the SLAM/localization algorithm before incorporating IMU or LiDAR to increase accuracy. In addition, encoder data is also a required feedback signal for the PID control loop to keep the motor speed stable - the topic of the PID article in this category.

Practical example

#include <stdio.h>

int main() {
    int ppr = 13;
    int decode_factor = 4;
    int gear_ratio = 50;   // gear box 1:50
    float wheel_diameter_cm = 6.5;

    int counts_per_wheel_rev = ppr * decode_factor * gear_ratio;
    float wheel_circumference = 3.1416 * wheel_diameter_cm;

    int delta_count = 2600;   // The number of counts read in a period of time
    float distance_cm = ((float)delta_count / counts_per_wheel_rev) * wheel_circumference;

    printf("So count/vong banh xe = %d\n", counts_per_wheel_rev);
    printf("Quang duong di chuyen = %.2f cm\n", distance_cm);
    return 0;
}
Output:
So count/vong banh xe = 2600
Quang duong di chuyen = 20.42 cm

delta_count = 2600 is exactly equal to counts_per_wheel_rev - meaning the wheel has just rotated exactly 1 revolution, and the calculated distance (20.42cm) matches the circumference of the wheel with a diameter of 6.5cm - a way to check for yourself whether the calculation formula is correct or not before trusting it for real odometry.

Common mistakes

  • Confusing the PPR at the motor shaft with the effective PPR at the wheel shaft — as in the example above, the PPR recorded on the encoder datasheet (13) is usually measured before the gear reducer — forgetting to multiply by the gear ratio when calculating odometry will give the distance a difference of dozens of times.
  • Using x1 decoding instead of x4 without knowing it — some libraries default to only counting 1 edge (x1), reducing the effective resolution to 4 times the encoder's actual capabilities — check the correct decoding mode configuration in the library/HAL document in use.
  • Electrical noise causes wrong counting (overcounting or undercounting pulses) — the encoder signal wire is long, without shielding, running close to the high-power motor wire is susceptible to inductive noise causing wrong counting — you should use twisted pair cable with shielding and wire it separately from the motor wire when possible.
  • Expect relative encoder to "remember" position after power failure — as mentioned in the Main Concepts section, incremental encoders always restart from count 0, requiring a separate "homing" procedure if the application needs to know the absolute position immediately after power on.

Conclusion

Encoders convert motor control from "blind" (only knowing what command has been given) to having real feedback (knowing exactly what the motor is doing) — a must-have for both odometry and PID control loops. Always calculate the correct decoding coefficient and transmission ratio when converting encoder counts into actual physical units (rpm, cm). The next article in this section is about Servo Motor - a type of motor that has an integrated position control mechanism, different from the DC motor + discrete encoder approach just learned.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

What is an encoder? Measuring motor speed and position — Atlas Robotics