Motor Encoders and Closed-Loop Control

Motor Encoders and Closed-Loop Control

An encoder provides information about motor shaft rotation. It converts physical rotation into electrical signals that a motor controller can understand. This allows the robot to:

  • Track Distance: Measure exactly how many rotations a wheel has made to calculate travel distance.
  • Control Speed: Maintain a constant speed even when the robot goes uphill or encounters resistance.
  • Precise Positioning: Ensure a robotic arm stops at a specific angle every single time.
A geared DC motor with a Hall-effect quadrature encoder mounted on its rear
A geared DC motor with a Hall-effect quadrature encoder mounted on its rear

Usually, the encoder is mounted directly onto the back of the motor shaft. The shaft spins a patterned disc inside the encoder housing, and fixed sensors read the pattern as it passes, converting rotation into electrical signals for the motor controller.

Types of Encoders

Illustration comparing an incremental encoder with an absolute encoder
Comparing an incremental encoder with an absolute encoder
  • Incremental Encoders. They measure rotation by counting: every time the disc moves one step past a fixed sensor, the encoder sends out a pulse. Inside the sensor there is a disc with alternating transparent and opaque segments, or a ring magnet with alternating poles. In both designs the sensor simply "sees" the pattern pass by and turns it into pulses. By counting the pulses, the controller knows how far the shaft has turned. If it also knows how many pulses one full revolution produces, it can calculate the number of turns the shaft has made. To also detect the direction of rotation, the encoder outputs two channels (A and B) shifted in phase.
  • Absolute (Positional) Encoders. An absolute encoder uses the same physical idea - a disc patterned uniquely for every angle, read by sensors (optical or magnetic), but instead of producing pulses to count, it reports a unique code for each position. The controller reads this code and knows the exact position instantly, with no counting and no reference point. Some magnetic encoder designs work like a tiny compass: a magnet mounted on the shaft creates a magnetic field whose direction changes as it rotates. The sensor chip measures the direction of the magnetic field to report the shaft angle directly. Even if you turn the power off and move the motor by hand, the encoder reports its exact position the moment it is turned back on.
Feature Incremental Encoder Absolute Encoder
Position Information Relative: pulses counted from a reference point Unique value for every shaft angle
After Power Cycle Position is lost until re-referenced Exact position available immediately
Direction Detection Requires two quadrature channels (A/B) Inherent in the position value
Typical Interfaces Quadrature (A/B) PWM, I2C, SPI
Typical Robotics Use Wheel speed and travel distance Joint angles in robot arms and actuators

Popular Encoder Protocols

Below are the most popular protocols used by encoders to report the motor's rotation information:

  • Quadrature (A/B Phase): The standard for incremental encoders. It uses two signals to determine both distance and the direction of rotation. The two signals (usually called A and B) output pulses that are offset in phase by 90°. By observing which channel leads or lags, you can determine the direction in which the motor is turning.
  • PWM: This protocol is used by absolute encoders. The encoder sends a pulse whose width represents the angle of the motor's or gearbox's shaft.
  • I2C / SPI: Digital protocols used by high-precision absolute encoders to send exact numerical data. Most common on absolute encoders, but can be used by incremental encoders as well.

The quadrature encoder is the most popular encoder type in DIY robotics. Its two signals are offset in phase: the phase difference reveals the direction of rotation, while the pulse frequency determines the rotational speed.

Experiment with the simulation below. It models a 9-pole magnetic encoder disc on a motor shaft, with sensors A and B fixed at the disc's rim. Move the Input Voltage slider to spin the motor. You will see how the disc rotates, the sensors light up as each pole passes and the oscilloscope draws the two square-wave channels. The higher the voltage makes the shaft turn faster - watch the RPM readout climb and the pulse frequency on the scope rise with it. Reverse the voltage and see how the phase relationship between the A and B channels swaps. This is how a controller understands the direction of rotation.
Use the Pause button to freeze the motion and inspect the channel states at a single moment in time, or Stop to reset the voltage to zero.

Why Encoders Are Required: Closed-Loop Control

Without an encoder, you are using Open-Loop Control: you tell the motor to turn, but you have no way of knowing whether it actually did. The controller sends a command (for example, "spin at half power") and simply trusts the motor. If the battery is running low, the floor is slippery, or the robot is carrying an extra load, the real speed quietly drifts away from the intended one, and the controller never finds out.

This does not mean open-loop control is useless. It works perfectly fine when the load is predictable and precision does not matter: a fan spinning at a constant voltage, a conveyor belt, or a simple toy car. The trouble starts when the real world changes — an incline, a heavier payload, worn gears, a weakening battery. Then the same command produces a different result every time, with no way to correct it.

If you have an encoder, you can create a Closed-Loop Control. The system "closes the loop" by constantly comparing the encoder data (feedback) against the original command. If the motor is spinning too slowly because the robot is heavy, the controller sees this in the encoder data and automatically increases the power to compensate.

Block diagram of an open-loop and closed-loop motor control system
Block diagram of an open-loop and closed-loop motor control system

Look at the diagram above. On the top, in the open-loop chain, information flows in only one direction — from the controller to the motor, and that is the end of the story. On the bottom, the encoder adds a return path: a feedback arrow that carries the measured speed (or position) back to the controller. The one-way chain becomes a loop, and the controller can finally answer the question it previously could not: "Did the motor actually do what I asked?"

Inside this loop, three things happen continuously, many times per second:

  • Measure: The encoder reports the shaft's real rotation as a stream of pulses or data packets.
  • Compare: The controller checks the measured value against the target you commanded.
  • Correct: If the two differ, the controller adjusts the power sent to the driver — raising it when the motor lags behind, lowering it when the motor overshoots.

That constant measuring, comparing, and correcting is what makes the robot's behavior predictable. Whether it rolls over a carpet, climbs a slight ramp, or carries a heavier payload, the closed loop keeps nudging the motor back toward the target.

How the controller decides exactly how much power to add at every correction is the job of control algorithms such as PID. We will explore them in detail in a separate article — for now, the key takeaway is simple: the encoder provides the eyes, and the loop provides the correction.

What's Next? In our next article, we’ll explore the PID algorithm, experiment with an interactive closed-loop simulator, and learn how to implement a PID controller.
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.