Coupled Tank Water Level Control on Raspberry Pi 5: Proteus Simulation: the Raspberry Pi 5, the coupled tanks, the ADS1115, the OLED, the knob and the ALARM LED

Coupled Tank Water Level Control on Raspberry Pi 5: Proteus Simulation

1 View
40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Coupled Tank Water Level Control on Raspberry Pi 5 in Proteus Simulation, the sixtieth project of our IoT Projects series. If you have searched for a Raspberry Pi water level controller, a PID level control project, or a Proteus simulation of cascade control, here is a complete one. Two tanks hang one above the other: a pump fills the top tank, the top tank drains into the bottom one, and the bottom one drains away. The Raspberry Pi 5 reads both levels, drives the pump, and holds either tank at the level you set with a knob, while a dashboard on a laptop or a phone shows everything. Then a leak opens in the bottom tank, and the Pi not only keeps the level, it works out how much water is being lost and how big the hole is.

Coupled tanks are the classic process control lab of engineering courses, because they behave like the real thing: chemical reactors, boilers, water towers and treatment plants are all tanks connected by pipes and valves. In this tutorial, we will go through the circuit, see how Torricelli's law gives the flow out of a tank, turn that into a model the Pi can use, design the PI controllers from the model with a little algebra instead of trial and error, see why a textbook PI overshoots and how setpoint weighting fixes it, build a cascade for the bottom tank, and find a leak with a mass balance. Then we run the simulation: a fill, two setpoint steps, the cascade, the leak, and manual control. At the end, you can download the project with the program and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the coupled tank water level control on Raspberry Pi 5 in Proteus simulation:

Coupled Tank Water Level Control on Raspberry Pi 5: Proteus Simulation: the Raspberry Pi 5, the coupled tanks, the ADS1115, the OLED, the knob and the ALARM LED
Figure: The coupled tanks in a Proteus simulation: a leak on tank 2, the ALARM LED on.
Coupled tank Proteus simulation: the Pi holds the levels, a leak, MANUAL
Figure: The working circuit in the simulation: tank 1 at 25 mm, the step to 30 mm, the cascade on tank 2, the leak with the ALARM LED, and MANUAL.

What Does This Level Controller Do?

  • Reads two water levels: each tank has a level sensor (0.5 V empty, 4.5 V at 50 mm); a 16-bit ADS1115 converts them for the Pi, which has no analog inputs of its own.
  • Drives the pump: a PWM signal on the pump's MOSFET sets its average voltage from 0 to 12 V.
  • Holds tank 1: a PI controller with a feed-forward keeps the top tank at the setpoint, 0.5 mm for every percent of the knob.
  • Holds tank 2 with a cascade: an outer PI watches the bottom tank and moves the top tank's setpoint; the inner PI follows.
  • Designs its own gains: every new setpoint, the Pi works out the tanks' model there and the PI gains for the response it wants.
  • Finds a leak: the water balance of the bottom tank shows water lost somewhere; the Pi lights the ALARM LED and estimates the size of the hole.
  • Measures every step: rise time, overshoot, peak and settling time, as a control lab report would.
  • Shows it all: an OLED with the levels and a drawing of the tanks, and a dashboard with MANUAL, TANK 1 and CASCADE and a trend picture of the last 120 seconds.

It is a great project for learning process control the way it is done in industry: models from physics, controller design, feed-forward, anti-windup, bumpless transfer, cascade loops and fault detection, all in plain Python on the Raspberry Pi 5. In the simulation, the tanks' pop-up shows the water, the flows, the model at the current level and the PI gains it recommends, and it measures every level move itself, so you can check the Pi's numbers against an independent judge.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file and the TEPPi runtime that runs Python in Proteus.

The parts of the coupled tank level controller and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, tank_control.py, Ethernet 192.168.1.51
Coupled tanks labCOUPLEDTANKSTEPCoupled Tanks Level Control Lab LibraryU1: PWM from GPIO18, L1 / L2 to the dividers
ADS1115 16-bit ADCADS1115ADVTEPADS1115 ADC Library for ProteusU2, I2C at 0x48: A0, A1 the tanks, A2 the knob
10k potentiometer (the knob)POTROTARY16TEPPotentiometer Library for ProteusRV1, across 3.3 V, the wiper to A2
SSD1306 OLED 128x64OLED12864WHITEADVTEPOLED SSD1306 Library for ProteusOLED1, I2C at 0x3C
Red 5 mm LEDLED5MMREDTEP5mm LED LibraryD1 ALARM, GPIO23 through R5 220 ohm
ResistorsRESISTOR4BANDTEPResistor LibraryR1 / R3 10k, R2 / R4 20k (the dividers), R5 220
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Level Controller: the Circuit

Coupled tank level control Proteus simulation circuit: Raspberry Pi 5, the coupled tanks, ADS1115, OLED, potentiometer and ALARM LED
Figure: The complete circuit of the level controller on an A3 Proteus sheet.
The connections of the level controller
PartPinsRaspberry Pi 5 / supply
U1 the tanksPWM / 12V / 5V / GNDGPIO18 (pin 12) / +12V / +5V / GND
U1 the sensorsL1 / L2R1 10k - A0 - R2 20k to GND; R3 10k - A1 - R4 20k to GND
U2 ADS1115, OLED1SDA / SCLGPIO2 (pin 3) / GPIO3 (pin 5)
U2, OLED1VDD / VCC+3.3V
RV1 the knob1 / W / 3GND / A2 / +3.3V
D1 ALARMA (through R5) / KGPIO23 (pin 16) / GND

The tanks' level sensors give up to 4.5 V, but the ADS1115 runs on 3.3 V here and its inputs must stay below its supply. So each sensor goes through a divider: 10 kohm from the sensor to the input and 20 kohm from the input to ground, which passes 20 / 30 = 2/3 of the voltage. 4.5 V becomes 3.0 V, inside the ADC's 4.096 V range. The Pi multiplies the reading by 1.5 to get the sensor's voltage back. The OLED shares the I2C bus with the ADC. The pump's MOSFET sits on the tanks' base, so GPIO18 drives its gate directly with PWM, and the pump draws its current from the 12 V supply, not from the Pi.

How the Level Controller Works

Water Out of a Hole: Torricelli's Law

Water leaves a hole at the bottom of a tank as fast as if it had fallen from the water's surface: v = sqrt(2 g h). Through a hole of area a, with a discharge coefficient Cd of about 0.62 for a sharp-edged hole, the flow is q = Cd a sqrt(2 g h). Each tank here is 36 x 36 mm inside (1296 mm2) with a 4 mm hole (12.57 mm2). Worked example at 25 mm: sqrt(2 x 9810 x 25) = 700 mm/s, so q = 0.62 x 12.57 x 700 = 5458 mm3/s, about 5.46 mL/s. The pump gives 1.2 mL/s for every volt above 1.5 V, so holding 25 mm needs 1.5 + 5.46 / 1.2 = 6.05 V. That is the feed-forward: the voltage the Pi starts from before the controller adds anything.

A Model the Controller Can Use

The outflow grows with the square root of the level, which is not a straight line, but near any one level it nearly is. Its slope there is q / (2 h): at 25 mm, 5458 / 50 = 109 mm2/s. A tank that loses 109 mm3/s more for every millimetre it rises behaves like a first-order system with a time constant of tau = area / slope = 1296 / 109 = 11.9 s, and a gain of 1200 / 109 = 11.0 mm of level for every volt of pump. At 30 mm the same sums give 13.0 s and 12.0 mm/V: a fuller tank is slower and more sensitive. The Pi recalculates the model at every new setpoint, which is called gain scheduling.

Designing the PI from the Model

A PI controller sets the pump to Kp times the error plus Ki times the error added up over time. With a first-order tank, the closed loop is a second-order system whose characteristic equation is tau s2 + (1 + K Kp) s + K Ki = 0. Matching it to the standard form with the speed and damping we want, wn = 0.6 rad/s and zeta = 0.8, gives the gains directly: Kp = (2 zeta wn tau - 1) / K and Ki = wn2 tau / K. At 25 mm: Kp = (2 x 0.8 x 0.6 x 11.9 - 1) / 11.0 = 0.946 V/mm and Ki = 0.36 x 11.9 / 11.0 = 0.389 V per mm.s. These are exactly the gains the tanks' own pop-up recommends. Notice that Ki = wn2 x area / pump gain, the same at every level; only Kp changes.

Why the Textbook PI Overshoots, and Setpoint Weighting

A damping of 0.8 promises almost no overshoot, yet the first run of this project overshot by 28 % on a 5 mm step. The reason is a zero: a PI controller adds one at s = -Ki / Kp = -0.41, right beside the closed loop's poles at -0.48, and a zero that close turns a gentle step into a jump. The cure is setpoint weighting: the proportional term only sees part of the setpoint, Kp (b x setpoint - level), while the integral still sees the whole error, so the level still ends exactly at the setpoint. With b = 0.5 the zero moves to -0.82, away from the poles. An offline model of this lab predicted 27.3 % overshoot with b = 1 (the run showed 28.2 %) and 3.8 % with b = 0.5; the final run below measured 4.8 %.

The Cascade: Holding the Bottom Tank

The pump only reaches tank 2 through tank 1, so a single controller on tank 2 would be slow and clumsy. A cascade uses two loops: the outer one watches tank 2 and decides what level tank 1 should have; the inner one, already designed, holds tank 1 there. The two holes are the same, so in steady state tank 1 sits at the same level as tank 2, and tank 2's gain from tank 1 is 1 mm per mm. The outer loop is made five times slower than the inner one, wn = 0.12 rad/s with a damping of 1.0, so the inner loop always keeps up: at 25 mm, Kp = 2 x 1.0 x 0.12 x 11.9 - 1 = 1.85 and Ki = 0.0144 x 11.9 = 0.171. Its setpoint weight is 0.7.

Anti-Windup and Bumpless Transfer

When the pump is already at 12 V or at 0 V, adding up more error only stores trouble: the integral keeps growing and the level overshoots badly once the pump comes off its limit. So the integral stops while the output is stuck at a limit in the direction of the error. When the mode changes, for example from TANK 1 to CASCADE, the new loop starts its integral at the value that keeps the pump exactly where it was, so nothing jumps.

Finding a Leak with a Mass Balance

Water is not created or destroyed, so for tank 2: what flows in from tank 1, minus what leaves through its hole, minus what the tank gains, must be zero. The Pi knows all three: q(level 1), q(level 2) and the area times the rate of change of level 2. Whatever is left over is lost somewhere. Worked example from the run: tank 1 at 30.9 mm sends 6.07 mL/s, tank 2 at 23.0 mm lets out 5.23 mL/s through its hole and is rising slowly, so about 0.78 mL/s is unaccounted for. Turned around, Torricelli's law then gives the hole: a = q / (Cd sqrt(2 g h)), and a 1.5 mm hole lets out 0.78 mL/s at 23 mm. The alarm needs more than 0.4 mL/s for 5 seconds, and clears after 10 seconds under 0.2 mL/s.

The Python Code Explained

The program is tank_control.py. It uses lgpio, the GPIO library of Raspberry Pi OS, for the PWM, the LED and the I2C bus, and Python's own http.server and threading for the dashboard. The physics is at the top:

def q_out(h):
    """Water out of a 4 mm hole under h mm of water, mm3/s (Torricelli with the discharge coefficient)."""
    return CD * HOLE * math.sqrt(2 * G * max(h, 0.0))

The model at a level: the time constant, the gain and the voltage that holds it:

    slope = q_out(h) / (2 * h)                                   # d(out)/dh, mm2/s
    return AREA / slope, K_PUMP / slope, V_START + q_out(h) / K_PUMP

The PI gains from the model, for the speed and damping asked for:

    return (2 * zeta * wn * tau - 1) / k, wn * wn * tau / k

The setpoint weights:

B1, B2 = 0.5, 0.7                                                # setpoint weights: the P terms see half (70 %) of a setpoint step

The ADS1115 is started for one conversion of one input against ground, at its 4.096 V range:

        lgpio.i2c_write_i2c_block_data(self.h, 0x01, [0x80 | (0x4 + ch) << 4 | 0x03, 0xE3])   # start, AINx to GND, +-4.096 V, 860 SPS

A reading becomes a level: times 1.5 for the divider, then the sensor's 0.5 V offset and 80 mV per mm:

        l1 = (ads.volts(0) * DIVIDER - 0.5) / 0.08               # mm: 0.5 V + 80 mV per mm

The knob is the setpoint, 0.5 mm a percent, kept between 5 mm and a safe distance from the overflow:

        sp = max(5.0, min(45.0 if mode == 'TANK 1' else 40.0, k * 0.5))

In the cascade, the outer PI turns tank 2's error into a setpoint for tank 1:

            sp1 = sp + kp2 * (B2 * sp - l2) + integ2

The inner loop: the feed-forward, the weighted P term, and the integral with its anti-windup:

            hold = tank1_model(max(sp1, 2.0))[2]                 # the feed-forward: the voltage that holds tank 1 at sp1
            p_term = kp1 * (B1 * sp1 - l1)                       # (the P term on a weighted setpoint, the I term on the whole error)
            if 0.0 < u < V_SUPPLY or (u <= 0 and e1 > 0) or (u >= V_SUPPLY and e1 < 0):
                integ1 += ki1 * e1 * DT                          # (anti-windup: no integrating while the pump is at a limit)

The pump voltage becomes a PWM duty cycle on the MOSFET's gate:

        lgpio.tx_pwm(chip, PUMP, PWM_HZ, round(duty, 1))

The mass balance of tank 2, and the hole that would lose that much water:

        lost = q_out(l1) - q_out(l2) - AREA * rate
                d = math.sqrt(4 * leakf / (math.pi * CD * math.sqrt(2 * G * max(l2, 1.0))))

Switching modes is bumpless: the integral starts where it keeps the pump unchanged:

                    integ1 = volts - hold - kp1 * (B1 * sp - l1)

The control loop runs ten times a second in its own thread, and all the I2C traffic, the ADC and the OLED, stays in that thread. A Step object measures each setpoint step: the rise from 10 to 90 %, the overshoot, the peak, and the settling time inside 5 % of the step, reported once the level has stayed there for 10 seconds. The dashboard's pages are / (the dashboard), /manual, /tank1 and /cascade (the modes), /trend.bmp (a 240 x 120 picture of the levels, the setpoint and the pump, drawn by the Pi) and /data (everything as JSON).

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files and the TEPPi folder (the steps are under the download button).
  2. Open Pi-5-Coupled-Tanks.pdsprj from the Proteus Simulation folder. Keep tank_control.py and the DLL files next to it.
  3. Press Play. The Pi starts in TANK 1 with the knob at 50 %: the pump fills tank 1 to 25 mm and holds it.
  4. Click the tanks (U1) for their pop-up. On the knob's pop-up (RV1), press +10 % or -10 % for a new setpoint.
  5. Open the Laptop or the Mobile WiFi Monitor, press Go, then CASCADE, TANK 1 or MANUAL.
  6. At 110 s the leak valve on tank 2 opens: watch the ALARM LED, the OLED and the dashboard.

Simulation Results: the Coupled Tanks in Proteus

Here is the run that made the screenshots of this tutorial, 151 seconds of simulation: a fill, a setpoint step on tank 1, the cascade, a step on tank 2, a leak, and manual control. The times are the program's own clock, as it printed them on the Pi's terminal; the parts' numbers come from their pop-ups.

Filling Tank 1 to 25 mm

The program starts in TANK 1 with the knob at 50 %, a setpoint of 25 mm, and prints its model: "tau 11.9 s, gain 11.0 mm/V, held by 6.05 V; Kp 0.946 V/mm, Ki 0.3888". The tanks' pop-up shows the same model and the same PI gains. The pump runs flat out until the level comes near, then settles: at 18 s tank 1 is at 25.1 mm with the pump at 6.08 V, just above the 6.05 V the feed-forward predicted, and tank 2 is still filling at 20.1 mm. The ADS1115's pop-up shows the three inputs: 1.67 V from tank 1 through its divider (2.5 V from the sensor), tank 2's, and the knob at 1.65 V.

The Raspberry Pi 5 coupled tank Proteus simulation: tank 1 held at 25 mm, tank 2 filling
Figure: Tank 1 at 25 mm.
Coupled tanks pop-up in the Proteus simulation: HOLDING, tank 1 at 25.1 mm, the model and the PI gains
Figure: The tanks: holding 25 mm.
OLED pop-up in the Proteus simulation: TANK 1 PI, the setpoint, both levels, the pump and the tanks drawn
Figure: The OLED: TANK 1 PI.
ADS1115 pop-up in the Proteus simulation: the two levels through their dividers and the knob
Figure: The ADS1115: three inputs.
Laptop WiFi Monitor in the Proteus simulation: the tank dashboard with the model, the gains and the trend
Figure: The dashboard at 25 mm.

A Step to 30 mm

The knob's +10 % moves it to 60 %, a 30 mm setpoint, at 22.9 s. The Pi redesigns the PI for the new level (tau 13.0 s, 12.0 mm/V, 6.48 V; Kp 0.954) and at 35.6 s reports the step: "tank 1 25.0 to 30.0 mm: rise 2.0 s, overshoot 4.8 %, peak at 4.4 s, settled in 2.7 s". The tanks' pop-up, which sees the real water, measured the same step at 2.1 % and 2.8 s; the Pi's numbers come through the sensor, the divider and the ADC, a tenth of a millimetre apart. For comparison, the first run with a plain PI overshot 28.2 % and took 6.4 s.

The Raspberry Pi 5 coupled tank Proteus simulation: the knob steps tank 1 to 30 mm
Figure: The step to 30 mm.
Coupled tanks pop-up in the Proteus simulation: tank 1 at 30.1 mm, the last move 2.1 % overshoot, 2.8 s
Figure: The tanks: the step measured.
Potentiometer pop-up in the Proteus simulation: the knob at 60 %, 1.98 V on the wiper
Figure: The knob at 60 %.
Laptop dashboard in the Proteus simulation: the 30 mm step, rise 2.0 s, overshoot 4.8 %, settled in 2.7 s
Figure: The dashboard: the step.

The Cascade on Tank 2

CASCADE on the laptop at 39.7 s hands tank 2 to the outer loop at the knob's 30 mm (outer Kp 2.122, Ki 0.1873), and tank 1's setpoint now comes from it. The knob's -10 % at 53.0 s asks for 25 mm in tank 2 (Kp 1.850, Ki 0.1710). The outer loop pulls tank 1 down first so that tank 2 drains, then lets it come back up, and at 75.5 s: "tank 2 29.3 to 25.0 mm: rise 7.9 s, overshoot 2.3 %, settled in 12.5 s". With plain PIs the same step had overshot 24 % and taken 42 s.

Laptop dashboard in the Proteus simulation: CASCADE, tank 2 at 30 mm, the outer PI gains
Figure: The dashboard: CASCADE.
The Raspberry Pi 5 coupled tank Proteus simulation in CASCADE: tank 2 steps to 25 mm
Figure: The cascade: tank 2 to 25 mm.
Coupled tanks pop-up in the Proteus simulation: the cascade moving both tanks to 25 mm
Figure: The tanks: the cascade.
OLED pop-up in the Proteus simulation: CASCADE T2, both tanks at 24.9 mm
Figure: The OLED: CASCADE T2.
Laptop dashboard in the Proteus simulation: the tank 2 step, overshoot 2.3 %, settled in 12.5 s
Figure: The dashboard: the cascade step.

A Leak in Tank 2

At 110 s the leak valve on tank 2 opens. Tank 2 starts to fall and the cascade raises tank 1 to push more water in. At 116.7 s the balance has seen enough: "LEAK on tank 2: 0.78 mL/s lost (a hole of about 1.5 mm at 23.0 mm of water)". The valve in the model is 1.5 mm. The ALARM LED comes on, the OLED shows LEAK! with the lost flow, and the dashboard and the phone show the alarm. The pop-up measures tank 2's dip at 1.98 mm; by the time of the next laptop page, tank 1 is up at 32.4 mm, the pump at 6.74 V, and tank 2 is coming back towards 25 mm with the hole still open.

The Raspberry Pi 5 coupled tank Proteus simulation: a leak on tank 2, the ALARM LED on, tank 1 raised
Figure: The leak: the ALARM LED on.
Coupled tanks pop-up in the Proteus simulation: the leak open, tank 1 filling, the dip measured
Figure: The tanks: the leak.
OLED pop-up in the Proteus simulation: LEAK! and 0.78 mL/s lost
Figure: The OLED: LEAK!
Mobile WiFi Monitor in the Proteus simulation: the coupled tank dashboard with the LEAK alarm
Figure: The phone: the alarm.
Laptop dashboard in the Proteus simulation: the leak ridden out, tank 1 at 32.4 mm, 0.76 mL/s lost
Figure: The dashboard: riding out the leak.

Manual Control

MANUAL on the laptop at 139.1 s turns the knob into the pump: 50 % is 6.00 V. Without the loop, nothing makes up for the leak, and tank 2 sinks to 23.5 mm while tank 1 drifts down too. That is the whole point of closed-loop control. The tanks' pop-up shows the whole run on its graph, and the Pi's terminal has every event.

Laptop dashboard in the Proteus simulation: MANUAL, the pump at 50 %, the leak still open
Figure: The dashboard: MANUAL.
The Raspberry Pi 5 coupled tank Proteus simulation in MANUAL: the knob sets the pump
Figure: MANUAL: the knob is the pump.
OLED pop-up in the Proteus simulation: MANUAL, the knob at 50 %, 6.00 V
Figure: The OLED: MANUAL.
Coupled tanks pop-up in the Proteus simulation: the levels and the pump over the whole run
Figure: The tanks: the whole run.
Raspberry Pi 5 terminal in the Proteus simulation: the models, the gains, the steps and the leak
Figure: The terminal: the whole run.
The results of the coupled tank level control simulation
WhatResult
Model at 25 mmtau 11.9 s, 11.0 mm/V, held by 6.05 V; Kp 0.946, Ki 0.389 (as the pop-up)
Tank 1 step 25 to 30 mmrise 2.0 s, overshoot 4.8 %, settled in 2.7 s (pop-up: 2.1 %, 2.8 s)
Tank 2 step 29.3 to 25 mmrise 7.9 s, overshoot 2.3 %, settled in 12.5 s
Plain PI (the first run)tank 1 overshoot 28.2 %, tank 2 24.1 % and 42.4 s
Leak (1.5 mm, at 110 s)found at 116.7 s: 0.78 mL/s, about 1.5 mm
MANUAL, 6.00 Vtank 2 sank to 23.5 mm
Simulation151 s of simulation

Download the Project Files

Click the button below to download Coupled-Tank-Level-Control-Raspberry-Pi-5-Proteus-Simulation-v1.0.zip (about 14.7 MB, without the source code of the models):

Coupled Tank Water Level Control on Raspberry Pi 5: Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Coupled-Tanks.pdsprj, tank_control.py and copies of the models.
  • Python Code: tank_control.py, the program.
  • Proteus Library Files: the LIB files of the Raspberry Pi 5, the coupled tanks, the ADS1115, the potentiometer, the OLED, the LED, the resistor and the two WiFi Monitors.
  • Proteus Model Files: the DLL models and the TEPPi folder (Python 3.11 and the runtime).
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files and the TEPPi folder from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

tank_control.py is plain Python: edit it next to the project and press Play again; PI1's PROGRAM property names the file it runs. The loop speeds, the dampings, the setpoint weights and the leak thresholds are at the top. Change the tanks' LEAK property to move the leak (0 = never), or LEVEL1 and LEVEL2 to start with water in the tanks. The same program runs on a real Raspberry Pi 5 with Raspberry Pi OS, where lgpio is already installed; enable I2C with raspi-config.

Ideas to Extend the Project

  • Add the derivative term and compare a PID with the PI on the same steps.
  • Tune with a relay test instead of the model: switch the pump between two voltages and read the gains from the oscillation.
  • Use the leak estimate as a feed-forward, so the cascade does not have to discover the leak through the level.
  • Log the levels and the pump to a CSV file and plot the steps afterwards.
  • Send an email or a message when the leak alarm comes on.
  • Hold both tanks at different levels with a second pump on tank 2.

From Simulation to Real Hardware

  • Calibrate the sensors: read them with the tanks empty and full, and put the real offset and slope in the program.
  • Measure the holes: the discharge coefficient of a real orifice is somewhere between 0.6 and 0.65; one step test with the pump off gives it.
  • Keep the electronics dry: real rigs drip; the Pi and the ADC go above the water, never below it.
  • Prime the pump: a pump with air inside does not pump; keep its intake under the reservoir's water.
  • Keep the overflows: a controller at its limit will fill a tank to the brim; the overflow is the last safety.

Troubleshooting

  • The levels read 0 mm: the tanks' 5V pin needs a real 5 V supply; the sensors switch on above 4.3 V.
  • The pump never runs: +12V on the 12V pin, PWM on GPIO18.
  • The levels are 1.5 times off: the dividers must be 10 kohm to the sensor and 20 kohm to ground.
  • The OLED stays blank: SDA / SCL on GPIO2 / GPIO3, address 0x3C, VCC on 3.3 V.
  • A big overshoot: keep the setpoint weights below 1; with 1.0 the PI's zero makes the level jump past the setpoint.

Limitations of the Simulation

  • The tanks, the pump and the water are the model's: Torricelli's law with a fixed discharge coefficient and a 0.3 s pump lag; real water sloshes and real holes clog.
  • The sensors are ideal straight lines with a little noise.
  • The leak opens at a fixed time set in the properties.
  • The network to the laptop and the phone is simulated inside Proteus.
  • Raspberry Pi is a trademark of Raspberry Pi Ltd and the ADS1115 a product of Texas Instruments; this project is not made or endorsed by them or by Labcenter.

So, that was all about the Coupled Tank Water Level Control on Raspberry Pi 5 in Proteus Simulation. I hope it helps you understand how a model from simple physics gives the controller its gains, why a PI needs setpoint weighting, how a cascade holds a tank the pump cannot reach directly, and how a mass balance finds a leak, all tested in a simulation before you build a real rig. More IoT projects with the Raspberry Pi 5 and the ESP32 are coming in this series. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation