68 PID: Control Theory
68.1 Learning Objectives
By the end of this chapter, you will be able to:
- Derive the PID Equation: Formulate the combined Proportional-Integral-Derivative output for a given control scenario
- Calculate PID Term Contributions: Compute P, I, and D term outputs from error signals, accumulated error, and error rate
- Evaluate System Response: Assess system behavior metrics including overshoot, settling time, and steady-state error
- Select PID Configurations: Justify the choice of P, PI, PD, or full PID control for specific IoT applications
MVU: The Three PID Terms
Core Concept: PID control combines three complementary strategies - P (Proportional) reacts to current error, I (Integral) accumulates past errors to eliminate offset, and D (Derivative) predicts future error to prevent overshoot. Why It Matters: Each term solves a specific problem that the others cannot - P alone leaves steady-state error, adding I eliminates that error but may overshoot, and D provides the damping needed for smooth, stable control. Key Takeaway: Think of PID as past-present-future: I remembers the past, P responds to the present, D anticipates the future - together they achieve what no single term can accomplish alone.
For Beginners: PID: Control Theory
Process control in IoT is about automatically adjusting systems to maintain desired conditions. Think of cruise control in a car: it continuously measures your speed, compares it to your target, and adjusts the throttle to keep you on track. IoT systems use similar feedback loops to control everything from room temperature to industrial manufacturing processes.
Sensor Squad: Sammy Explains PID Control
Hey there, future engineers! I’m Sammy the Sensor, and today I’m going to tell you about something SUPER cool called PID control!
Imagine you’re riding a bike and trying to stay in the center of a path. Here’s what each letter means:
P is for “Present” - It’s like looking at where you ARE right now. If you’re far from the center, you turn the handlebars a lot. If you’re close, just a tiny turn!
I is for “In the past” - It’s like remembering if you’ve been drifting to one side for a while. Even if you’re almost centered now, if you kept going left before, you need a little extra push to the right!
D is for “Direction” - It’s like noticing how FAST you’re moving toward or away from the center. If you’re zooming toward the edge super fast, you need to turn harder BEFORE you go off the path!
Put them all together and you get a super smart helper that: - Sees where you are NOW (P) - Remembers where you’ve BEEN (I) - Predicts where you’re GOING (D)
Real-world example: Your house thermostat uses PID! When it’s cold, it turns the heater on strong. As it gets warmer, it slows down so it doesn’t make your house TOO hot!
Try this at home: Fill a glass exactly to a line using water. Notice how you start fast, then slow down as you get close? That’s YOU doing P and D control!
68.2 Prerequisites
Before diving into this chapter, you should be familiar with:
- Feedback Fundamentals: Understanding feedback concepts and negative feedback
- Open-Loop and Closed-Loop Systems: Comparing control architectures
- Sensor Fundamentals: Sensors for feedback measurement
The Challenge: Simple On/Off Control Wastes Energy and Causes Wear
The Problem: Bang-bang (on/off) control is fundamentally inefficient:
- Overshoot: System blows past the setpoint, then must reverse direction
- Oscillation: Continuous cycling between “too hot” and “too cold” (or fast/slow)
- Actuator Wear: Frequent switching damages relays, compressors, and motors
- Energy Waste: Heating then immediately cooling wastes significant energy
What We Need:
- Smooth approach to setpoint without overshoot
- Fast recovery from disturbances
- Stable operation with no oscillations
- A general solution that works across different system types
The Solution: PID control uses three complementary strategies to achieve optimal control.
68.3 PID Controller Overview
While simple closed-loop systems provide basic feedback, achieving optimal performance often requires more sophisticated control algorithms. The PID controller is the most widely used feedback control algorithm in industrial and IoT applications.
A PID controller uses three different control strategies simultaneously, each addressing different aspects of error correction:
- P (Proportional): Reacts to current error magnitude
- I (Integral): Reacts to accumulated error over time
- D (Derivative): Reacts to rate of error change
PID Controller Block Diagram: Error signal splits into three parallel paths. P term responds to current error, I term accumulates past errors, D term predicts future based on rate of change. All three combine to produce optimal control signal.
68.4 The PID Equation
\[ u(t) = K_p \cdot e(t) + K_i \cdot \int_{0}^{t} e(\tau) \, d\tau + K_d \cdot \frac{de(t)}{dt} \]
Where:
- \(u(t)\) = Controller output at time \(t\)
- \(e(t)\) = Error (Set Point - Process Variable)
- \(K_p\) = Proportional gain
- \(K_i\) = Integral gain
- \(K_d\) = Derivative gain
Putting Numbers to It
Consider a thermostat targeting 22°C with \(K_p = 5\), \(K_i = 0.2\), \(K_d = 1\). At \(t = 10\)s, temperature is 20°C and falling at 0.1°C/s. The PID output is:
\[ \begin{align} e(t) &= 22 - 20 = 2\text{°C} \\ u_P &= 5 \times 2 = 10 \text{ (proportional)} \\ u_I &= 0.2 \times \int_0^{10} e \, dt \approx 0.2 \times 20 = 4 \text{ (accumulated error)} \\ u_D &= 1 \times 0.1 = 0.1 \text{ (temperature falling)} \\ u(t) &= 10 + 4 + 0.1 = 14.1 \end{align} \]
The P term (10) responds to current 2°C error. The I term (4) compensates for past accumulated offset. The D term (0.1) is small because temperature is changing slowly. Total output = 14.1 units drives the heater.
68.5 Understanding Error in PID Control
The foundation of PID control is the error signal:
- Process Variable (PV)
- The actual measured value of what we’re controlling (e.g., current temperature)
- Set Point (SP)
- The desired target value (e.g., target temperature)
- Error (e)
- The difference between desired and actual values
\[ e(t) = SP - PV(t) \]
68.6 Proportional Control (P)
The Proportional term provides control action directly proportional to the current error. Larger errors produce stronger corrections.
\[ u_p(t) = K_p \cdot e(t) \]
Characteristics:
- Fast response: Immediate reaction to errors
- Simple implementation: Single multiplication
- Proportional output: Big errors produce big corrections; small errors produce small corrections
Problems with P-Only Control:
- Steady-State Error: System may never reach exact set point
- Overshoot: High Kp causes system to go past target
- Oscillation: High Kp can cause continuous cycling around set point
68.7 Integral Control (I)
The Integral term accumulates error over time and provides control action based on the total accumulated error. This eliminates steady-state error that P-only control cannot handle.
\[ u_i(t) = K_i \cdot \int_{0}^{t} e(\tau) \, d\tau \]
How Integral Control Works:
- Accumulates error over time
- If error persists (even small error), integral grows
- Integral keeps increasing until error becomes zero
- Also called “automatic reset” - resets bias until error eliminated
Mathematical Intuition:
The integral accumulates (sums) all past errors:
Time | Error | Proportional | Integral Accumulated | Total Output
-----|-------|-------------|---------------------|-------------
0s | +3C | Kp×3 = 1.5 | 0 | 1.5
1s | +2C | Kp×2 = 1.0 | Ki×(3+2) = 0.5 | 1.5
2s | +1C | Kp×1 = 0.5 | Ki×(3+2+1) = 0.6 | 1.1
3s | +1C | Kp×1 = 0.5 | Ki×(3+2+1+1) = 0.7 | 1.2
4s | 0C | Kp×0 = 0 | Ki×(...) = 0.7 | 0.7
5s | 0C | Kp×0 = 0 | Ki×(...) = 0.7 | 0.7
Kp = 0.5, Ki = 0.1
Key Observation: Even when P term drops to zero (error = 0), I term maintains output, compensating for disturbances.
Benefits of Adding Integral:
- Eliminates steady-state error: System reaches exact set point
- Compensates for disturbances: Overcomes constant external forces
- Automatic adjustment: No manual reset needed
68.8 Derivative Control (D)
The Derivative term provides control action based on the rate of change of error. It anticipates future error trends and provides damping to prevent overshoot.
\[ u_d(t) = K_d \cdot \frac{de(t)}{dt} \]
How Derivative Control Works:
- Measures how fast error is changing: de/dt
- If error decreasing rapidly, derivative is negative
- Negative derivative reduces control action (anticipates reaching target)
- Provides damping effect to prevent overshoot
- Acts as predictive brake
Benefits of Adding Derivative:
- Reduces overshoot: Dampens aggressive P and I responses
- Improves stability: Prevents oscillations
- Faster settling: Reaches steady state more quickly
- Better response to changing conditions: Anticipates trends
Cautions with Derivative:
- Noise sensitivity: Amplifies high-frequency noise in sensor readings
- Difficult to tune: Kd selection is challenging
- Rarely used alone: Almost always combined with P and/or I
68.9 PID Configurations Comparison
PID Configuration Performance Comparison: P-only provides fast response but leaves steady-state error. PI eliminates error but may overshoot. Full PID combines fast response, zero error, and minimal overshoot.
| Mode | Usage | Application Examples |
|---|---|---|
| P | Sometimes | Simple temperature control, LED brightness |
| PI | Most common | HVAC systems, level control, pressure regulation |
| PID | Sometimes | Motor speed control, precision positioning |
| PD | Rare | Servo motors, fast response systems |
Alternative View: PID Terms as Time Perspectives
68.10 Knowledge Check
68.11 Common Mistakes and Pitfalls
Pitfall Cards: PID Implementation Mistakes
Mistake 1: Starting with Full PID
- What happens: Beginners often implement full PID immediately, making tuning extremely difficult
- Better approach: Start with P-only, add I if steady-state error persists, add D only if overshoot is problematic
- Why it matters: Each term interacts with others - tuning three parameters simultaneously is much harder than one at a time
Mistake 2: Ignoring Sensor Noise
- What happens: The D term amplifies high-frequency noise, causing erratic actuator behavior
- Better approach: Filter sensor readings before PID calculation, or reduce/eliminate Kd
- Real example: Temperature sensor with 0.5C noise + high Kd = heater rapidly cycling on/off
Mistake 3: No Anti-Windup Protection
- What happens: During large setpoint changes or actuator saturation, integral accumulates to extreme values
- Better approach: Implement integral clamping, stop integrating when output saturates
- Consequence: Massive overshoot when system finally responds
Mistake 4: Using Same Sample Rate for All Terms
- What happens: Fast sample rates make D term unstable; slow rates make P term sluggish
- Better approach: Consider separate update rates or digital filtering for D term
- Typical values: 10-100 Hz for temperature; 1-10 kHz for motor control
68.12 Practical IoT Example: Smart Greenhouse Temperature Control
68.13 Worked Example: PID Tuning for Smart Greenhouse Temperature Control
Worked Example: Tuning P, PI, and PID for a 50m2 Greenhouse With Solar Disturbance
Scenario: A smart greenhouse grows tomatoes requiring 25 degrees C (+/-1 degree C). The greenhouse has a 2 kW heater and a motorized vent. Morning sun causes a +3 degree C/hour disturbance starting at 9 AM. Sensor reads temperature every 5 seconds. The control output ranges from -100% (full vent) to +100% (full heat). Compare P-only, PI, and PID controllers.
Step 1: P-Only Controller (Kp = 4)
| Time | Setpoint | Actual | Error | P Output | What Happens |
|---|---|---|---|---|---|
| 8:00 AM | 25.0 | 22.0 | +3.0 | 12.0% heat | Heater warms greenhouse |
| 8:15 | 25.0 | 23.8 | +1.2 | 4.8% heat | Warming slows as error shrinks |
| 8:30 | 25.0 | 24.5 | +0.5 | 2.0% heat | Steady state: 0.5 degree C below setpoint |
| 9:00 | 25.0 | 24.5 | +0.5 | 2.0% heat | Sun starts – 3 degree C/hr disturbance |
| 9:30 | 25.0 | 25.8 | -0.8 | -3.2% vent | P reacts, but only partially compensates |
| 10:00 | 25.0 | 26.2 | -1.2 | -4.8% vent | Outside +/-1 degree C tolerance |
P-only result: Permanent 0.5 degree C offset (steady-state error). Cannot fully reject solar disturbance. Fails the +/-1 degree C requirement.
Step 2: PI Controller (Kp = 4, Ki = 0.1)
| Time | Error | P Term | I Term (accumulated) | Total Output | What Happens |
|---|---|---|---|---|---|
| 8:00 | +3.0 | 12.0 | 0.3 | 12.3% heat | Similar to P-only initially |
| 8:15 | +1.2 | 4.8 | 3.6 (accumulated) | 8.4% heat | I term keeps pushing even as error shrinks |
| 8:30 | +0.1 | 0.4 | 5.1 | 5.5% heat | Error nearly zero (I eliminated steady-state error) |
| 9:00 | 0.0 | 0.0 | 5.2 | 5.2% heat | Stable at setpoint when sun starts |
| 9:15 | -0.8 | -3.2 | 4.4 | 1.2% heat | I term slow to discharge – overshoot! |
| 9:30 | -1.5 | -6.0 | 2.9 | -3.1% vent | Overshoots to 26.5 degree C before I catches up |
| 10:00 | -0.3 | -1.2 | 1.8 | 0.6% heat | Eventually settles, but took 45 min |
PI result: Eliminates steady-state error (good). But during solar disturbance, I term causes overshoot because it accumulated positive values during heating phase and takes time to discharge. Marginally meets +/-1 degree C but with slow disturbance rejection.
Step 3: Full PID Controller (Kp = 4, Ki = 0.1, Kd = 2.0)
| Time | Error | P | I | D (rate of change) | Total | What Happens |
|---|---|---|---|---|---|---|
| 8:30 | +0.1 | 0.4 | 5.1 | 0.0 | 5.5% heat | Same as PI (steady state, no change) |
| 9:00 | 0.0 | 0.0 | 5.2 | 0.0 | 5.2% heat | Stable |
| 9:05 | -0.2 | -0.8 | 5.0 | -2.4 (temp rising fast!) | 1.8% heat → opens vent | D detects rate of change BEFORE error grows |
| 9:15 | -0.5 | -2.0 | 4.5 | -1.2 | 1.3% vent | D already opened vent 10 min earlier than PI |
| 9:30 | -0.6 | -2.4 | 3.9 | -0.2 | -1.3% vent | Max overshoot: only 0.6 degree C (vs 1.5 for PI) |
| 10:00 | -0.1 | -0.4 | 3.5 | 0.0 | 3.1% heat | Settled within 30 min (vs 45 for PI) |
PID result: D term detects the temperature rising at 9:05 AM (before error exceeds tolerance) and pre-emptively opens the vent. Overshoot reduced from 1.5 degrees C to 0.6 degrees C. Settling time improved from 45 min to 30 min.
Comparison Summary:
| Metric | P-Only | PI | PID |
|---|---|---|---|
| Steady-state error | 0.5 degree C | 0 degree C | 0 degree C |
| Solar disturbance overshoot | 1.2 degree C | 1.5 degree C | 0.6 degree C |
| Settling time after disturbance | Never (oscillates) | 45 min | 30 min |
| Meets +/-1 degree C tolerance? | No | Marginal | Yes |
| Energy savings vs on/off control | 15% | 20% | 23% |
68.14 Interactive: PID Term Calculator
Calculate the P, I, and D term contributions for a given system state.
Key Concepts
- PID Controller: A feedback control algorithm combining proportional (P), integral (I), and derivative (D) terms to drive a process variable (measured output) toward a setpoint by computing a corrective control output
- Transfer Function: A mathematical representation of the input-output relationship of a linear time-invariant system in the Laplace domain, used to analyze stability, frequency response, and design PID controller gains
- Stability: The property of a control system that returns to equilibrium after a disturbance — a stable PID system’s error converges to zero or a bounded steady-state value rather than growing unbounded
- Frequency Response: The analysis of a control system’s behavior across a range of input frequencies, characterizing bandwidth, gain margin, and phase margin that determine stability and performance limits
- Root Locus: A graphical method showing how closed-loop pole locations change as PID gain varies, used to select gain values that achieve desired transient response (overshoot, settling time) while maintaining stability
- Ziegler-Nichols Method: A heuristic PID tuning procedure that determines initial gain values from the system’s ultimate gain (Ku) and ultimate period (Tu) at the stability boundary, providing a starting point for fine-tuning
- Bode Plot: A frequency domain representation showing gain (dB) and phase (degrees) vs. frequency, used to assess gain margin and phase margin of a PID control loop
Common Pitfalls
1. Applying PID to Nonlinear Systems Without Linearization
PID controllers are designed for linear time-invariant systems. Directly applying PID to highly nonlinear systems (pH control, biological processes, aerodynamics) causes performance that varies with operating point. Linearize around the operating point or use gain scheduling for nonlinear plants.
2. Confusing Control Bandwidth with Sampling Rate
Setting the control loop sampling rate equal to the desired control bandwidth. The Nyquist criterion requires sampling at least 2× the control bandwidth — in practice, 5–20× is needed to avoid aliasing and phase lag from sampling. Undersample a control loop and instability results.
3. Not Accounting for Actuator Saturation in Theory
Designing PID gains based on linear theory without considering that real actuators saturate (valves fully open/closed, motors at max speed). During saturation the integral term continues to accumulate (integrator windup), causing overshoot when the actuator exits saturation. Include anti-windup analysis in theoretical design.
4. Using Ideal PID Equations Without Derivative Filtering
Implementing the pure derivative term (D) without a low-pass filter on the derivative path. Pure differentiation amplifies high-frequency noise — a 1 mV sensor noise spike becomes a large control output spike. Always include a first-order filter (time constant = D/5 to D/10) on the derivative term.
68.15 Summary
This chapter covered the theory of PID control:
- PID Equation: Combined P, I, and D terms with their respective gains
- Proportional (P): Responds to current error magnitude - fast but leaves steady-state error
- Integral (I): Accumulates past errors - eliminates steady-state error but can cause windup
- Derivative (D): Predicts future from rate of change - reduces overshoot but sensitive to noise
- Configuration Selection: P for simple systems, PI for most applications, full PID for precision control
68.16 What’s Next
The next chapter covers PID Tuning and Applications, including systematic tuning methods, real-world examples, and practical implementation considerations for IoT systems.
| Previous | Current | Next |
|---|---|---|
| Open & Closed Loop Systems | PID Control Theory | PID Tuning and Applications |
Related Chapters
- Feedback Fundamentals - Foundation concepts
- Open-Loop and Closed-Loop Systems - Control architectures
- PID Tuning and Applications - Practical implementation
- Sensor Fundamentals - Feedback sensors
- Actuators Introduction - Control outputs