Learning Goals 5 min
Every L1–L3 motor control we've done was "open loop" — we told the motor what to do and hoped it complied. Real robots measure what actually happened and adjust. That's closed loop, and it's the whole reason robots can be precise. By the end of this lesson you will be able to:
- Define open-loop vs closed-loop control with one concrete example each.
- Identify the four parts of any feedback loop: setpoint, sensor, controller, actuator.
- Pick which type of loop suits a given build (servo position, robot speed, oven temperature, etc.).
Warm-Up 10 min
No new hardware today. Stand up, eyes closed.
Two ways to walk to a chair
- Open loop: You take 5 steps forward, eyes closed. You probably miss the chair.
- Closed loop: You open your eyes, see the chair, walk toward it, watch how you're getting closer, adjust your direction as you go.
Open loop = command without feedback. Closed loop = measure and correct.
Examples from your L3 builds
- L03-03 servo sweep: command angle, trust it gets there. Open loop (the servo internally is closed-loop, but our sketch doesn't verify).
- L03-10 robot car: drive forward for 2 s, hope you went 1 m. Open loop.
- L03-44 plant monitor: target moisture > 40% → water; reads sensor; reacts when low. Closed loop (sensor + decision + actuator + repeat).
New Concept · The four parts of a loop 25 min
| Part | Job | Example (oven) |
|---|---|---|
| Setpoint | The desired value | 180 °C |
| Sensor | Measures the actual value | Thermocouple — currently 165 °C |
| Controller | Compares setpoint to sensor, decides on action | "Below target — heat more" |
| Actuator | Acts on the world | Heating element |
The loop runs forever: sensor reads → controller decides → actuator acts → physical world changes → sensor reads again.
The error signal
error = setpoint − measurement. Positive = need more action; negative = need less. The controller's job is to drive error to zero.
Three flavours of controller
- Bang-bang: if error > 0, full action; if error < 0, none. Cheap, often jittery. Used in thermostats, water-heaters.
- Proportional only (P): action = Kp × error. Bigger error = bigger action. Quiet but may leave residual error.
- PID: P + integral + derivative terms. The industrial gold standard. We'll meet it tomorrow.
When open loop is fine
- Servo with internal feedback: the SG90's internal pot + control board is closed-loop already; your sketch just commands a target.
- Stepper at slow speed: it advances in known step amounts; missing steps unlikely.
- Short-duration moves: drive a robot forward for 200 ms; close enough.
When closed loop is mandatory
- Self-balancing robots (must continuously correct for gravity).
- Line-following cars (sensor feedback essential).
- Temperature control (heat element overshoots without it).
- Motor speed regulation (load changes cause speed changes).
- Drone altitude / orientation.
Three real loops
| System | Setpoint | Sensor | Controller | Actuator |
|---|---|---|---|---|
| Cruise control | Target speed (70 km/h) | Speedometer | PID | Throttle |
| Drone altitude hold | Target altitude (5 m) | Barometer / ultrasonic | PID | Motor RPM |
| Aircon | Target room temp (22 °C) | Room thermistor | Bang-bang | Compressor on/off |
Trade-offs of closed loop
- Pros: tracking, robust to load variation, can be more precise than the actuator alone.
- Cons: needs a sensor (more cost / complexity / failure points), needs tuning (gains), can oscillate or go unstable if tuned wrong.
Worked Example · Build a bang-bang oven (in code) 20 min
Imagine an oven with a TMP36 in the chamber and a relay-controlled heating element on D7. We want to hold the temperature at 80 °C.
Open-loop version (bad)
// Bad: just run the heater for some fixed duration.
digitalWrite(HEATER, HIGH);
delay(60000); // 1 minute on
digitalWrite(HEATER, LOW);Problem: we have no idea what temperature actually reached. With ice-cold air it's undershoot; with hot ambient it's overshoot.
Closed-loop version (bang-bang)
const float SETPOINT = 80.0;
const float HYSTERESIS = 2.0; // ±2 °C dead band
void loop() {
float temp = readTemp();
static bool heating = false;
if (temp < SETPOINT - HYSTERESIS) heating = true;
if (temp > SETPOINT + HYSTERESIS) heating = false;
digitalWrite(HEATER, heating ? HIGH : LOW);
delay(500);
}Four parts: setpoint = 80, sensor = readTemp(), controller = the if logic, actuator = digitalWrite. The hysteresis (±2 °C dead band) prevents rapid on/off cycling at the setpoint.
Trace the loop
- Oven at 22 °C. Heater on (22 < 78). Temp climbs.
- Reaches 78 °C → no change yet (still < 78 condition false). 80 °C → still heating. 82 °C → heater off.
- Temp drifts down. 80 °C → no change. 78 °C → still off. 77.9 °C → heater on.
- Cycle continues, holding 78–82 °C.
Average ~80 °C without ever overshooting badly. The first true control loop. Tomorrow we'll make it smoother with PID.
Try It Yourself · Paper exercises 15 min
Identify open vs closed loop:
- (a) Microwave oven: you set 1:30, it heats for 90 s.
- (b) Modern oven with thermostat: you set 180 °C; it cycles on/off.
- (c) A toaster: you set the "darkness" dial; pops when timer runs out.
Reveal
(a) Open — fixed time, no feedback. (b) Closed — temperature thermostat. (c) Open — runs a fixed duration. Modern toasters with "crumb-darkness sensors" are closed-loop, but classic ones aren't.
Name the four parts of the loop for: cruise control on a car.
Reveal
Setpoint: target speed (70 km/h). Sensor: speedometer. Controller: PID inside the ECU. Actuator: throttle valve.
Your robot drives forward for 1 second and ends up 80 cm in one room and 110 cm in another. Why? How would closed-loop fix it?
Reveal
Battery voltage, tyre slip, surface friction vary. Closed loop: add wheel encoders (count rotations), drive until you've gone 100 cm worth of rotations regardless of voltage / slip. We'll meet encoders in L04-27.
You add a closed-loop controller and the system oscillates wildly. Two likely causes?
Reveal
(1) Sensor sign wrong (positive feedback). Negate the error. (2) Gain too high — drives past setpoint, overcorrects, overshoots the other way. Lower Kp. (3) Loop frequency too slow — sensor reads too rarely. Speed up the loop. Any of the three can do it.
Mini-Challenge · Identify the loops in your L3 builds 10 min
For each L3 project, identify whether it's open or closed loop and which parts:
- L03-04 Indicator Needle (servo).
- L03-10 Two-Wheel Test Bed (chassis).
- L03-43 BT Car v2 with obstacle stop.
- L03-44 Smart Plant Monitor.
Reveal
- L03-04: open loop from your sketch's view (you write angle). Servo internal: closed loop (pot + control board).
- L03-10: open loop. No feedback on whether wheels actually moved.
- L03-43: partial closed loop — obstacle sensor adds a safety feedback path ("don't drive forward if obstacle").
- L03-44: closed loop. Sensor reads moisture, threshold logic decides on alert.
Recap 5 min
Open loop = act without measuring. Closed loop = measure → decide → act → repeat. Four parts: setpoint, sensor, controller, actuator. Bang-bang controller = simplest. Most L3 builds are open loop (or partial); pro robotics is closed-loop everywhere. Tomorrow we meet the PID algorithm — the smoother closed-loop controller used everywhere.
- Open-loop control
- Command without feedback. Simple, fast, but can't correct for disturbances.
- Closed-loop control
- Measure → decide → act → repeat. Robust, precise, but needs a sensor and tuning.
- Setpoint
- The target value the system tries to achieve.
- Process variable / measurement
- The actual value the sensor reports.
- Error
- setpoint − measurement. The signal the controller acts on.
- Controller
- The decision-making code: maps error to action. Bang-bang, P, PI, PID.
- Actuator
- The thing that physically changes the world (motor, heater, valve, servo).
- Hysteresis / dead band
- A gap around the setpoint where the controller doesn't react. Prevents rapid on/off cycling.
- Positive vs negative feedback
- Negative = error drives the system back toward setpoint (stable). Positive = error grows (unstable, runs away). Closed-loop controllers must be NEGATIVE feedback.
- Loop period
- How often the loop runs. Too slow = sluggish. Too fast = noisy / CPU-heavy. Match to the physical time constants.
Homework 5 min
- Identify 3 closed-loop systems in your daily life (other than the examples in §3).
- For each, name the four parts.
- Read ahead to ARD-L04-24 (The PID Algorithm).