---
title: Digital control
heading: Controllers that run as code on a microcontroller, sampling and the zero-order hold, the half-sample delay a hold adds, PID written as a difference equation, mapping poles from s to z, and a sampled loop that goes unstable when its gain times its sample period exceeds two
eyebrow: D3 · Lesson 8 · Digital control
xp: 40
---

::: hook
Nearly every controller built today is a few lines of code in a loop: read the sensor through an ADC, compute the control law, write the result to a PWM or DAC, wait for the next tick. The continuous-time ideas of the earlier lessons carry over almost unchanged as long as the loop runs fast enough. When it doesn't, sampling shows up as a delay, and delay, as lesson 7 showed, eats phase margin. A sampled loop can go unstable at a gain the continuous design would have tolerated forever.
:::

::: play
- A sampled loop around an integrating plant (for example, a motor's speed driven by a current command), with proportional gain K and sample period T: $y[k + 1] = y[k] + KT(r - y[k])$.
- The closed-loop pole is at $z = 1 - KT$. For KT < 1 the output creeps up smoothly; KT = 1 reaches the setpoint in one sample (deadbeat); between 1 and 2 it overshoots and alternates; above 2 it diverges.
- The continuous version of this loop is stable at any gain: the instability comes entirely from sampling.
:::

```sim plot
view: graph
x: k = 0..20
y: y = -0.5..2.5
slider: a = 0.1..2.2 step 0.05 = 0.5
plot: 1 + 0*k | dashed | color: ch3 | label: setpoint
plot: 1 - Re((1 - a)^k) | stems | label: output, sample by sample (a = KT)
readout: closed-loop pole z = 1 - a | digits: 3
```

::: predict
## A sampled proportional loop around an integrator runs every 10 ms. Above what gain K does it go unstable?

- [ ] It never does
- [ ] K = 100 s⁻¹
- [x] K = 200 s⁻¹
- [ ] K = 2 s⁻¹

::: reveal
The pole is at z = 1 − KT, and a discrete system is stable while all its poles lie inside the unit circle, |z| < 1. That requires 0 < KT < 2, so K < 2/0.01 = 200 s⁻¹. Halving the sample period doubles the allowed gain.
:::
:::

::: nameit
## Sampling and the hold

The controller sees the measurement only every T seconds, and a **zero-order hold** (the DAC or PWM register) keeps each output constant until the next update. On average the hold delays the signal by half a sample, costing phase

$$\Delta\phi \approx \frac{\omega T}{2} \text{ rad}$$

at frequency ω. A common rule is to sample 10 to 30 times faster than the closed-loop bandwidth, which keeps that loss at the crossover to a few degrees.

## From s to z

A continuous pole at s maps to a discrete pole at $z = e^{sT}$. The left half of the s-plane maps inside the unit circle, so a discrete system is stable when every pole has |z| < 1 (C9's z-plane).

## PID as code

Approximating the integral by a running sum and the derivative by a difference gives

$$u[k] = K_pe[k] + K_iT\sum_{i \le k}e[i] + \frac{K_d}{T}(e[k] - e[k - 1])$$

In practice the derivative is filtered, the integrator is clamped when the output saturates (anti-windup), and the loop runs from a timer interrupt so that T is steady (C12).
:::

::: example
## A loop crosses over at 50 rad/s and is sampled every 2 ms. How much phase does the hold cost? Where does a continuous pole at s = −10 land?

::: solution
1. **Phase cost of the hold.**
   $$\begin{aligned} \omega T/2 &= 50 \times 0.002/2 \\ &= 0.05\ \text{rad} \end{aligned}$$
   or 2.9°, a small price.
2. **Mapping the pole.** With T = 2 ms:
   $$\begin{aligned} z &= e^{-10 \times 0.002} \\ &= 0.980 \end{aligned}$$
   close to 1, since the pole's time constant (100 ms) is fifty samples long.

**Answer:** the hold costs 2.9° of phase, and the pole lands at z = 0.980.
:::
:::

::: example
## The integrator loop runs with K = 50 s⁻¹ and T = 10 ms. Where's its pole, and how does it respond to a step?

::: solution
1. **The pole.**
   $$\begin{aligned} KT &= 50 \times 0.01 \\ &= 0.5 \end{aligned}$$
   so z = 0.5.
2. **Step response.** Each sample halves the remaining error. After four samples (40 ms) it has reached
   $$1 - 0.5^4 = 94\%$$
   of the step.

**Answer:** the pole is at z = 0.5, and the output reaches 94% of the step after four samples (40 ms).
:::
:::

::: example
## Map a continuous pole at s = −100 with T = 10 ms, exactly and by the Tustin (bilinear) approximation.

::: solution
1. **Exact mapping.**
   $$\begin{aligned} z &= e^{sT} \\ &= e^{-1} \\ &= 0.368 \end{aligned}$$
2. **Tustin.**
   $$\begin{aligned} z &= \frac{1 + sT/2}{1 - sT/2} \\ &= \frac{1 - 0.5}{1 + 0.5} \\ &= 0.333 \end{aligned}$$
3. **Compare.** The two agree for slow poles and drift apart as $|s|T$ grows. Tustin keeps every stable pole inside the unit circle, which is why it's used to convert continuous designs.

**Answer:** z = 0.368 exactly, and 0.333 by Tustin.
:::
:::

::: check
given: w = 20 | 100 | 500; T = 0.5 | 1 | 5
answer: w*T*1e-3/2*180/pi
unit: °
tolerance: 0.5%

## A loop crosses over at {w} rad/s and is sampled every {T} ms. About how much phase does the zero-order hold cost at crossover?

::: right
Right. $\omega T/2$ = {answer}°.
:::

::: misses
- [w*T*1e-3*180/pi] The hold's average delay is half a sample.
- [w*T*1e-3/2] Convert radians to degrees.
- [any] Not yet. Half a sample of delay.
:::
:::

::: check
given: s = 2 | 10 | 50; T = 1 | 5 | 20
answer: exp(-s*T*1e-3)
tolerance: 0.5%

## A continuous pole at s = −{s} s⁻¹ is sampled every {T} ms. Where does it land in the z-plane?

::: right
Right. $z = e^{sT}$ = {answer}.
:::

::: misses
- [any] Not yet. $z = e^{sT}$.
:::
:::

::: check
given: T = 1 | 5 | 20
answer: 2/(T*1e-3)
unit: s⁻¹
tolerance: 0.5%

## A sampled proportional loop around an integrator runs every {T} ms. Above what gain does it go unstable?

::: right
Right. KT < 2 gives K < {answer} s⁻¹.
:::

::: misses
- [1/(T*1e-3)] That's the deadbeat gain; instability starts at twice it.
- [any] Not yet. The pole 1 − KT must stay inside the unit circle.
:::
:::

::: check
given: B = 5 | 20 | 100; n = 10 | 20 | 30
answer: n*B
unit: Hz
tolerance: 0.5%

## A loop needs a closed-loop bandwidth of {B} Hz, and you want to sample {n} times faster than that. What sample rate do you need?

::: right
Right. {answer}: {n} samples in every period of the fastest signal the loop has to follow.
:::

::: misses
- [any] Not yet. Bandwidth times the oversampling factor.
:::
:::

::: check
kind: parts
given: p = 10 | 50 | 200; T = 1 | 5 | 10

## A continuous pole at s = −{p} is sampled every {T} ms.

::: part
answer: exp(-p*T/1000)
tolerance: 0.5%

### Where does it map exactly?

::: misses
- [any] $z = e^{sT}$.
:::
:::

::: part
answer: (1 - p*T/2000)/(1 + p*T/2000)
tolerance: 0.5%

### Where does Tustin put it?

::: misses
- [any] $(1 + sT/2)/(1 - sT/2)$.
:::
:::
:::

::: check
kind: parts
given: w = 10 | 50 | 100; T = 1 | 2 | 5

## A loop crossing over at {w} rad/s is sampled every {T} ms.

::: part
answer: w*T/1000/2*180/pi
unit: °
tolerance: 0.5%

### How much phase does the zero-order hold cost?

::: misses
- [any] $\omega T/2$.
:::
:::

::: part
answer: 2*pi/(T/1000)/w
tolerance: 0.5%

### How many times the crossover is the sample rate (in rad/s)?

::: misses
- [any] $2\pi/T$ over $\omega_c$.
:::
:::
:::

::: explain
## Why can sampling make a feedback loop unstable when the continuous version wasn't?

::: model
A sampled controller only learns about the output at discrete instants and holds its output constant between updates, so it always acts on information that is up to one sample old and keeps applying a correction after the plant has moved. That staleness behaves like a delay of about half a sample, which adds phase lag that grows with frequency and erodes the loop's phase margin. At high gain the controller overcorrects on each sample: in the simplest case each update overshoots the setpoint by more than the previous error, and the alternating error grows. Sampling faster shrinks the delay and pushes this limit to higher gains.
:::
:::

::: practice

::: check
given: s = 0.5 | 200; T = 0.1 | 50
answer: exp(-s*T*1e-3)
tolerance: 0.5%

## z for s = −{s} with T = {T} ms?

::: misses
- [any] $e^{sT}$.
:::
:::

::: check
given: T = 0.05 | 0.1 | 1 | 5 | 10 | 100
answer: 2/(T*1e-3)
unit: s⁻¹
tolerance: 0.5%

## Gain limit for a sampled integrator loop, T = {T} ms?

::: misses
- [any] 2/T.
:::
:::

::: check
given: w = 1 | 5000; T = 0.01 | 10
answer: w*T*1e-3/2*180/pi
unit: °
tolerance: 0.5%

## Hold phase loss at {w} rad/s, T = {T} ms?

::: misses
- [any] $\omega T/2$ in degrees.
:::
:::
:::
