Atlas Robotics
Motor ControlIntermediate

Motor speed control: from open-loop to closed-loop PID

Atlasbot · 16/8/2026 · 11 min read

Motor speed control: from open-loop to closed-loop PID
Open-loop vs Closed-loop when load changes

The previous three articles have separated each piece: The DC motor needs a driver to run, the encoder measures the actual speed, and the PID knows how to calculate and adjust based on errors. This article combines all three into a complete speed control loop — something that lies at the core of every stable-moving AMR wheel.

Key concepts

There are two ways to control motor speed, differing in only one point: whether to use feedback or not.

Open-loop — just set the PWM, don't check the results

The simplest way: decide on a fixed PWM value corresponding to the "desired speed", send it straight to the driver, and trust that the motor will rotate at that speed.

set_motor_pwm(600);  // "Hopefully" the motor rotates at about 60% of maximum speed

Problem: the actual rotation speed depends both on battery voltage (decreases when the battery is low) and load (the robot climbs hills or runs on flat floors) — the same PWM value, the actual speed will vary depending on the conditions. Open-loop accepts this imprecision in exchange for simplicity.

Closed-loop — real speed measurement, continuously self-adjusting

A more accurate way: use an encoder to measure the actual speed, feed it into the PID to automatically recalculate the PWM value each cycle, so that the measured speed always closely follows the desired speed — regardless of load or battery voltage changes.

In summary: Open-loop is cheap and simple, accepts errors according to load · Closed-loop (encoder + PID) is a bit more complicated but keeps a truly stable speed — almost mandatory for AMR wheels, where the load and floor slope change continuously during operation.

How it works

Complete speed control loop, running periodically in the Timer interrupt (e.g. every 10ms = 100Hz):

void speed_control_loop(void) {  // Call periodically in Timer interrupt
    int32_t pulses = __HAL_TIM_GET_COUNTER(&htim2);
    __HAL_TIM_SET_COUNTER(&htim2, 0);            // Read & reset encoder counter

    float measured_speed = pulses_to_rpm(pulses); // Convert number of pulses → rpm

    float error = target_speed - measured_speed;
    integral += error * DT;
    float derivative = (error - prev_error) / DT;

    float pwm = Kp * error + Ki * integral + Kd * derivative;
    pwm = constrain(pwm, -1000, 1000);            // Limited to valid PWM range

    set_motor_pwm(pwm);
    prev_error = error;
}
Flat deck, light load: Stable low-level PWM to keep target speed
Climbing hills, heavy loads: PID automatically INCREASES PWM to compensate, speed remains stable
Going downhill: PID automatically REDUCES PWM, avoiding overspeeding

Technical details

Formulas

Quantitative comparison of speed errors between the two approaches when the load changes suddenly (eg: the robot starts climbing a slope):

Open-loop error (%) = |actual speed - desired speed| /desired speed × 100%

Example: Fixed PWM for desired speed of 100 RPM on flat floor
When climbing a hill, the actual speed drops to 70 RPM (PWM remains constant, load increases)
Open-loop error = |70 - 100| / 100 × 100% = 30%

Closed-loop (well-tuned PID): after a settling time, usually several hundred ms
up to a few seconds depending on the system), the error is usually kept below 1-5%.

Parameters

Quick comparison table to help decide when you really need to invest in an additional encoder + PID:

CriterionOpen-loopClosed-loop (encoder + PID)
Accuracy when loading changesLow (error can be 20-30%+)High (usually less than a few % after stabilization)
Hardware costsLow (no encoder needed)Higher (needs encoder + additional processing)
Code complexityVery simpleNeed to tune Kp/Ki/Kd
Suitable forRadiator fan, fixed loadwheel AMR, load changes continuously

Practical applications

In addition to keeping a steady speed, having a closed-loop available also opens up possibilities wheel slip detection — if the PID has to continuously increase the PWM to near maximum but the speed measured from the encoder still does not increase proportionally, this is a sign that the wheel is sliding on a slippery floor or hanging off the ground (eg: a wheel is at the edge of a step) — this information can be used as a safety warning signal, something an open-loop system is completely incapable of detecting because it never knows what the actual speed is.

Practical example

#include <stdio.h>

int main() {
    float target = 100.0;   // Desired RPM
    float pwm_openloop = 600;   // Fixed PWM value corresponds to 100 RPM on a flat floor

    // Simple simulation: each PWM unit generates a different speed depending on the load
    float speed_flat = pwm_openloop * 100.0 / 600.0;      // Flat floor: exactly as designed
    float speed_incline_openloop = speed_flat * 0.7;       // climbing: 30% drop (open-loop helpless)

    // Closed-loop: PID automatically increases PWM to compensate, assuming it reaches ~98% of target after stabilization
    float speed_incline_closedloop = target * 0.98;

    printf("Sàn phẳng (open-loop):        %.1f RPM\n", speed_flat);
    printf("Leo dốc (open-loop):          %.1f RPM (sai so %.0f%%)\n",
           speed_incline_openloop, (target - speed_incline_openloop)/target*100);
    printf("Leo dốc (closed-loop PID):    %.1f RPM (sai so %.0f%%)\n",
           speed_incline_closedloop, (target - speed_incline_closedloop)/target*100);
    return 0;
}
Output:
San phang (open-loop):        100.0 RPM
Leo doc (open-loop):          70.0 RPM (sai so 30%)
Leo doc (closed-loop PID):    98.0 RPM (sai so 2%)

The results illustrate the exact comparison table in the Technical Knowledge section — facing variable load (hill climbing), open-loop accepts an error of 30%, while closed-loop PID self-adjusts to keep the error to only about 2%.

Common mistakes

  • Using open-loop for continuously varying load applications — choosing the wrong tool for the problem, as shown above; Open-loop is only suitable when the load is almost fixed (eg: cooling fan), not suitable for wheel robots moving on changing terrain.
  • Do not handle encoder counter overflow — the encoder counter register on many MCUs is only 16-bit. If not read and reset regularly (like line __HAL_TIM_SET_COUNTER(&htim2, 0) in the example above), the count can overflow between 2 readings, causing incorrect speed calculations at that exact time.
  • Unstable control loop frequency — if speed_control_loop() does not run on a fixed period (e.g. inserted by another higher priority interrupt, or placed in loop() instead of a Timer interrupt), the assumed DT value in the code will not match the actual elapsed time, distorting both parts I and D of the PID.
  • Skip the tuning step when changing robot motors/loads — the Kp/Ki/Kd set that is well adjusted for a specific motor/load does not automatically fit when changing to another motor or the robot load changes significantly (eg: adding goods to the cargo robot) — needs to be checked and adjusted if there are significant hardware changes.

Conclusion

Combining a DC motor, encoder, and PID into a complete closed-loop control is the foundation for any AMR wheel that needs to move stably through changing terrain and loads — the trade-off is higher hardware costs and tuning effort than open-loop, but is almost mandatory for real-world applications. This is also the last article of the Motor Control category, closing the entire embedded programming (Embedded) category — from Foundational Knowledge, Electronic Components, Microcontroller, C/C++ Programming, Hardware Communication, to Motor Control just learned, enough foundation to start building a complete real-time control layer for an actual AMR robot.

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.

Motor speed control: from open-loop to closed-loop PID — Atlas Robotics