Calculating the water loss rate in a tank - detecting leaks

Hello everyone!

I’ve got an aquaponics greenhouse. I’ve used the Arduino to automate, control and monitor almost every important aspect to the greenhouse environment. One of my biggest concerns is water leakage. Let me explain. I have an ultrasonic sensor that reports the sump tank level. the level is fairly erratic since there are multiple sources of water returning to the sump. I have grow towers, fish tanks and flood and drain grow beds. I guess the constant flooding and draining of the grow beds causes the most fluctuation in the depth of the sump tank. I’ll attach a graph or two to show the fluctuation.

On occasion, due to various factors, I get a leak where the grow towers spill water out on to the ground. When this happens, the sump level starts a much faster decline. I have a normal decline simply from evaporation; this is expected. The water loss is costly since the water comes from a Reverse Osmosis Filtration system. So, the more water I use, the more frequently filters must be replaced. So, the is a cost associated with excess water loss. Additionally, the rock base in the greenhouse becomes saturated and causes higher humidity levels than are desirable.

So, the goal would be to detect a water leak that is causing the sump tank to drain at a faster rate than would be expected from evaporation.

I’ve not been able to wrap my head around how to accomplish this since the readings are so erratic. I guess I need a way to smooth out the graph and perhaps calculate a running average over an hour period of time or something like that. If I could do that then I could also calculate the rate at which the sump tank level is suffering loss. If I had that information, I could tell what is normal and what is a leak.

So, I guess the big question is how do I even go about accomplishing this task? Any suggestions would be appreciated!

Thank you.


Sump tank 4 hour graph.png

Is there ever a time when your system is not moving water around? If not, could you create one? Would a leak then show up as an unexpected drop in sump level?

So, I guess the big question is how do I even go about accomplishing this task?

I would need to see some pictures of your setup. I would expect the water tank to have some baffles in it, so that dumping water into it didn't cause the fluctuations you are seeing, assuming that the graph is supposed to represent the depth measured over time. Is that even a valid assumption?

It sounds like you need to measure the rate of change of water level in the tank which involves taking readings at fixed intervals and calculating how much the level has changed during the period since the last reading. If the rate of change is larger than expected and stays higher for several successive periods then that will indicate that there is a leak in the system.

If the tank is of a regular shape then you could convert the water level readings into a volume of water lost over a period which might be easier to visualise.

The main problem will be to determine what rate of change and over what period is regarded as excessive.

Since your outgoing and incoming flow are not steady(ie, you may have less incoming flow without this neceserilly meaning spillage), you cannot use these parametes to estimate spillage rate. On the other hand:

On occasion, due to various factors, I get a leak where the grow towers spill water out on to the ground.

if this is the only or the main leak, you just have to “sense” it, how often it happens (…etc) and act accordingly (stop flow on this part, re-program usage …)

The readings from the ultrasonic sensor are accurate. The surface disturbance is negligible so baffles aren’t necessary. Also, I’m not collecting inflow and outflow data, just the tank water depth.

I guess what I am looking for is a way to smooth out the graph/readings so that I can detect a variation in the rate of water loss.

Food for thought: Maybe create an array and keep a running/rolling sample every minute. So the array would contain the last 60 readings over the last hour. Then every so often, maybe 5 minutes, take an average of those readings to get an average tank level of the last hour at 5 minute intervals.

I would think this would “smooth out” the data and give me useful information. Here is a monthly chart. You can see the normal evaporation occurring. Then there is a dip. That was leak that almost emptied the sump tank.

The reason this is a big concern now is I just used the Arduino/OpenHab to implement a auto-filling process two days ago. I get an e-mail when the autofill valve is turned off and on based on specific tank levels. So, at least I’ll see the tank filling and will be aware that something is up. It would be better to extract that information from the tank level data.

I could put a flow meter/sensor on the fill and record how much water the system is using over time, but I’m not sure how that would work since this component is all new.

tank monthly chart.png

create an array and keep a running/rolling sample every …

That is what most people do, myself included.

The averaging period varies, of course, depending on sensor noise and water level disturbances. You may want to consider several, simultaneous running averages.

For example, if you compute and check the average every 10 minutes, nothing will save you from a catastrophic leak that empties the tank in 5 minutes, but checks every 1 minute may be too noisy and unreliable to warn about small leaks.

My system monitors the tanks using a 90 second running average, along with a “change over the last 24 hours” figure, also updated every 90 seconds.

Finally, when you have rare but large noise spikes that throw off running averages, a median filter is extremely useful.

I needed to get my ultrasonic 20cm (9 inches) above the water so it would read.

Take a 4 inch PVC pipe and put your ultrasonic on that.
then put a couple small holes in the bottom and at the top (or open top), then the pipe will show the accurate level and have no surface ripples.

my sensor immediately stabilized when I did that on my rain tank.

That all sounds good. But where can I find example code to accomplish such a task?

On this forum, we generally expect people to write their own code. You can look up "moving average" or "running average" for examples. If you run into problems, ask.

Otherwise, post on the Gigs & Collaborations section and be prepared to pay for the help.

Ah. OK. I'll do my research. Definitely not going to pay.

as a tip, when doing a search use Arduino

arudino moving average

but since you are using C++ you might try that as well.

Sounds like you just need to track the lowest level result and time how long till you get a “new” lower level result. No real need for trying to average reading when what you actually care about is rate of change of the lowest level.

Maybe have the arduino add more water for a certain period of time when a float switch drops, and if the arduino detects the float switch dropped again too soon, turn off the pumps and sound an alarm.

I dont think any average can solve your problem. Thats because what you get after averaging - filtering doesnt "say" when the leak happened. Note that toy may have leak and the depth being static, or going up or going down. The only think it will be revealed is that (after some "time spots") the depth is really lower than the previous lower.
That alone, doent say when and under what conditions the leak happened.

Try to "sense" the leakage by combining the water outlet (vanes open) .
Try some less time (and more frequently) in water supply to the net.

I would think about tracking and graphing the Min and Max values over a time and also show the values as numbers also - you mention a spill from what sounds like a specific area(s) - is it possible to put sensors to detect the spills?

Use a minimum level switch that, when activated, adds a metered volume of water. Keep track of the amount of make up water added over time.

An average over time will give you a quite stable value, e.g. take a measurement every minute, then calculate average of the last 60 minutes every 15 minutes, and store that value. That's your reservoir level. A plot of this will give you a very stable line, if not consider increasing either the measurement frequency (should be several times greater than your cycle times) or periods for averaging.

Now you can calculate the water loss rate over time. You probably know quite well how fast you should lose water through evaporation, and if the slope is steeper than that, have the system send you an alarm.

You may even be able to create a smarter algorithm: something that calculates the water loss rate of say the latest 20 or 100 samples, and compares it to the latest three or four. If the second is more than 10% or 20% greater than the first, it's indicating a leak.