Hauke’s Projects

Playin' around with Electronics and Computers

Lessons Learned: Measure Water Levels in a Barrel with SR04-Type Sensors

Using ultrasonic distance sensors I monitor water levels for my garden irrigation system. I have an underground rainwater cistern and a wooden barrel as an interim water storage in the sun to have the water warmed up before use. I started off with the classic HC-SR04 ultrasonic distance sensor, but it turned out to be a bad idea for the warm water barrel: Moisture and temperatures up to 40°C in the summer sun made the sensor rot within half a year down to complete failure. I switched to AJ-SR04M watertight sensor (which seems to be very similar to JSN-SR04T which is often also mentioned on the internet). This has a higher minimum distance (~20 cm vs. ~2 cm), and a much larger opening angle (45° to 75° vs. 15°) as compared to the HC-SR04, and in this post I describe how I dealt with that.

The Situation

My underground rainwater cistern collects up to 3 m³ of rainwater, and I use this for watering the plants in my garden and on my balcony. Being underground, the water from the cistern is always pretty cold, and watering your vegetables with it slows down their growth, some plants (e.g. cucumbers) even developing a bitter taste. It is recommended to use warm water in “agricultural” gardening. So we got an old wine barrel and put it on top of our garage, where it sits in the sun most of the day. With an ATmega328P controlled magnet valve I fill the barrel up from the cistern. In order to control the valve, I need to monitor the water level, to start filling the barrel before it gets empty, and stop the fill before it overflows.

Reading a bit through the internet forums, many users claim that the classic HC-SR04 ultrasonic distance sensor does a good job, and survives the humid ambience for years. Its rather cheap, so I gave it a try. And it worked to my utter satisfaction in the beginning, giving robust measurements. But soon the measurements were a bit off on warm days, and it turns out that condensating water altered the characteristics of the sensors so that the reported distances were too large. At some point, the measuremnts were totally off altogether, and never returned to normal. I finally, after about half a year and a very hot summer, dismounted the sensor, and it was rotten and rusty! So I suppose it lasts for years in a cool environment, but up in the sun this is what you get very quickly:

Corroded sensor

The Watertight Solution

A bit more expensive, but watertight, are the sensors JSN-SR04T and AJ-SR04M (Here’s an in-depth comparison of the different sensor types, also with instructions how to access it from a microcontroller). Another advantage is that the sensor is detached from the control PCB, so it is easier to place the electronics away from moisture. And finally (not relevant for me), it has more operation modes, with onboard automatic measurement and transfer via UART. Still, since my whole code was already written for the “low level” communication with HC-SR04, I decided to stick with that mode, which the sensor is capable of also.

On the downside, the sensor has a larger minimum distance it can sense, the effective lower limit being at about 18-19 cm. The HC-SR04 works nice down to even only 2 cm. And with the barrel being ~60 cm in effective usable height, losing ~20 cm is considerable.

It seems that the watertight modules come with different kinds of ultrasonic converters, on a random basis. From the look they recycle sensors originally made for cars, and take what they get. Depending on which sensor you actually get, its opening angle is anything between 45° to 75° – much broader than the 15° of HC-SR04, which in a round barrel will not be helpful.

Here’s how my version looks like – you can spot the asymmetric outer shell which hints on being designed to sit in a car bumber:

The ultrasonic converter

Overcoming the Disadvantages

My first thought was to use a standard PVC tube to mount the sensor ~10-15 cm above the barrel. Turns out that the PVC tube reflects already so much sound, that I ended up always measuring the minimum distance. Bummer! So I took a piece of paper, rolled it into a narrow cone and used this with the sensor on the smaller side of the cone: Worked! Just a bit of an angle is enough to divert the sound so that only the desired signal reaches back to the sensor. Nice side effect: The opening angle gets very well constrained along!

Final Solution

I fired up my 3D workflow and designed and printed a cone mount for the sensor.

Dismantle the Sensor

I found that you can easily dismantle the sensor, which consists of an outer mounting shell, an elastic decoupling wrapper and the actual ultrasonic converter, which is much smaller than the whole contraption:

Sensor disassembled
The disassembled sensor

I decided to take away the outer shell and design my final mount to have the sensor plus the elastic wrapping sitting in it.

The Cone Mount

The cone itself is quite narrow – here are its dimensions:

Cone dimensions
Cone dimensions

The resulting opening angle of the cone is ~10°.

And here is the part the sensor sits in:

The cone from above

When testing this, I got again always the minimum distance, meaning that the printed cone reflects back sound in itself. I kind of expected this, since the 3D print has a visible layer structure which makes the surface of the cone bumpy and rough. I considered sanding it or spraying something on the surface that dries smooth, but in the end I went for a very simple approach: I picked a sheet of stiff plastic (I think it was a laser printable transparancy sheet for old fashioned overhead projection), cut a piece out that I could roll into a cone and stick it into the printed cone. And that already did the trick! Here you can see the plastic sheet in the cone (ignore the strange shape of my base – it needed to be that way with my barrel):

Cone with plastic sheet
Cone with plastic sheet

I fixated the sheet with a few drops of superglue – done! And in goes the sensor:

Sensor mounted
Ultrasonic sensor in place

Looking into the cone:

Sensor mounted (inner view)
Sensor mounted (inner view)

And that’s it – works really nice, with solid measurements and a narrow opening angle! Long term stability remains yet to be confirmed…

Update: Long term stability is not fully given… read here!

In summary: As long as you’re OK with the sensor sticking out from your water container (or you have enough space inside it), the watertight versions of the SR04 sensor type are a good idea when used in humid environments, and you can overcome some of their disadvantages.

Make Your Own

Since your actual mount will most likely be different from my situation, I created a STL file only for the cone itself with the mounting slot for the ultrasonic converter plus wrapping which you can include into your project. Download it here or from Thingiverse.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top