Online Circuit Simulator
Water tank level control simulation: pump, pressure sensor, on/off and PI
We typically respond within one business day.
A small 6 V pump fills a tall, narrow tank that drains through a fixed opening at the bottom. A pressure sensor at the bottom tells an ATmega328P the water level, and the program sets the pump speed with PWM through a MOSFET to hold 200 mm, either by switching the pump on and off around a band or with a PI controller. The circuit, the pump, the water and the chip are simulated together.
Recorded runs. Blue: level; orange: pump duty; dashed: 200 mm. In the PI run a second valve opens at 150 s.
What is being simulated?
Water leaves through the opening at a rate that grows with the square root of the level (Torricelli), so the tank is a nonlinear plant: it fills fast when it is nearly empty and slowly near the top. The pump is a DC motor whose load grows with the square of its speed, and its flow is proportional to its speed.
A dh/dt = Q_pump - Cd a sqrt(2 g h)
| Part | Value |
|---|---|
| Tank cross-section A (about 50 mm across) | 0.002 m2 |
| Outlet Cd a | 7.2e-6 m2 |
| Second valve | 1.08e-6 m2 |
| Pump supply | 6 V |
| Pump flow at full speed | 1.05 L/min |
| Level sensor | 0.5 to 4.5 V, 0 to 0.5 m |
With the pump off a level of 200 mm drains away in A sqrt(2 h / g) / (Cd a) = 56 s. With the pump always on the level settles at 299 mm, so 200 mm needs part of the pump: 41 % duty. Near 200 mm a small change of flow settles with a time constant of 2 A h / Q = 56 s.
What does the default program do?
Every 100 ms it reads the sensor and turns the code into millimetres. A switch on D3 picks the controller. On/off: pump at full below 195 mm, off above 205 mm. PI: 8 duty steps per mm of error plus an integral with a 20 s integral time, with anti-windup by tracking.
With PI the level reaches 200 mm within 2 mm by 66 s from empty, without overshoot. When the second valve opens the level dips 5.0 mm and is back within 2 mm 44 s later at 74 % duty. With on/off the level moves between 194.8 and 206.2 mm.
Default program in AVR assembly
Default program in C
Lessons and challenges
- Draining. Turn the pump off at 200 mm and time the tank: check the drain time against the formula above.
- On/off. Close the switch on D3 and measure the swing and the period; then widen the band and watch the pump switch less often.
- PI. Check: within 2 mm of 200 mm by 70 s, no more than 3 mm over.
- Disturbance. The second valve adds 15 % more outflow at 150 s. The controller finds the new duty of 74 % by itself.
- Too small a pump. Make the second valve half the size of the outlet: the pump runs flat out and the level falls to 133 mm, where its full flow equals what drains. No controller can do better.
- Challenge: an overflow alarm. Light the LED on D13 if the level passes 350 mm, and stop the pump.
How were the numbers checked?
The tank model was checked against the exact draining curve, the equilibrium level with the pump at full speed and the small-signal time constant. The on/off run was compared with an ideal on/off model, and the assembly and C programs give the same duty.
Get your project started
We typically respond within one business day.