Liquid level measurement is a very important application in industrial environment, There are several methods to measure liquid level. Here I will use ultrasonic sensor to measure the liquid level in a tank. Surely this method is one of the best method available for measurement of liquid level and the output is continuous as well.
The principle is very simple , we will send sound waves from sensor towards the liquid surface, the waves hits the surface and comes back to sensor , by measuring the time taken for the waves to hit the sensor we can calculate the distance between liquid surface and sensor.
I am going to use HC-SR04 here. The sensor working is very simple. We need to send 10us long trigger pulse on
trigger pin to start measurement. Once measurement completed you will get signal on echo pin , the length of echo pulse will be equal to the time taken by sound to travel from sensor to liquid surface and back. So we need to capture the length of the echo pulse in order to measure Dm.
The following function(written for Atmega8) will calculates the Dm .
//this function uses Timer 1 and PC3 for trigger, PC4 for echo
float getSurfaceDistance() //gives the distance in cm, gives -1 if time out(no obstacle within 5 meters)
//GIVE A PULSE TO THE PC3
while(!(PINC&(1<<PC4))); //wait until echo signal becomes high
TCNT1=0; //reset the timer
TCCR1B=0x02; //START TIMER(pre scaler - 8)
// START TIMER
while((PINC&(1<<PC4))&&(TCNT1<60000)); //wait until echo signal becomes low or timeout count=TCNT1;//LOAD TIMER if(count>=60000)
I have developed a simple application using this library. This application will switch on differrent loads based on the distance between the obstacle and the sensor. The connection details are present in the code itself. You can use any Atmega8 development board.