Engineering · Lesson 03
PID Control & Feedback Loops
Feedback controllers use a simple idea: measure the error, then correct it — proportionally, by its accumulation, and by its rate of change. Investigate one controller term at a time, then use your evidence to hold a virtual drone at its target altitude.
Your engineering mission
Hold altitude at 6 m. Explain why pushing harder, remembering error, and slowing motion solve different problems.
Route: Understand feedback → meet PID → predict → tune → test. Allow 35–45 minutes; work with a partner and predict before each run.
Three tools
- P — Proportional: correction scales with current error
- I — Integral: corrects accumulated past error (a lasting gap)
- D — Derivative: dampens based on rate of change (overshoot)
- Together they form a widely used feedback controller
Before you fly · Understand the problem
How can a machine keep something steady?
Imagine steering a ship toward a lighthouse. You turn the wheel, but the ship takes time to turn. If you wait until it points exactly at the lighthouse before easing the rudder, it may keep turning past the heading you wanted. Wind can also keep pushing it to one side. Good steering means watching both where you are and how you are moving, then correcting again and again.
A drone faces the same kind of problem vertically. Its motors push upward, gravity pulls downward, and its momentum keeps it moving. One motor command cannot guarantee a steady height. A controller needs to measure what happened and adjust its next command.
Open loop: give a command
“Run the motors at this power.” The command does not change in response to measured altitude. If the payload changes, the drone may rise or fall.
Closed loop: check and correct
“Keep measuring altitude and adjust power to hold 6 m.” Feeding the measurement back into the next decision creates a feedback loop.
Follow one trip around the loop
- Setpoint: the value we want — here, an altitude of 6 m.
- Sensor measurement: the value we observe — suppose the drone is at 3 m.
- Error: target minus measurement — 6 − 3 = +3 m.
- Controller: the rule that turns that error and its history into a motor command.
- Actuator and system: motors change thrust; the drone moves; the next measurement starts another trip around the loop.
Think first: If the drone reaches 7 m, the error is −1 m. Should the controller increase or reduce upward thrust? Reduce it — but reducing thrust does not instantly stop a rising drone.
Where the idea came from
From ship steering to programmable machines
PID grew out of a practical engineering question: how can a machine make the repeated corrections a skilled human operator makes?
- Early 1900s · automatic steering. Gyrocompasses and powered steering helped make ship autopilots practical. Elmer Sperry filed a gyropilot patent in 1914. Measuring heading was only part of the challenge; the rudder also needed well-timed corrections.
- 1922 · a mathematical explanation. Nicolas Minorsky published an analysis of automatic ship steering that discussed the three-term controller we now call PID. His work addressed how a ship’s inertia and persistent disturbances affect stable steering. Trials on the battleship New Mexico followed in 1923.
- 1930s onward · adjustable industrial controllers. Instrument companies developed general-purpose three-term controllers for industrial processes. Engineers could adjust each action to suit the machine instead of relying on one fixed response.
The lesson’s drone replaces the ship, altitude replaces heading, and motor thrust replaces rudder action. The shared problem is keeping a moving system near a target despite disturbances.
History source: S. Bennett, “Nicolas Minorsky and the Automatic Steering of Ships” (IEEE Control Systems Magazine, 1984; PDF).
Meet the algorithm
PID asks three different questions
An algorithm is a repeatable set of instructions. PID combines three instructions into one output. Its gains — Kp, Ki, and Kd — control how strongly each term contributes. Tuning means choosing those strengths for the system you are controlling.
P · How far away am I now?
Proportional means the correction scales with present error. Twice the error gives twice the P correction at the same gain. Strong P can move quickly, but momentum can carry the drone past its target.
I · Has a gap persisted?
Integral means accumulating error over time. A small error that lasts keeps building a correction. This can supply the extra thrust needed to close a lasting gap, but stored correction can also cause overshoot.
D · How fast am I moving?
Derivative uses a rate of change. In this simulator, D opposes measured vertical velocity: rising motion reduces its thrust contribution; falling motion increases it. It helps damp motion rather than waiting for a large error.
A worked controller decision
Suppose the target is 6 m, measured altitude is 5 m, accumulated error is 2 m·s, and the drone is rising at 0.5 m/s. With Kp = 3, Ki = 0.5, and Kd = 2:
- P: 3 × (6 − 5) = +3.
- I: 0.5 × 2 = +1.
- D: −2 × 0.5 = −1.
- Requested output: 3 + 1 − 1 = 3 in the model’s thrust units, then limited to the motor range of 0–12.
These numbers request thrust, not a new altitude. The drone still has to move under gravity and air resistance before the next measurement. The gains carry the unit conversions; their numerical values are specific to this model.
Why D uses motion here: A textbook PID often differentiates error. For a fixed target, error changes at minus the measured velocity. Using measured velocity also avoids a sudden derivative spike when you move the target.
Preflight check
Predict a correction before you pilot it
The drone is below the target but rising quickly. P asks for more thrust. What should D contribute?
Choose a prediction, then connect it to the drone’s motion.
Ready to experiment: You should be able to identify the target, measurement, error, and motor output, and explain P as present error, I as accumulated error, and D as rate of change. The simulator starts paused so you can read its controls before running a trial.
1. Measure, predict, then fly
Target − measured altitude = error. At the start, 6 − 3 = +3 m. Positive error asks for more upward thrust; negative error asks for less. The drone keeps moving while the controller updates its correction.
This simplified vertical model includes gravity, limited thrust, and air resistance. Gains belong to this model; they are not settings for a real drone. There is no automatic hover thrust: the controller must supply it.
Paused at 3 m. Choose Resume or load a guided experiment to start.
Altitude: 3.00 m · Error: +3.00 m · Time: 0.0 s
Drag the red target line or use the target slider. Restart between comparisons so altitude, velocity, elapsed time, and accumulated error start the same.
Read the response
Blue = target · Orange = measured altitude · Shaded gap = error · Horizontal window = last 10 seconds; vertical range = 0–10 m. Overshoot is how far the drone rises above the target.
Proportional (P)
The P term produces an output proportional to the current error. High Kp responds aggressively but can overshoot. In this model, P alone needs a remaining error to supply thrust against gravity; larger Kp reduces that steady-state error.
Integral (I)
The I term sums all past error over time. It eliminates steady-state error that P alone cannot fix, but too much Ki causes oscillation and "integral windup."
Derivative (D)
The D term reacts to the rate of change of the error. It acts as a brake, smoothing out overshoot and damping oscillations. Differentiating noisy measurements can amplify sensor noise; this simulation uses ideal measurements.
Tuning in the Real World
Engineers use methods like Ziegler–Nichols to find good starting gains. The goal: reach the setpoint quickly, with minimal overshoot and no sustained oscillation. Explore the sliders above to feel this balance.
2–5. Build your controller with evidence
For each experiment: predict → load the setup → observe for 15 seconds → explain. Change one gain at a time and restart. Do not change the target during a comparison.
2. P: respond to the present
Predict what happens with no feedback, then compare Kp = 1 with Kp = 3 (Ki = Kd = 0). Which rises faster? Which overshoots more? Why can a drone stop below its target even while the controller is working?
Explain: At rest, thrust must balance gravity. P needs a positive error to produce that thrust. With Kp = 1, expect a remaining error near 1.5 m after oscillations decay.
3. D: slow the approach
Keep Kp = 3 and Ki = 0; add Kd = 2. Predict how the peak height changes. Compare with the stronger-P run. Does less overshoot also remove the final gap?
Explain: Here D uses minus the measured vertical velocity. Rising motion reduces thrust; falling motion increases it. At rest D is zero, so it cannot supply the missing hover thrust.
4. I: remove a lasting gap
Keep Kp = 3 and Kd = 2; add Ki = 0.5. Watch the final gap shrink over 15–25 seconds. Then try Ki = 1.5 from the same start. Does accumulating correction faster improve every part of the response?
Explain: A small positive error builds up the I term until thrust can balance gravity at the target. Too much accumulation can keep pushing after the target is crossed. This model caps the error integral at ±8 m·s; real systems need appropriate anti-windup logic.
5. Disturbance challenge: recover and justify
Choose your own gains. Restart at 6 m target and let the drone settle, then apply one downward gust. Aim to return within ±0.2 m of the target and stay there for 3 seconds. The live hold timer resets when you leave that band. Compare two gain sets using the same gust; report recovery time and peak overshoot rather than claiming one setting is always best.
Transfer: Change the target to 8 m. Does the same tuning still work? Explain what you would retest if the payload or motor changed.
6. Make your tuning recommendation
Record on paper or in the fields below. These notes stay only on this page until you reload; print or copy them to turn in.
Arduino connection: Each loop reads a sensor, computes error, accumulates error × elapsed seconds, estimates motion, and limits motor output. A fixed update interval, consistent units, sensor filtering, and output limits matter. A motor cannot produce unlimited thrust just because the formula asks for it.
Where PID fits in a robot
PID holds a target, but it needs two things from the rest of the robot: a trustworthy measurement and a sensible target. A drone's angle comes from fusing a drifting gyro with a noisy accelerometer. Try it in Sensor Fusion. The target speeds and headings come from a planner. See Motion Planning, where the wheel speeds a planner asks for are exactly what a PID loop on each motor has to deliver.