Implementing PID Control for Water Level Systems on Arduino
Introduction
PID control is the backbone of most industrial process control systems. Despite its age, it remains the dominant algorithm in production because it is predictable, tunable without a model, and runs on a microcontroller with microseconds to spare. This article documents the exact process used to implement and tune a water level controller on Arduino Uno.
The Plant
The system is a single-tank water level plant: a pump fills the tank, a drain valve creates a load disturbance, and a VL6180X Time-of-Flight LIDAR sensor measures level at 50 ms intervals. The LIDAR was chosen over ultrasonic sensors because its accuracy at short distances (5–100 mm) is dramatically better — standard HC-SR04 sensors have ±3 mm noise on a calm surface, the VL6180X delivers ±1 mm.
System Identification
Before writing control code, spend time characterizing the plant. A step test — turning the pump fully on and recording the level response — yields the process gain, time constant, and dead time needed for initial PID parameters via Ziegler-Nichols or Cohen-Coon tuning rules.
// Simple step test logger
void loop() {
float level = readLidar(); // mm from sensor to water surface
Serial.print(millis());
Serial.print(",");
Serial.println(level);
delay(50);
}
Tuning Results
After three iterations of manual fine-tuning from the Ziegler-Nichols starting point:
- Kp = 2.1, Ki = 0.08, Kd = 0.4
- Steady-state error: < 2.5 mm
- Overshoot: < 2% of full-scale range
- Settling time: < 8 seconds from a 10 cm step
The derivative term was implemented as derivative-on-measurement (not derivative-on-error) to eliminate derivative kick on setpoint changes.