Guides for building a DIY water level monitoring system using Ultrasonic Sensor and ESP32 or Arduino UNO are quite common. In real life, using an Ultrasonic Sensor for a water tank is often not practical because a water tank can have 1-2 sources of water falling to fill up the tank from the top. Also, with Ultrasonic Sensor, you need to make sure that the position of the sensor is optimum in a way so that there is no reflection. In industrial fluid measurement systems, laser ToF sensor-based systems are not uncommon.
In this guide, we will use a generic VL53L0X Laser ToF Sensor which is cheap (should cost you around $4). The communication is via I²C. There are challenges with these cheap sensors. These devices are rated for a maximum of 3.3V, you might damage it if you connect it to 5V. You may need to add a pullup resistor to 3.3V to fix the “not working” situation. Also, the number of libraries is fewer and the number of tutorials on the internet is too less. The code or circuit diagram which works for me may not work for you. The total cost of an ESP32 dev board and a generic VL53L0X sensor will cost you around $10. If you power the system using a mobile phone charger, still the cost will not go over $15. It may consume some time to make it work but it is worth that effort.
Brief Note About Laser ToF Sensors
These cheap sensors are of high-end technology. Mostly, flagship devices use this kind of sensor such as Xbox 360, flagship smartphones etc.
---
Time-of-flight (TOF) is the time a particle or wave takes to travel a certain distance. By measuring that time, we can calculate the distance travelled, the velocity or kinetic energy or the composition. These Laser ToF Sensors can also tell you whether water from the inlet is flowing or not. A Raspberry Pi can unlock all possible usage of this sensor.
Libraries and Connection
This is the datasheet:
1 | https://www.st.com/resource/en/datasheet/vl53l0x.pdf |
Mainly there are two main sellers of the VL53L0X sensor module – Adafruit and Pololu. Most of the clone modules support any of these libraries.
1 2 3 4 | # pololu https://github.com/pololu/vl53l0x-arduino # adafruit https://github.com/adafruit/Adafruit_VL53L0X |
The VL53L0X and VL53L1X have a range of 200 and 400 cm respectively. Sparkfun sells VL53L1X module. 200 cm is more than 6 feet. Light takes about 3.3 x 10-12 seconds to travel 1cm. So the module is fast and thus you should shut down the power when not in use.
The Sparkfun library is superior to that of Pololu. But the VL53L1X module will cost you more. Library by Adafruit is not closest to advanced.
Connect Vin to the power supply.
Connect GND to common power/data ground.
Connect the SCL pin to the I2C clock SCL pin on your Arduino (On a UNO this is A5). The hardware I2C clock SCL pin for the ESP32 is GPIO 22.
Connect the SDA pin to the I2C data SDA pin on your Arduino (On a UNO this is digital 20). For ESP32, the SDA pin is GPIO 21.
In ESP32, you can set almost any pin as I2C via software. The VL53L0X has a default I2C address of 0x29.
Example Code
For a basic system, you can test the continuous measurement feature. But Polou has not implemented interrupt in the library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /* This example shows how to use continuous mode to take range measurements with the VL53L0X. It is based on vl53l0x_ContinuousRanging_Example.c from the VL53L0X API. The range readings are in units of mm. */ #include <Wire.h> #include <VL53L0X.h> VL53L0X sensor; void setup() { Serial.begin(9600); Wire.begin(); sensor.setTimeout(500); if (!sensor.init()) { Serial.println("Failed to detect and initialize sensor!"); while (1) {} } // Start continuous back-to-back mode (take readings as // fast as possible). To use continuous timed mode // instead, provide a desired inter-measurement period in // ms (e.g. sensor.startContinuous(100)). sensor.startContinuous(); } void loop() { Serial.print(sensor.readRangeContinuousMillimeters()); if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); } Serial.println(); } |
In this article, I pointed towards building a basic working model.
Notes
If you place the sensor behind glass or clear plastic, there will be a cross-talk effect resulting in errors which required to be calibrated. Naturally, we can send the data as HTTP Post to our own server in the way we have shown how to control a relay with PHP scripts.
Tagged With laser Water Tank Level Monitor