DC motor speed control
About this lesson
One whole design, start to finish: model a small DC motor from its two equations, turn them into a transfer function, set specs, then tune P, PI and PID. The textbook PID meets every spec on paper, then turns out to need 10 100 V from a 24 V driver. A PI tuned for the volts you actually have meets the specs for real and shrugs off a drag on the shaft.
Hold a motor at 1 rad/s
A small DC motor spins a flywheel. Our job: hold it at 1 rad/s (radians per second, about 9.5 turns a minute), even when something drags on the shaft. We go the whole way: model, tune, then the snags textbooks skip.
setpoint 1 rad/s (1 rad/s = 60/2π ≈ 9.5 rpm) plant DC motor + flywheel actuator driver, −24 to +24 V sensor encoder on the shaft disturbance a drag torque on the load Model → transfer function → specs → tune → check what real hardware allows.
Read the lesson step by step
Hold a motor at 1 rad/s
A small DC motor spins a flywheel. Our job: hold it at 1 rad/s (radians per second, about 9.5 turns a minute), even when something drags on the shaft. We go the whole way: model, tune, then the snags textbooks skip.
setpoint 1 rad/s (1 rad/s = 60/2π ≈ 9.5 rpm) plant DC motor + flywheel actuator driver, −24 to +24 V sensor encoder on the shaft disturbance a drag torque on the load Model → transfer function → specs → tune → check what real hardware allows.Two equations, one motor
Voltage V drives current i through the winding (R = 1 Ω, L = 0.5 H); the spinning rotor pushes back with K·ω. Current makes torque K·i, which turns the rotor (inertia J = 0.01 kg·m²) against friction b·ω (b = 0.1 N·m·s).
electrical L·di/dt = V − R·i − K·ω mechanical J·dω/dt = K·i − b·ω − T_load J = 0.01 kg·m² b = 0.1 N·m·s K = 0.01 N·m/A (= V·s/rad) R = 1 Ω L = 0.5 H K·ω is the back-emf: a spinning motor is also a generator.The transfer function
Laplace-transform both and remove i: ω/V = 0.01 / (0.005s² + 0.06s + 0.1001). Its poles (the motor's natural decay rates, 1/0.5 s and 1/0.1 s) are −2.0 and −10.0: overdamped, no overshoot. One volt gives 0.0999 rad/s, so we fix 10.01 V.
ω(s) K 0.01 ──── = ───────────────── = ────────────────────── V(s) (Js+b)(Ls+R) + K² 0.005s² + 0.06s + 0.1001 poles s = −2.0, −10.0 (time constants 0.5 s, 0.1 s) DC gain 0.0999 rad/s per volt open loop V = 1 / 0.0999 = 10.01 VA drag on the shaft
At 2.5 s a brake puts 0.01 N·m of drag on the flywheel. The voltage stays at 10.01 V because nothing measures the speed, so the motor slows to 0.90 rad/s and stays there. That 10 % loss is a disturbance.
t = 2.5 s T_load 0 → 0.01 N·m V stays 10.01 V steady drop = T_load·R / (R·b + K²) = 0.01 / 0.1001 = 0.0999 rad/s speed at 4 s 0.90 rad/sMeasure, compare, set specs
An encoder measures the shaft speed and the controller computes the error e = 1 − ω every millisecond. Our specs for a 1 rad/s step: settle within 2 s, overshoot under 5 %, steady error under 1 %.
every 1 ms: read ω from the encoder e = setpoint − ω = 1 − ω compute V, send it to the driver specs (1 rad/s step) settling time (2 %) < 2 s overshoot < 5 % steady-state error < 1 %Proportional only
Try V = 100·e. The speed overshoots by 25.3 % and settles at 0.909 rad/s, 9.1 % short: a P controller needs some error to make any voltage. It fails two specs. More gain shrinks the error but adds overshoot.
V = 100·e final speed = 100·0.0999 / (1 + 100·0.0999) = 0.909 rad/s steady error 9.1 % spec < 1 % ✗ overshoot 25.3 % spec < 5 % ✗ characteristic eq. s² + 12s + 220.02Add integral action
Add 200 × the running total of error × time. While any error is left the total keeps growing, so the steady error goes to zero. But the extra push makes the overshoot worse: 30.9 %.
V = 100·e + 200·∫e dt steady error 0 % spec < 1 % ✓ overshoot 30.9 % spec < 5 % ✗ settling 0.78 s spec < 2 s ✓ The integral term makes the loop type 1: zero error for a constant setpoint.Add derivative: done on paper
Add 10 × the rate of change of the error, a brake on fast swings. Overshoot drops to 1.0 % and the speed settles in 0.26 s with no error. Every spec is met. This is where most textbook examples stop.
# textbook PID prints 1.0 10100.2: 1 kHz digital loop, motor by RK4 J, b, K, R, L, dt = 0.01, 0.1, 0.01, 1.0, 0.5, 0.001 f = lambda w, i, V: ((K*i - b*w)/J, (V - R*i - K*w)/L) w = i = I = e_old = 0.0; top = Vtop = 0.0 for n in range(2500): # 2.5 s e = 1.0 - w; I += e*dt V = 100*e + 200*I + 10*(e - e_old)/dt; e_old = e a = f(w, i, V); c = f(w + dt/2*a[0], i + dt/2*a[1], V) d = f(w + dt/2*c[0], i + dt/2*c[1], V); g = f(w + dt*d[0], i + dt*d[1], V) top, Vtop = max(top, w), max(Vtop, V) w, i = (w + dt/6*(a[0] + 2*c[0] + 2*d[0] + g[0]), i + dt/6*(a[1] + 2*c[1] + 2*d[1] + g[1])) print(round((top - 1)*100, 1), round(Vtop, 1))Now look at the voltage
The setpoint jumps, so the error's rate of change is huge for one sample: the PID asks for 10 100 V for 1 ms (derivative kick), then −102 V to brake the surge. Holding speed takes 10.0 V; our driver gives 24 V at most.
t = 0 e jumps 0 → 1 D = (1 − 0) / 0.001 = 1000 per s V = 100 + 0.2 + 10·1000 = 10 100 V t = 2 ms V = −102 V (the D term brakes the surge) steady V ≈ 10.0 V (no drag) driver limit ±24 V The paper design assumes volts we do not have.Tune for the volts you have
Drop the derivative and use V = 20·e + 40·∫e. Ki/Kp = 2 puts the PI zero at −2, cancelling the slow motor pole. Overshoot 1.7 %, settling 0.58 s, peak 21.2 V: every spec, inside 24 V.
# tuned PI prints 1.7 21.2: 1 kHz digital loop, motor by RK4 J, b, K, R, L, dt = 0.01, 0.1, 0.01, 1.0, 0.5, 0.001 f = lambda w, i, V: ((K*i - b*w)/J, (V - R*i - K*w)/L) w = i = I = e_old = 0.0; top = Vtop = 0.0 for n in range(2500): # 2.5 s e = 1.0 - w; I += e*dt V = 20*e + 40*I a = f(w, i, V); c = f(w + dt/2*a[0], i + dt/2*a[1], V) d = f(w + dt/2*c[0], i + dt/2*c[1], V); g = f(w + dt*d[0], i + dt*d[1], V) top, Vtop = max(top, w), max(Vtop, V) w, i = (w + dt/6*(a[0] + 2*c[0] + 2*d[0] + g[0]), i + dt/6*(a[1] + 2*c[1] + 2*d[1] + g[1])) print(round((top - 1)*100, 1), round(Vtop, 1))The drag again
The same 0.01 N·m drag hits at 2.5 s. The speed dips to 0.932 rad/s, the integral raises the voltage to 11.0 V, and it is back within 1 % after 0.59 s. Open loop lost 10 % for good.
drag at 2.5 s, 0.01 N·m open loop tuned PI lowest speed 0.90 0.932 voltage after 10.01 V 11.0 V speed at 4 s 0.90 1.000 The integral found the extra 1 V by itself.
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the motor model, what is the term K·ω in the electrical equation?
A spinning DC motor is also a generator. It produces a voltage K·ω that opposes the applied voltage. The torque is K·i, in the mechanical equation.
02With V = 100·e alone, the speed settles at 0.909 rad/s instead of 1. Why?
Holding 1 rad/s needs about 10.0 V, and V = 100·e can only give that if e is not zero. The balance point is 0.909 rad/s, an error of 9.1 %. Integral action removes it.
03Why does the textbook PID ask for 10 100 V at the very first sample?
D = (e − e_old)/dt = (1 − 0)/0.001 = 1000 per second, times Kd = 10, gives 10 000 V on top of the 100 V from the P term and 0.2 V from the integral.
04Why does choosing Ki/Kp = 2 work so well for this motor?
The motor's poles are at about −2 and −10. The PI zero at −Ki/Kp = −2 cancels the slow one, leaving a fast, well damped loop: 1.7 % overshoot and 0.58 s settling with at most 21.2 V.
05A drag of 0.01 N·m hits the shaft. Why does the tuned PI get back to 1 rad/s while open loop stays at 0.90?
The encoder sees the speed drop, the error builds up in the integral, and the voltage rises from 10.0 V to 11.0 V until the error is gone. Open loop never measures the speed, so it keeps 10.01 V.
Glossary
- Back-emf
- The voltage a spinning motor generates, K·ω, which opposes the applied voltage. K is also the torque constant: torque per amp, K·i. In SI units both are the same number.
- Laplace transform
- A tool that turns a system's differential equations into ordinary algebra in a new variable s, so they can be solved with fractions instead of calculus.
- Transfer function
- The ratio of output to input after the Laplace transform, here ω(s)/V(s). Its poles set how fast and how smoothly the motor responds.
- Pole
- A value of s that makes a transfer function's denominator zero. Each pole p is one natural motion, e^(pt); a pole at −2 is a decay with a 0.5 s time constant.
- Overdamped
- Settles to its final value without any overshoot. A system whose poles are all real and negative, like this motor, is overdamped.
- Zero
- A value of s that makes a transfer function's numerator zero. A PI controller adds one at −Ki/Kp; placed on a stable pole, it cancels that pole's slow motion.
- Integral action
- A controller term that adds up error × time. While any error remains it keeps growing, so it drives the steady error to zero.
- Derivative kick
- A huge spike in a PID's output when the setpoint jumps, because the error's rate of change is very large for one sample. It can ask for far more than the driver's ±24 V.
Notes and sources
- The motor values (J = 0.01 kg·m², b = 0.1 N·m·s, K = 0.01 N·m/A, R = 1 Ω, L = 0.5 H) are an illustrative small motor, the set many university tutorials use for this example, not a datasheet. Every curve and number in the figure is computed from them.
- The controller runs every 1 ms and holds its voltage in between, as in the lesson "Writing a digital PID loop". Between updates the motor equations are stepped with fourth-order Runge-Kutta, accurate far beyond the digits shown. The controller itself is discrete: the 10 100 V kick exists because the derivative is taken over one 1 ms sample; a continuous PID would give an impulse instead.
- A common fix for derivative kick is to differentiate the measured speed instead of the error, since the setpoint jump then never reaches the derivative. Here that cuts the peak to 100 V but raises the overshoot to 11.6 %, so the gains would have to be tuned again. The derivative also amplifies encoder noise, a second reason to leave it out when it is not needed.
- If a 24 V driver simply clipped the textbook PID's demand, the integral would keep growing while the voltage is stuck at the limit and the speed would overshoot. That is integrator windup, the subject of "Actuator limits and integrator windup".
- The PI zero at −Ki/Kp = −2 sits almost on the motor's slow pole (−2.003), so the loop behaves like s² + 10s + 40: natural frequency 6.3 rad/s, damping ratio 0.79. Cancelling a stable pole is fine; never try it with an unstable one.
- N. S. Nise, Control Systems Engineering, 8th ed., Wiley (§2.8 electromechanical systems; ch. 9 PID design)
- K. Ogata, Modern Control Engineering, 5th ed., Pearson (DC motor model; ch. 8 PID controllers and PI-D control)
- G. F. Franklin, J. D. Powell, A. Emami-Naeini, Feedback Control of Dynamic Systems, 8th ed., Pearson (ch. 2 DC motor; ch. 4 PID)
- MIT OpenCourseWare 16.06 Principles of Automatic Control (S. Hall)
- MIT OpenCourseWare 6.302 Feedback Systems (Roberge, Dawson, Lundberg)
- Python Control Systems Library (python-control) documentation
- python-control: control.step_response
Quiz Unlocks after the last step.