In this article, we will make a device that will monitor liquid levels in water/oil or any tank. The monitored level information will be sent to a monitoring station. This can be useful for monitoring some water tanks installed in places where humans cannot reach frequently and in places that are toxic, such as laboratories where liquid chemicals are stored in large containers.

link:
Required components
Required tools/required libraries
Arduino IDE
Arduino Library – Wi-Fi manager
Technical information
We will be using an IR distance measuring sensor to measure the liquid level in containers or tanks. The sensor output is measured in one unit and stored in a microcontroller; in this case, Arduino UNO. The sensor data is then sent using ESP to the Thingspeak server for monitoring.
Block diagram

Figure 1 Liquid Level Monitoring System
The IR sensor is connected to the Arduino Uno and sending sensor data to it. ESP is connected to Arduino and Wi-Fi router, with a ThingSpeak library for communication purposes.
Circuit Diagram

Figure 2 Atmega 328 board with ESP
How the system works
- When turned on for the first time, ESP generates a hotspot called “OilTank -1” displayed on an OLED display.
- We need to connect to it and open the URL to configure ESP.
- Then in the configuration panel we can define which Wi-Fi ESP will connect to. Just enter the Wi-Fi SSID and password.
- After connecting to Wi-Fi, the Wi-Fi name is displayed on the OLED screen.
- A battery also operates the device, so the battery percentage is also shown on the screen and the liquid level in the tank.
- Now, after connecting to the internet, the device sends data every 20 seconds to the Thingspeak server.
- If Wi-Fi is disconnected, on the screen you will see that Wi-Fi is disconnected and ESP will generate a hotspot again.
Understanding the source code
To understand the code, there are two files; one is for ESP and another is for Arduino.
ESP.ino
We are also using a Wi-Fi library for ESP to easily configure ESP using your access point without writing Wi-Fi information in code. We are connected with Thingspeak API when Internet is connected. The code is designed to send anything that reaches the serial, which is not an ESP command, to the thingspeak API panel.
So let's take a look at the code.
- In the configuration function we are creating a hotspot using the WiFi-Manger library
wifiManager.autoConnect(“OilTank-1”); - If the hotspot is created, the ESP was not previously connected to the Wi-Fi that is now available. The hotspot is a program blocking loop, so we send a “setup” command before creating a hotspot. This means that the configuration information will be displayed on the screen.
- If the hotspot is OFF, we send the “conn” command, which means the ESP is connected to Wi-Fi, and the program continues.
Serial.print(“con”); - The ESP is also listening to some commands which the Arduino responds to
- If none of these commands are recognized then this data will be the tank level data that will be sent using the thingspeak library
ThingSpeak.writeField(myChannelNumber, 1, recivedData, myWriteAPIKey);
Arduino.ino
Arduino is installed with an OLED library to display OLED data; it also communicates with ESP in serial communication.
The Arduino code also takes analog values from the sensor on analog pin A2 and calculates the voltages. From there we can map this to the tank level.
The tank_data function takes a reading on analog pin 2 and then calculates the voltage reading and sends it to the ESP over a period of 20 seconds.
val = analogRead(A2);
voltage = ((val * 3.3000) / 1024.0);
There are some commands for OLED, which Arduino is receiving from ESP in series.
This is how we can make a liquid level monitoring system.
Board.ino source code:
ES.ino source code:
Video demonstration: