---
title: "Algorithm/pseudo-code for software PID"
subject: "Embedded System"
chapter: 7
key: "semester_6/embedded_system:pid-software-algorithm"
source: https://www.bctengineer.com/q/semester_6/embedded_system/pid-software-algorithm
site: BCT Engineering
---

# Algorithm/pseudo-code for software PID

Embedded System · Chapter 7: Control System

- Asked in: 2070 Magh, 4 marks; 2073 Bhadra, 4 marks; 2074 Bhadra, 5 marks (asked 3 times)
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/pid-software-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-7

## Question

Write an algorithm (pseudo-code) to implement the PID controller in software.

## Short answer

The PID algorithm samples error at fixed period T, computes proportional, integral, and derivative terms discretely: P=Kp·e, I=I+Ki·T·e (clamped for anti-windup), D=Kd·(e-prev_e)/T. Output u=P+I+D is clamped to actuator limits and written to DAC/PWM. T must be much smaller than plant time constant for stability.

## Answer

A software PID samples the output at a fixed period T, computes the error, and updates the integral and derivative terms in discrete form. The routine is called from a timer interrupt every T seconds.

#### Discrete equations

e[k] = r - y[k]

I[k] = I[k-1] + Ki·T·e[k]

D[k] = Kd·(e[k] - e[k-1]) / T

u[k] = Kp·e[k] + I[k] + D[k]

#### Algorithm

1. Initialise Kp, Ki, Kd, sampling period T, I = 0, prev_error = 0, output limits u_min, u_max.
2. Wait for the timer tick (every T).
3. Read the sensor (ADC) and get y.
4. Compute error e = r - y.
5. Compute P = Kp·e.
6. Update I = I + Ki·T·e; clamp I between limits (anti-wind-up).
7. Compute D = Kd·(e - prev_error)/T.
8. Compute u = P + I + D; clamp u to [u_min, u_max].
9. Write u to the actuator (DAC / PWM).
10. Save prev_error = e and go to step 2.

#### Pseudo-code

```text
init: I = 0; prev_e = 0
every T seconds (timer ISR):
    y = ReadSensor()
    e = r - y
    P = Kp * e
    I = I + Ki * T * e
    if I > Imax then I = Imax
    if I < Imin then I = Imin
    D = Kd * (e - prev_e) / T
    u = P + I + D
    if u > Umax then u = Umax
    if u < Umin then u = Umin
    WriteActuator(u)
    prev_e = e
```

The choice of T matters: it should be much shorter than the plant's time constant; otherwise control is poor and the loop may become unstable.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is the software PID routine executed?
   A. Continuously as fast as possible with no timing
   B. From a timer interrupt every sampling period T
   C. Only when the error becomes zero
   D. Once at start-up
   Answer: B. A fixed period T is needed for the discrete integral and derivative to be correct. (Key point: Timer-driven sampling)

2. Which update gives the discrete integral term?
   A. I = I + Ki*T*e
   B. I = Ki*(e - prev_e)/T
   C. I = Kp*e
   D. I = I - Ki*e/T
   Answer: A. The integral accumulates error multiplied by Ki and T. (Key point: Discrete integral)

3. Why is the integral clamped between limits?
   A. To remove the derivative kick
   B. To increase the sampling rate
   C. To make the error zero faster
   D. To prevent wind-up when the actuator saturates
   Answer: D. Without clamping the integral grows while the output is saturated, causing large overshoot later. (Key point: Anti-wind-up)

4. How should T relate to the plant's time constant?
   A. T should be much longer than the time constant
   B. T should equal the plant's gain
   C. T should be much shorter than the time constant
   D. T does not matter
   Answer: C. A too-long T makes the loop unstable. (Key point: Choice of T)

## Related in Embedded System

- [PID controller types and applications](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Apid-types-realtime-application) — Embedded System, chapter 7
- [GCD processor algorithm and VHDL](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Avhdl-gcd-processor) — Embedded System, chapter 9, asked 3×
- [LCM processor algorithm and VHDL](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Avhdl-lcm-processor) — Embedded System, chapter 9, asked 2×
- [VHDL architectures; elevator controller](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Avhdl-architectures-elevator) — Embedded System, chapter 9

## Related in other subjects

- [Algorithm versus pseudo-code](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-vs-pseudocode) — Computer Programming, chapter 2
- [Gauss-Jordan pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Agauss-jordan-pseudocode) — Numerical Method, chapter 3, asked 4×
- [Bisection method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Abisection-pseudocode) — Numerical Method, chapter 2, asked 3×
- [Power method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Apower-method-algorithm) — Numerical Method, chapter 3, asked 2×
