Liquid Level Monitoring (VL53L4CD)
Liquid Level Monitoring (VL53L4CD)
Application note
Water and liquid level monitoring using VL53L4CD Time-of-Flight high accuracy
proximity sensor
Introduction
This document describes how a user can design a system to measure the level of a liquid in a container with ST Time-of-Flight
(ToF) sensors.
The ST ToF sensors work by emitting a pulsed cone of light, which hits the target and bounces back. The sensor detects the
returning signal and measures the photon travel time. The travel time divided by two and multiplied by the speed of light gives
the distance. In most cases, this is a very robust system.
In this application note, the VL53L4CD ToF sensor is used to measure the level of water in a container. All ST single zone
Time-of-Flight sensors (including VL53L3CX and VL53L0X) can be used in the same manner.
Background
The VL53L4CD is a ToF sensor especially designed to provide very accurate distance measurements from only 1 mm up to
1300 mm. A new generation laser emitter with 18° diagonal FoV improves performances under ambient light, with ranging
speed up to 100 Hz.
1 Observations on water
The basic principle of a ToF sensor is to measure the distance to the target based on the time for emitted photons
to be reflected. In most applications, photons travel through the air. But, to measure the level of a liquid, photons
travel through air and water. As a result, the light path is impacted by the different refractive indices.
In other words, there is part of the signal that is reflected from the surface, and another part returned from the
container’s base (traveling through the liquid), and another part bounces from the liquid itself. Thus, the final
ranging distance is impacted with respect to accuracy (see Figure 1. Different light paths when ToF signal aimed
at liquid). However, this document describes some techniques and methods to minimize the ranging error, using
the nonlinearity correction algorithm.
2 ST recommendations
In addition, there should be a reasonable gap (minimum 30 mm) between the sensor and the maximum water
level. This avoids any obstruction of the ToF’s light path, in the case water drops bounce while the water is
pouring, or if the container is shaken.
3 ST solution
Example
If a container’s height is 1000 mm and it has five indicator levels. The distance corresponds to the indicators: 200
mm, 400 mm, 600 mm, 800 mm, and 900 mm. These correspond to 20%, 40%, 60%, 80%, and 90%.
When the water level starts pouring, note the ranging distance at each level.
20 800 840 5
40 600 620 3
60 400 380 5
80 200 160 20
90 100 70 30
You can reiterate the test to get a clearer view of the deviation in ranging with regard to the water level.
Apply these ranging errors to the algorithm to improve the ranging accuracy. For example, if the program gets 840
mm in 20% water level, then apply 5% offset to the algorithm to minimize the error.
It is recommended to use a dust free cover glass on top of the container and to measure more than one
similar setup (minimum 10 sets) to validate the consistency in measurement. Once the same nonlinear curve
with ranging deviation is found across all setups, you can adopt a nonlinearity algorithm to reduce nonlinearity in
ranging.
This algorithm uses a lookup table that is created based on noted data points and ranging deviation on different
water levels. The lookup table can be easily modified based on the container’s size or setup, and the data
received. The only input to this algorithm is the total distance from the sensor to the container’s base, which
needs to be stored in the host memory (NVM). This algorithm reads this value before the start of ranging, and
calculates how much offset to apply dynamically on different levels.
Alternatively, the user can apply a linear regression model to all data points to find the intercept and coefficient for
the best fit line. These two parameters can be used in ranging to compensate the ranging deviation.
Following is a snippet of characterization tool; it allows the user to perform a defined number of times the
characterization setup. For example, in the characterization plot below, the red lines show the characterization of
the same setup and liquid three times. The green line is the mean derived from all characterized values. This can
be used to create a lookup table (Figure 10. Data for lookup table).
The final data points are created in FinalResult.csv (Figure 11. Sample data points of characterization).
The characterization process begins by continuously pouring liquid at each level and clicking the “Characterize”
button until all levels are finished. This process continues until the defined number of setups or iterations are
characterized. At the end, the user can see the final characterization graph by using a nonlinearity correction
algorithm. For the experiment, it includes a linear regression model as well.
// Check first
if (i >0)
{
value = (ogalgo_data_inst[i].expected_res + ogalgo_data_inst[i-1].expected_res)/2;
}
else
value = ogalgo_data_inst[i].expected_res;
Based on the liquid level, the algorithm detects if it is over ranging or under ranging. It then uses a lookup table to
apply offset to improve ranging accuracy. The offset compensation value applied depends on the environment or
setup.
// position = water level position, og_val= got compensated value from characterization method
switch(pos)
{
case 1:
*rangevalue_out=rangevalue + ogalgo_data_inst[pos].og_val; // Category Underranging,
break;
case 2:
*rangevalue_out=rangevalue + ogalgo_data_inst[pos].og_val; // Category Underranging, 30mm deviation b/w C3&C2
break;
case 3:
*rangevalue_out=rangevalue + ogalgo_data_inst[pos].og_val; // Category Underranging, 25mm deviation b/w C4&C3
break;
case 4:
*rangevalue_out=rangevalue + ogalgo_data_inst[pos].og_val; // Category Underranging, 12mm deviation b/w C2&C2
break;
case 5:
*rangevalue_out=rangevalue - ogalgo_data_inst[pos].og_val; // Category Overranging, 4mm deviation b/w C2&C2
break;
case 6:
*rangevalue_out=(uint16_t)(rangevalue - ogalgo_data_inst[pos].og_val); // Category Overranging, 1mm deviation
break;
case 7:
*rangevalue_out=(uint16_t)(rangevalue - ogalgo_data_inst[pos].og_val); // Category Overranging, 2mm deviation
break;
case 8:
*rangevalue_out=(uint16_t)(rangevalue - ogalgo_data_inst[pos].og_val); // Category Overranging, 4mm deviation
break;
case 9:
*rangevalue_out=(uint16_t)(rangevalue - ogalgo_data_inst[pos].og_val); // Category Overranging, 4mm deviation
break;
default:
printf("Not meet condition");
}
The test setup uses a narrow transparent glass bottle, with a width of 220 mm and radius of 50 mm. The sensor is
mounted slightly above the bottle, but all the measurements are relative to the initial height. The test is conducted
on the water level with a timing budget of 50 ms. This application is not usually time critical, so the rolling average
along with a bigger timing budget can be used to allow the ToF to accumulate more signals that ultimately
enhance ranging performance.
4.1 Test results without nonlinearity correction algorithm and without cover glass
The following graph shows the ranging data with different water levels. It can be observed that there is a
nonlinearity between expected and actual measurement in low water level.
4.2 Test results with offset calibration, without nonlinearity correction algorithm,
and with cover glass
The following graph illustrates the data with offset calibration and with cover glass, but without nonlinearity
correction algorithm. The offset calibration is done with the same setup, maintaining 100 mm distance from sensor
to water level. The results are more accurate, yet over range persists in low water levels. Offset calibration is done
using the feature available and described in the VL53L4CD UM2931.
Figure 15. With offset calibration, without nonlinearity correction algorithm, and with cover glass
4.3 Test results with offset calibration, with nonlinearity correction algorithm, and
with cover glass
The following figure shows that the measurements are quite linear between expected and actual measurement
after adopting the nonlinearity correction algorithm and offset calibration. It may be further fine tuned depending
on the specific container setup.
Figure 16. With offset calibration, with nonlinearity correction algorithm, and with cover glass
6 Known limitations
The following known limitations have been identified and should be considered for final setup:
• Ranging is not reliable if the distance between the sensor and the water level is less than 20 mm.
• A highly reflective bottom impacts the accuracy for low water levels.
The lower reflectivity base has better linearity in ranging than high reflectivity. It is recommended to
use a low reflective base, like black mat reflector when selecting a container. Figure 19. Water level
measurement with different colored base and no compensation shows the effect of the base's reflectivity on
the ranging measurement without compensation.
• Ranging results are not reliable if water or liquid is in motion (for example ripples on the surface), or if
bubbles form on the surface.
Figure 19. Water level measurement with different colored base and no compensation
Revision history
Table 2. Document revision history
Contents
1 Observations on water. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 ST recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
2.1 Container properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Container reflectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 ST solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
3.1 Nonlinearity correction algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Ranging result characterization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 System characterization using ST tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Nonlinearity correction algorithm flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.5 Reference code for nonlinearity correction algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Test setup and results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
4.1 Test results without nonlinearity correction algorithm and without cover glass . . . . . . . . . . . 13
4.2 Test results with offset calibration, without nonlinearity correction algorithm, and with cover
glass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3 Test results with offset calibration, with nonlinearity correction algorithm, and with cover
glass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Non transparent liquid results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Description of the experiment context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
5.1 Hardware required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.2 Cover glass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.3 Software requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6 Known limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18