---
title: "Motor controller: modeling the motor"
heading: The DC motor as two coupled first-order systems, electrical and mechanical, the back-EMF and torque constant that link them, steady speed and current under load, stall current, the two time constants and the poles they become, and a simulation to check them against
eyebrow: D13 · Lesson 9 · Motor controller
xp: 40
---

::: hook
To control a motor's speed you need to know how it responds, and a brushed DC motor is one of the most predictable machines there is. Its winding is a resistor and an inductor in series with a back-EMF proportional to speed; its rotor is an inertia turned by a torque proportional to current. Two equations, five constants from the datasheet, and you can predict the current surge at start-up, the top speed, how far it slows under load and how fast it gets there.
:::

::: play
- A motor (1 Ω, 0.05 V·s/rad, friction 10⁻⁵ N·m·s/rad) switched onto V volts from rest, with rotor inertia J (in units of 10⁻⁵ kg·m²) and winding inductance L.
- The current leaps toward the stall current V/R, then falls as the rotor speeds up and its back-EMF opposes the supply. Speed settles at about 4570 rpm for 24 V.
- Lower J or raise L and the two parts start to interact: the current's peak drops, and with very little inertia the speed overshoots and rings.

```sim plot
view: graph
x: t = 0..50
y: y = 0..25
slider: V = 6..24 step 1 = 24
slider: J = 0.2..20 = 2 log
slider: Lm = 0.1..10 = 1 log
plot: (V*0.05/0.00251)*(1 - Re(exp(-((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))*(cosh(sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000)) + ((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))*sinh(sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))/sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9))))*60/(2*pi)/1000 | samples: 800 | label: speed, thousands of rpm (t in ms)
plot: (J*1e-5*(V*0.05/0.00251)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*Re(exp(-((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))*sinh(sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))/sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)) + 1e-5*(V*0.05/0.00251)*(1 - Re(exp(-((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))*(cosh(sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000)) + ((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))*sinh(sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))*(t/1000))/sqrt(((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5))))^2 - 1 + 1e-9)))))/0.05 | samples: 800 | color: ch3 | label: current, A
readout: steady speed, rpm = V*0.05/0.00251*60/(2*pi) | digits: 4
readout: stall current, A = V/1 | digits: 3
readout: damping ratio = ((1/(Lm*1e-3) + 1e-5/(J*1e-5))/(2*sqrt((1e-5 + 0.0025)/(Lm*1e-3*J*1e-5)))) | digits: 3
```

::: predict
## A motor with 1 Ω of winding resistance is switched onto 24 V from rest. What current flows at the instant it starts turning?

- [ ] About 0.1 A, its running current
- [ ] About 12 A
- [x] Up to 24 A, limited only by the resistance
- [ ] Nothing until it's up to speed

::: reveal
At rest there's no back-EMF, so only the winding's resistance limits the current: 24 V/1 Ω = 24 A, the **stall current**. The inductance slows the rise, and by the time it peaks (19.5 A after 2.5 ms here) the rotor is already moving. A controller that doesn't limit current would make the supply and switches carry this surge at every start.
:::
:::

::: nameit
## The equations

$$L\frac{di}{dt} = v - Ri - K\omega, \qquad J\frac{d\omega}{dt} = Ki - B\omega - T_L$$

The same constant K is the back-EMF constant (V·s/rad) and the torque constant (N·m/A), since the motor converts power without loss between $Ki$ torque at speed ω and back-EMF Kω times current i.

## Steady state

Setting both derivatives to zero:

$$\omega = \frac{KV - RT_L}{K^2 + RB}, \qquad i = \frac{B\omega + T_L}{K}$$

so speed falls in a straight line with load, by $R/K^2$ rad/s per N·m when friction is small. The stall current is V/R and the stall torque KV/R.

## Dynamics

The **mechanical time constant** $\tau_m = JR/(K^2 + RB)$ sets how fast speed responds; the **electrical** one, $\tau_e = L/R$, how fast current responds. When $\tau_e \ll \tau_m$ they act separately; here (1 ms against 8 ms) they interact, and the poles are the eigenvalues of

$$A = \begin{pmatrix} -R/L & -K/L \\ K/J & -B/J \end{pmatrix}$$

−853 and −147 s⁻¹ for the motor above. Small inertia or large inductance can make the poles complex, and the speed then overshoots. Lesson 10 controls the current fast enough to hide the electrical dynamics, then controls speed around it.
:::

::: example
## What are the motor's no-load speed and current at 24 V, and how far does 0.05 N·m of load slow it?

::: solution
1. **No-load speed.**
   $$\begin{aligned} \omega &= \frac{24 \times 0.05}{0.0025 + 10^{-5}} \\ &= 478.1\ \text{rad/s} \\ &= 4565\ \text{rpm} \end{aligned}$$
2. **No-load current.**
   $$\begin{aligned} I &= \frac{B\omega}{K} \\ &= 0.096\ \text{A} \end{aligned}$$
3. **Speed with 0.05 N·m.**
   $$\begin{aligned} \omega &= \frac{1.2 - 0.05}{0.00251} \\ &= 458.2\ \text{rad/s} \end{aligned}$$
4. **The drop.** It's 19.9 rad/s, close to
   $$\frac{RT_L}{K^2} = 20\ \text{rad/s}$$
5. **Loaded current.** The current rises to 1.09 A.

**Answer:** 478.1 rad/s (4565 rpm) at 0.096 A with no load; 0.05 N·m slows it by 19.9 rad/s to 458.2 rad/s, at 1.09 A.
:::
:::

::: example
## What are its time constants and poles?

::: solution
1. **Electrical time constant.**
   $$\begin{aligned} \tau_e &= \frac{1\ \text{mH}}{1\ \Omega} \\ &= 1\ \text{ms} \end{aligned}$$
2. **Mechanical time constant.**
   $$\begin{aligned} \tau_m &= \frac{2 \times 10^{-5}}{0.00251}\ \text{s} \\ &= 7.97\ \text{ms} \end{aligned}$$
3. **Poles.** The characteristic polynomial is
   $$s^2 + 1000.5s + 125{,}500$$
   with roots −853.4 and −147.1 s⁻¹.
4. **Compare with the mechanical time constant.** The slow pole is near $1/\tau_m$ = 125 s⁻¹ but not equal to it, since the inductance delays the torque.

**Answer:** $\tau_e$ = 1 ms and $\tau_m$ = 7.97 ms, with poles at −853.4 and −147.1 s⁻¹.
:::
:::

::: check
kind: python

## Model the motor: its equations, steady state, poles and a simulated start.

```python starter
import numpy as np

P = dict(R=1.0, L=1e-3, K=0.05, J=2e-5, B=1e-5)   # ohms, henries, V·s/rad, kg·m², N·m·s/rad

def derivs(i, w, v, tl, p=P):
    # The motor's two equations: L di/dt = v - R i - K w and J dw/dt = K i - B w - tl.
    # Return (di/dt, dw/dt).
    return 0.0, 0.0

def steady_state(v, tl, p=P):
    # The current and speed where both derivatives are zero, solved with np.linalg.solve.
    # Return (i, w).
    return 0.0, 0.0

def poles(p=P):
    # The eigenvalues of A = [[-R/L, -K/L], [K/J, -B/J]], as a sorted array of real numbers.
    return np.zeros(2)

def simulate(v, tl, t_end, dt=5e-6, p=P):
    # Start at rest and integrate with RK4 using derivs. Return arrays t, i and w, one entry
    # per step, starting with t = 0.
    n = int(round(t_end/dt))
    return np.zeros(n + 1), np.zeros(n + 1), np.zeros(n + 1)
```

```python solution
import numpy as np

P = dict(R=1.0, L=1e-3, K=0.05, J=2e-5, B=1e-5)   # ohms, henries, V·s/rad, kg·m², N·m·s/rad

def derivs(i, w, v, tl, p=P):
    return (v - p['R']*i - p['K']*w)/p['L'], (p['K']*i - p['B']*w - tl)/p['J']

def steady_state(v, tl, p=P):
    a = np.array([[p['R'], p['K']], [p['K'], -p['B']]])
    i, w = np.linalg.solve(a, [v, tl])
    return i, w

def poles(p=P):
    a = np.array([[-p['R']/p['L'], -p['K']/p['L']], [p['K']/p['J'], -p['B']/p['J']]])
    return np.sort(np.linalg.eigvals(a).real)

def simulate(v, tl, t_end, dt=5e-6, p=P):
    n = int(round(t_end/dt))
    t, I, W = np.arange(n + 1)*dt, np.zeros(n + 1), np.zeros(n + 1)
    i = w = 0.0
    for k in range(n):
        a1, b1 = derivs(i, w, v, tl, p)
        a2, b2 = derivs(i + dt/2*a1, w + dt/2*b1, v, tl, p)
        a3, b3 = derivs(i + dt/2*a2, w + dt/2*b2, v, tl, p)
        a4, b4 = derivs(i + dt*a3, w + dt*b3, v, tl, p)
        i += dt/6*(a1 + 2*a2 + 2*a3 + a4)
        w += dt/6*(b1 + 2*b2 + 2*b3 + b4)
        I[k + 1], W[k + 1] = i, w
    return t, I, W
```

```python tests
assert np.allclose(derivs(1.0, 100.0, 24.0, 0.01), ((24 - 1 - 5)/1e-3, (0.05 - 1e-3 - 0.01)/2e-5)), "Check both equations against a hand calculation."
_i, _w = steady_state(24, 0)
assert abs(_w - 478.09) < 0.01 and abs(_i - 0.09562) < 1e-4, f"Unloaded at 24 V: 478.09 rad/s (4565 rpm) drawing 0.0956 A; got {_w:.2f} rad/s, {_i:.4f} A."
_i2, _w2 = steady_state(24, 0.05)
assert abs(_w2 - 458.17) < 0.01 and abs(_i2 - 1.0916) < 1e-4, f"With 0.05 N·m of load: 458.17 rad/s at 1.0916 A; got {_w2:.2f}, {_i2:.4f}."
assert np.allclose(poles(), [-853.45, -147.05], atol=0.01), f"The poles are −853.45 and −147.05 s⁻¹; got {poles()}."
_t, _I, _W = simulate(24, 0, 0.05)
assert len(_t) == 10001 and _t[0] == 0, "One entry per step from t = 0."
assert abs(np.max(_I) - 19.52) < 0.02 and abs(_t[np.argmax(_I)] - 0.00249) < 2e-5, f"The current peaks at 19.52 A after 2.49 ms; got {np.max(_I):.3f} A at {1000*_t[np.argmax(_I)]:.3f} ms."
_a = np.array([[-1000.0, -50.0], [2500.0, -0.5]])
_ev, _vec = np.linalg.eig(_a)
_xs = np.array([0.09561753, 478.0876494])
_x5 = (_vec @ np.diag(np.exp(_ev*0.005)) @ np.linalg.solve(_vec, -_xs)).real + _xs
assert abs(_W[1000] - _x5[1]) < 1e-3 and abs(_I[1000] - _x5[0]) < 1e-5, f"At 5 ms the exact solution is {_x5[1]:.4f} rad/s and {_x5[0]:.5f} A; got {_W[1000]:.4f} and {_I[1000]:.5f}."
```

::: right
Right. Your model's steady state, poles and simulated start match the exact solution, current surge included.
:::
:::

::: check
given: V = 12 | 24 | 48; K = 0.02 | 0.05 | 0.1
answer: V*K/(K^2 + 1e-5)*60/(2*pi)
unit: rpm
tolerance: 0.5%

## A motor with 1 Ω of resistance, friction 10⁻⁵ N·m·s/rad and K = {K} V·s/rad runs unloaded from {V} V. What's its steady speed?

::: right
Right. $KV/(K^2 + RB)$ is {answer} rpm.
:::

::: misses
- [V*K/(K^2 + 1e-5)] Convert rad/s to rpm.
- [any] Not yet. Set both derivatives to zero.
:::
:::

::: check
given: R = 0.5 | 1 | 2; K = 0.02 | 0.05 | 0.1; T = 0.02 | 0.05 | 0.1
answer: R*T/K^2
unit: rad/s
tolerance: 0.5%

## A motor with resistance {R} Ω and K = {K} V·s/rad takes on a load of {T} N·m. Ignoring friction, how much does its speed drop?

::: right
Right. $RT_L/K^2$ = {answer} rad/s.
:::

::: misses
- [R*T/K] The extra current is $T_L/K$, and its voltage drop $RT_L/K$ costs speed at $1/K$ rad/s per volt.
- [any] Not yet. Extra current times resistance, converted to speed.
:::
:::

::: check
given: J = 1 | 2 | 10; R = 0.5 | 1 | 2; K = 0.02 | 0.05 | 0.1
answer: J*1e-5*R/K^2*1000
unit: ms
tolerance: 0.5%

## Ignoring friction, what's the mechanical time constant of a motor with inertia {J} × 10⁻⁵ kg·m², resistance {R} Ω and K = {K}?

::: right
Right. $JR/K^2$ = {answer} ms.
:::

::: misses
- [J*1e-5*R/K*1000] K appears squared: once making torque, once making back-EMF.
- [any] Not yet. $\tau_m = JR/K^2$.
:::
:::

::: check
kind: parts
given: R = 0.5 | 2; L = 0.5 | 2; k = 0.02 | 0.05; V = 12 | 24

## A DC motor has $R$ = {R} Ω, $L$ = {L} mH and $k$ = {k} V·s/rad (or N·m/A), on {V} V.

::: part
answer: L*1e-3/R*1e3
unit: ms
tolerance: 0.5%

### What is its electrical time constant?

::: misses
- [R/(L*1e-3)*1e3] It's $L/R$.
- [any] Not yet. $\tau_e = L/R$.
:::
:::

::: part
answer: k*V/R
unit: N·m
tolerance: 0.5%

### What is its stall torque?

::: misses
- [k*V] Stall current is $V/R$; torque is $k$ times that.
- [any] Not yet. $T = kV/R$.
:::
:::

::: right
Right. The electrical time constant sets how fast current (torque) can change; the mechanical one is usually much slower.
:::
:::

::: check
given: J = 1 | 5 | 20; R = 0.5 | 2; k = 0.02 | 0.05
answer: J*1e-6*R/k^2*1e3
unit: ms
tolerance: 0.5%

## A motor with rotor and load inertia {J} × 10⁻⁶ kg·m², resistance {R} Ω and constant {k} N·m/A has what mechanical time constant, $JR/k^2$?

::: right
Right. $\tau_m = JR/k^2$ = {answer}.
:::

::: misses
- [J*1e-6*R/k*1e3] The constant enters squared: once as torque per amp, once as back-EMF per speed.
- [any] Not yet. $\tau_m = JR/k^2$.
:::
:::

::: explain
## Why does a DC motor draw so much more current when starting or stalled than when running freely?

::: model
The current through the winding is set by the difference between the applied voltage and the back-EMF, divided by the winding's small resistance. A spinning motor generates a back-EMF proportional to its speed, which at full speed nearly cancels the supply, so only a small current flows, just enough for the torque that friction and load require. At standstill there's no back-EMF at all, so the whole supply voltage drives current through the resistance alone, many times the running current. That's why motors need current limiting at start-up, and why a stalled motor overheats quickly.
:::
:::

::: practice

::: check
given: R = 0.2 | 5; K = 0.01 | 0.3; T = 0.005 | 0.5
answer: R*T/K^2
unit: rad/s
tolerance: 0.5%

## Speed drop: R = {R} Ω, K = {K}, load {T} N·m?

::: misses
- [any] $RT_L/K^2$.
:::
:::

::: check
given: J = 0.5 | 50; R = 0.3 | 3; K = 0.01 | 0.2
answer: J*1e-5*R/K^2*1000
unit: ms
tolerance: 0.5%

## $\tau_m$: J = {J} × 10⁻⁵, R = {R} Ω, K = {K}?

::: misses
- [any] $JR/K^2$.
:::
:::
:::
