What is Ultrasonic?
Ultrasonic sensor HC-SR04 is a sensor that can measure distance. It emits ultrasound at 40kHz which travels through the air and if there is an object or obstacle on its path it will bounce back to the module. We can calculate the distance if we know the travel time and speed of the sound.
​


The HC-SR04 Ultrasonic Module has 4 pins, VCC, Trig, Echo and Ground. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O in the Arduino Board. The LCD display is added to display the values of the distance and volume of the tank in Litres.
​

Specifications

To generate the ultrasound, the Trig is set on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave travelled.
​

If the object is 20.5 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 602.9 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backwards. So in order to get the distance in cm, multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
​
Calculations


height (h) = 20.5cm
radious (r) = 5.45cm
​
distance(d) = t * 0.034/2
Water level height (wlh) = h - d
​
Volume (v) = pai*r^2* wlh
Capacity in litre = v/1000
Working circuit designs
Code for Arduino
The code measuring the distance is pretty much the same as the basic example. Here, instead of printing the results on the serial monitor, we print them on the LCD. The PDF link on the right-hand side contains the full working code with explanations.
​