Arduino ESP8266 with TL-136 liquid level sensor

I’ve been asked/suggested to share my pet project here. As the title says, it’s a “TL-136” liquid level meter attached to an ESP8266 wifi board. I use it to measure the water volume in my rain collector.

For this, I used the Arduino ESP8266 base, because of the sheer number of libraries present there. I haven’t checked out ESPHome; maybe I’ll do that later. I’ll describe the hardware layout a bit, and then what the software does.

First the hardware. I want the ESP8266 to report the water levels to HA, but also show it on a display. After all, the desire to know how much water is left in the tank is when you’re watering the plants, not checking HA.
My first thought was to use an ultrasonic sensor. There are 5 euro kits with a parking sensor, which will report back the distance between the sensor and an object (the water surface). The downside is that these sensors have a minimal distance of ~22cm, which is already 1/4th of the tank. It’s also not the most stable read-out. So I switched to the 40-euro liquid level sensor named “TL-136”. I took a sensor that has a full scale of 0 to 1 meter. That’s enough for the tank I have, which is 1 meter high. The output of the sensor is 4 to 20 mA, where 0 meter=4mA and 1 meter= 20mA.
This means that we need way for the ESP8266 to measure this current. There is an A/D converter in the ESP8266, but I decided to use an I2C-based AD converter, ADS1115. For any A/D converter to work we need to convert the current into voltage. Here, I took the easiest path and used a 220 Ohm resistor. It means that with 4 mA there is a voltage drop of 2200,004=0,88V and at 20 mA this is (2200,02=) 4,40V. That’s a nice range and feeding the AD converter with 5V means it’s in the safe operating range of the converter.
The TL-136 needs at least 12 volts to operate, so at 20 mA the power supply of the sensor and the 220 Ohm resistor in series must provide at least 16.4 volts, but for safe margin use something at 20 or 24V.
To provide this voltage I used what is known as a DC-DC boost converter. These devices can provide a higher voltage from a lower DC input (i.e convert 5 volts to 21 volts.)

Now the software. I use the Arduino ESP8266 development environment based on Eclipse, called “Sloeber”. There are many Arduino libraries out there, so I combined several libraries. Off course I needed the MQTT library (PubSubClient) as well as WifiManager and the ADS1115 AD Conversion library.

The software measures the voltage over the resistor, which is then converted in a level. Since I have a square tank converting that level to a volume is straightforward. Initially I hand-crafted the configuration yaml for the sensor in HA. Later, I used the MQTT discovery messages.

The software needs to be ‘calibrated’ for the sensor and the tank. This is done with a pitcher, and a bucket. The first step is to establish the actual water level versus the claimed water level. Remember that 4mA is 0M and 20mA is 1meter? My sensor draws 3,85 mA if there is no water. Also the measuring point of the probe is not at the tip. So, for this, I put the sensor in a pitcher, pour enough water on it (it will measure something) and then write down the microvolts returned by the AD converter, (Va).
Then, I added another 10 cm of water. this time be as precise as possible. Now write down the microvolt reading again (Vb) From here, we can calculate the “microvolts per millimeter” since it’s (Vb-Va) divided by 100mm, let’s call it VPM. Now, measure as exact as possible the water level in the pitcher, in mm, (d). This will allow you to compute the uV value that would be returned if the sensor measures 0mm. (it can’t reach that low, the measuring point is 15mm above the tip). The value is computed by Vb-(VPM*d)= Vzero.

Now the sensor is capable of accurately (+/- 5mm) measuring the water level. Now, to convert to volume I need to know what the level drop is when I take a bucket of water out. My bucket is 10 liters and the level drop was 8,5 mm. Since my tank is an IBC and thus square the math for my tank is easy: liters per mm * height. The result is that my HA now reports nicely that there is 1055 liter in my tank.

4 Likes

Nice project Gerrit. Thanks for sharing :blush:

1 Like