Try Embedded

Water tank level (PI, anti-windup, a drain)

Hold a water column at 150 mm with a 12 V pump and a level sensor: a slow, non-linear plant, a PI loop that must not wind up while it fills, a float switch…

Control · Intermediate

In a real product: Level controller (Industrial): A process skid holds a tank's level while the plant downstream draws more or less water.

A level loop is the classic process-control plant. A 12 V pump fills a clear column through a MOSFET board, and water leaves through a valve at the foot. The pressure sensor at the bottom gives the level, and a float switch near the top is the last line of defence. Open the World tool to watch it fill, settle and fight the drain.

What you'll learn

  • PWM as flow: the pump's speed follows the duty, after a 0.3 s spin-up
  • A non-linear plant: the outflow grows with the square root of the level (Torricelli), so the tank's time constant changes with where it is
  • Why P alone leaves an error here, and the integral that removes it
  • Anti-windup by conditional integration: no integrating while the pump is flat out
  • A hard safety stop (the float switch) that the controller cannot override

How it works

The column is 36 mm across and 250 mm to the rim. While SIG is high the MOSFET puts 12 V across the pump: up to 60 mL/s. With the valve half open the water leaves at 155 · 0.5 · √h mL/s (h in metres), so holding 150 mm takes 30 mL/s, about 58% duty. On its own the tank takes about 10 s to settle there: feedback is what makes it quick.

Every 20 ms the sketch reads LEVEL on A0 (4% of 5 V empty, 94% full), filters it lightly against the sensor's noise and runs Kp·error + integral, clamped to 0–255. From empty the error is large and the pump runs flat out; the integral would fill up during those 4 s and the level would sail past 150 mm. So it integrates only while the output is not pinned the same way the error points. If the float switch on D4 lifts (230 mm), the pump stops whatever the loop says.

The sketch prints setpoint:, level: and duty: ten times a second, and reads kp=, ki= and sp=; a line p alone turns the integral off.

Try this

  • Run with the World tool open: rise, overshoot and settling of the fill. It should settle with almost no overshoot.
  • Send p: the level settles below 150 mm, short by the error that P alone needs to run the pump.
  • Take the anti-windup out (integrate always) and run it again: how far past 150 mm does it go?
  • Open the drain once it has settled: about 15 mL/s more leaves. How far does the level dip, and how fast does it come back?
  • Set sp=60 and then sp=220: the same gains feel different low and high, because the plant is non-linear.
  • Shut the valve to 0%: nothing leaves, so the pump only has to stop. Why does the level still go past the setpoint?

sketch.ino

// Uno holding the water tank's level: a PI loop with conditional integration (anti-windup).
//   Pump MOSFET SIG on D9 (PWM), LEVEL on A0, the FULL float switch on D4; the tank's VCC on 5V and VIN on 12 V.
//   Every 20 ms: read the level, PI towards the setpoint, drive the pump. Every 100 ms it prints
//   "setpoint:<mm> level:<mm> duty:<0..255>" (the World tool's format). Over Serial: "kp=<x>", "ki=<x>", "sp=<mm>";
//   "p" alone runs P only (no integral), to show the steady-state error.

const int PUMP = 9, LEVEL = A0, FULL = 4;
float kp = 13.0;      // duty counts per mm
float ki = 13.0;      // duty counts per mm per second
float setpoint = 150; // mm
bool useI = true;
float integral = 0;
float level = 0;
unsigned long lastLoop = 0, lastPrint = 0;
String line;

float readLevel() {
  // The sensor gives 4% of VCC empty and 94% at the 250 mm rim.
  float share = analogRead(LEVEL) / 1023.0;
  return (share - 0.04) / 0.9 * 250.0;
}

void command(const String &s) {
  if (s.startsWith("kp=")) kp = s.substring(3).toFloat();
  else if (s.startsWith("ki=")) ki = s.substring(3).toFloat();
  else if (s.startsWith("sp=")) setpoint = s.substring(3).toFloat();
  else if (s == "p") { useI = false; integral = 0; }
}

void setup() {
  pinMode(PUMP, OUTPUT);
  pinMode(FULL, INPUT);
  Serial.begin(115200);
  level = readLevel();
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n' || c == '\r') { if (line.length()) command(line); line = ""; }
    else line += c;
  }
  unsigned long now = millis();
  if (now - lastLoop < 20) return;
  lastLoop = now;
  // A light filter on the noisy sensor (about 60 ms).
  level += (readLevel() - level) * 0.3;
  float error = setpoint - level;
  float out = kp * error + integral;
  // Integrate only while the pump is not already flat out the same way: no wind-up while it fills.
  bool saturatedUp = out >= 255 && error > 0, saturatedDown = out <= 0 && error < 0;
  if (useI && !saturatedUp && !saturatedDown) integral += ki * error * 0.02;
  integral = constrain(integral, 0, 255);
  int duty = constrain((int)(kp * error + integral), 0, 255);
  if (digitalRead(FULL)) duty = 0;   // the float switch: never overflow
  analogWrite(PUMP, duty);
  if (now - lastPrint >= 100) {
    lastPrint = now;
    Serial.print("setpoint:"); Serial.print(setpoint, 0);
    Serial.print(" level:"); Serial.print(level, 1);
    Serial.print(" duty:"); Serial.println(duty);
  }
}

Lessons behind it