# IoT water tank telemetry project – ultrasonic sensor – HC-SR04

This is a new project derived from IoT water tank telemetry project – part 1 , but using an inexpensive ultrasonic ranging sensor to determine the height of water in a tank or dam, and so water depth or volume. Above is the HC-SR04 ultrasonic ranging sensor, it was purchased for around \$6 from a local eBay seller and delivered within days. Note that there are somewhat similar looking things with a second board on the back and a different interface, the basic HC-SR04 as pictured suits this project.

The basic operation is that a short pulse is sent on the TRIG pin and a short while later, a pulse occurs on the ECHO pin of duration equal to the round trip time. The speed of sound is about 340m/s @ 20°, and so the distance to the water can be calculated. Because the speed of sound is a little dependent on air temperature, the project incorporates a DS18B20 one wire temperature sensor to feed a compensation calculation.

## Dam volume vs depth

The project incorporates a means of calculating the volume of water from depth.

For simple tanks with vertical sides, the volume per increment of depth is uniform, but for more general shapes, eg a natural earth dam, that is not so.

Agricultural agencies have published lots of guides on estimating dam volume from surveys of the form during or after dam construction. For the purpose of testing of the software, a simple ‘sperical cap' form is assumed. This example is that of a spherical bowl of radius 100m and maximum depth of 5m, giving a maximum capacity of 7.7Ml.

The dam profile is characterised by a small set of points from which a cubic spline interpolation provides a stepless response. Above is the dam characteristic, volume in m^3 vs height in m.

## Computation

To make things easier for the system, the dam characterisation is formatted in a file dam.xy in a way that makes interpretation quicker / easier for the lua system.

```Entry{0.000,0.000}
Entry{1.000,313.112}
Entry{2.100,1375.744}
Entry{3.000,2799.159}
Entry{4.000,4959.528}
Entry{5.000,7723.082}```

Above is the table of depth,volume pairs characterising our dam. Above is a plot of the dam characterisation and a plot of the cubic spline interpolation.

In fact the sensor measures the height above the water, so depth must be calculated from that. The code incorporates a linear interpolation function that deals with the negative slope of the function to convert measured distance to depth, and provides also for an intercept to correct for offset.

The calculated depth is then used to calculate the volume using the cubic spline interpolation.

## Test run A test was performed indoors over a fixed distance of 0.75m to assess measurement noise (or jitter) and validate the temperature compensation. Above is a chart of calculated volume and environment temperature. It looks pretty good, lets expand the scales. The plots suggest that long term, the temperature compensation is good, though there might be some very small dynamic variations due to sensor lag and air currents. To put it in perspective, the standard deviation of volume over the 100 measurements is 0.074%, that is quite good.

A work in progress…