Open loop vs. closed loop
Think about a toaster. You push the lever, and it heats for two minutes. It never looks at the bread. If the bread was frozen, you get pale toast. If it was thin, you get charcoal. The toaster does its job and hopes for the best. Engineers call this open-loop control: the machine acts, but it never checks the result.
Now think about the thermostat on a wall. You set it to 68 °F. It measures the room, sees that it is 64 °F, and turns on the heat. A few minutes later it measures again. Once the room is warm enough, it turns the heat off. It keeps checking all day. This is closed-loop control, also called feedback control. The result of the action is fed back in to decide the next action, so the information travels in a loop.
Open loop is fine when nothing surprising happens. Robots almost never get that luxury. Wind pushes drones. Floors tilt. Batteries run low. A robot that wants to stay balanced has to keep measuring and keep correcting. Here are some feedback loops you already know:
| System | Goal | What it measures | What it adjusts |
|---|---|---|---|
| Cruise control | Stay at 65 mph | Wheel speed | Gas pedal (throttle) |
| Self-balancing scooter | Stay upright | Tilt angle, from a gyroscope | Wheel motors |
| Drone hovering | Stay at a set height | Height, from a pressure or distance sensor | Propeller speed |
| You, on one foot | Do not fall over | Balance, from your inner ear and eyes | Ankle and leg muscles |
The feedback loop, block by block
Every feedback system has the same five parts. Engineers draw them as boxes with arrows, called a block diagram.
- Setpoint: the goal. A height, a speed, an angle, or a temperature.
- Sensor: a part that measures what is really happening right now. Its reading is called the measurement.
- Error: how far off you are. It is the goal minus the measurement.
- Controller: the "brain," usually a small computer called a microcontroller. It turns the error into a command.
- Actuator: the part that does the pushing, such as a motor, a heater, or a valve.
Step through the loop below. Pick a system, then press Next to watch one trip around the loop.
Lab 1 · One trip around the loop
Goal
After step 6 the loop starts again with a fresh measurement. Real controllers run this loop fast: a thermostat checks every few seconds, while a drone's flight controller runs it hundreds or even thousands of times every second.
On/off control, and why it wobbles
The simplest controller has only two choices: full power or no power. Below the goal? Motor on. Above the goal? Motor off. Engineers call this bang-bang control because the actuator slams from one extreme to the other. Before we explain why it wobbles, try being one.
Lab 2 · You are the controller
Ready. 20 s roundYour drone's motors are either on or off. Hold the button (or focus it and hold Space) to fire the motors. Keep the green line inside the orange band. The goal moves twice, and there is a little wind.
Hard, right? You probably saw the line climb past the band, then drop below it, then climb again. That happens for two reasons:
- Momentum. When you let go at exactly the right height, the drone is still moving up. It coasts past the goal.
- Delay. You notice the drone is too low, then you react, then the motors spin up. By then it has already fallen further.
A bang-bang controller has the same problem. It only ever pushes at full strength, so it always overshoots a little, then overcorrects, then overshoots again. Your home thermostat works this way, and it is fine for a living room. It is not fine for a drone carrying a camera, or a robot that needs to stand still. Try the heating lab below. First run it as an open-loop timer, then as a thermostat, and open the window in each mode.
Lab 3 · Heating a room
Two things to notice. In timer mode, opening the window makes the room drift colder and colder, because the timer never checks. In thermostat mode, the heater fights back. But even with a band of 0 °F, the temperature still swings past the goal, because the heater stays warm for a while after it turns off. Make the band wider and the heater clicks on and off less often, which saves wear on the parts, but the swings get bigger. That is a trade-off, and on/off control can never fully escape it.
P, I, and D: three ways to push
What if the controller could push a little or a lot instead of only on or off? That is the idea behind the PID controller, the most common controller in the world. PID stands for Proportional, Integral, Derivative. Those are math words, but each one has a simple meaning. The controller works out three separate pushes and adds them together.
Proportional
"How far off am I right now?"
Push harder the farther you are from the goal. Twice the error means twice the push. Close to the goal, the push gets gentle.
- Too much P: flies past the goal and wobbles back and forth (overshoot and oscillation).
- Too little P: slow and lazy. It can also stop short of the goal and stay there. That leftover gap is called steady-state error.
Integral
"How long have I been off?"
Keep a running total of the error over time. If the drone hovers a little too low for a while, the total grows and the push slowly gets stronger until the gap is gone. This fixes problems that never go away, like gravity.
- Too much I: the total builds up while the drone is far away, then keeps pushing after it arrives, causing overshoot. This is called integral windup.
Derivative
"How fast is the error changing?"
Watch how quickly you are closing in. If you are rushing toward the goal, D pushes the other way, like a brake or a shock absorber. It smooths out the wobble that P causes.
- Downside: D reacts to quick changes, so it also reacts to sensor noise, the tiny random jitter in every real measurement. Too much D makes motors twitch.
Each push gets its own gain, a number that works like a volume knob for that part. The gains are written Kp, Ki, and Kd. The controller's output is:
Everyday version. Picture parking a car at a stop line. P: the farther the line, the more gas. D: as you get close and you are still moving fast, ease off and brake. I: if the car keeps stopping a foot short, add a bit more gas each time until it lands right on the line.
PID tuning lab: make a drone hover
This drone starts on the ground and wants to hover at the goal height. Gravity pulls it down the whole time, so the motors must keep pushing just to stay level. Pick a preset to see a common mistake, then tune the three gains yourself. The scope shows height in green, the goal as a dashed orange line, and motor thrust as a cyan line at the bottom.
Lab 4 · Drone altitude hold
t = 0.0 sPresets
Measuring the climb...
Overshoot is how far the drone flies past the goal, as a percent of the climb. Settling time is how long it takes to get within 5% of the goal and stay there. Leftover gap is the steady-state error once things calm down.
How to tune by hand
Engineers have fancy math for tuning, but many robotics teams tune by hand with this recipe. Try it in the lab, changing one slider at a time.
- Set Ki and Kd to zero. Start with a small Kp.
- Raise Kp until the drone reacts quickly and just starts to wobble. Notice that it hovers below the goal: P alone cannot hold up against gravity without some error.
- Add Kd until the wobble calms down and the climb looks smooth.
- Add a little Ki until the leftover gap disappears. If it starts overshooting again, you added too much.
- Press Wind gust and turn on Noisy sensor. A good tune recovers quickly from the gust without making the thrust line jitter wildly.
Challenge: get overshoot under 10%, settling time under 3 seconds, and a leftover gap under 0.05 m. Then check that the drone still recovers from a wind gust.
PID in code, and in real robots
A PID controller is surprisingly short. Here is a tiny Python program that simulates a drone climbing to 5 meters. Each trip through the for loop is one trip around the feedback loop: measure, find the error, compute three pushes, and set the motors.
goal = 5.0 # target height in meters
height, speed = 0.0, 0.0
Kp, Ki, Kd = 8.0, 3.0, 4.0
error_sum = 0.0
last_error = goal - height
dt = 0.05 # check 20 times a second
for step in range(201):
error = goal - height # P: how far off are we?
error_sum += error * dt # I: add up past error
change = (error - last_error) / dt # D: how fast is error changing?
last_error = error
thrust = Kp * error + Ki * error_sum + Kd * change
thrust = max(0, min(thrust, 30)) # motors have limits
speed += (thrust - 9.8) * dt # gravity pulls down
height = max(0, height + speed * dt)
if step % 20 == 0:
print(f"t = {step * dt:4.1f} s height = {height:4.2f} m")t = 0.0 s height = 0.05 m t = 1.0 s height = 4.39 m t = 2.0 s height = 5.05 m t = 3.0 s height = 4.96 m t = 4.0 s height = 4.97 m t = 5.0 s height = 4.98 m t = 6.0 s height = 4.99 m t = 7.0 s height = 4.99 m t = 8.0 s height = 5.00 m t = 9.0 s height = 5.00 m t = 10.0 s height = 5.00 m
The output shows the drone climbing fast, passing 5 m by just a few centimeters around the 2 second mark, then settling right on the goal. Notice the line thrust = max(0, min(thrust, 30)). Real motors cannot spin infinitely fast or push backward, so every real controller clamps its output.
Where you will find PID
- Line-following robotsThe error is how far the line is from the center sensor. P steers back, D stops the zig-zag. See the line follower in What Is a Robot?
- DronesFlight controllers run several PID loops at once: one each for tilt forward, tilt sideways, spin, and height. Learn more in How Do Drones Fly?
- Balancing robots and scootersThe goal is a tilt of zero degrees. If the body leans forward, the wheels roll forward to get back underneath it.
- 3D printer hotendsThe nozzle must stay near 200 °C while cold plastic flows through it. Many printers run a "PID autotune" to pick the gains. See How 3D Printers Work.
- Robot armsEach joint has a motor and an angle sensor. A PID loop moves the joint to the target angle without slamming into it.
- Cruise controlThe goal is a speed. I is what keeps the car at 65 mph on a long hill instead of slowly losing speed.
Frequently asked questions
What is PID control?
PID control is a way for a machine to reach and hold a goal, like a height, speed, angle, or temperature. Many times a second it measures how far it is from the goal (the error) and adds up three parts: P pushes in proportion to the error, I adds up past error to close any leftover gap, and D reacts to how fast the error is changing to brake before overshooting.
What is the difference between open-loop and closed-loop control?
Open-loop control does an action without checking the result, like a toaster that heats for two minutes no matter how dark the bread gets. Closed-loop control, also called feedback control, measures the result and keeps adjusting, like a thermostat that checks the room temperature and turns the heater on or off.
What does PID stand for?
PID stands for Proportional, Integral, Derivative. Proportional means the push matches the size of the error. Integral means adding up error over time. Derivative means the rate of change, or how fast the error is growing or shrinking.
How do you tune a PID controller?
A simple hand-tuning method is: set I and D to zero, raise P until the system reacts quickly and starts to wobble, add D until the wobble calms down, then add a small amount of I to remove any leftover gap. Test with a bump or disturbance after each change, and change one number at a time.
Why does my robot overshoot and wobble back and forth?
Usually the P gain is too high or there is not enough D. The robot pushes hard toward the goal, builds up speed, and cannot stop in time, so it flies past and has to come back. Lower P a little or add D, which acts like a brake as the robot gets close. Too much I can also cause overshoot.
What is bang-bang control?
Bang-bang control, also called on/off control, has only two choices: full power or no power. A home thermostat is a common example. It is simple and cheap, but the value always swings above and below the goal because the system keeps moving after the switch flips.
Quick quiz
1. Which of these is open-loop control?
2. The goal is 70 °F and the sensor reads 64 °F. What is the error?
3. A drone with only P control hovers a little below its goal and stays there. Which part fixes that leftover gap?
4. Your balancing robot overshoots and rocks back and forth. What should you try first?
5. Why can too much D make a robot's motors twitch?
Keep learning
You just learned the idea that keeps drones in the air and robots on their feet. Next, see the robots, chips, and machines that use it.