Liquid level measurement using ultraSonic sensor

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.

Liquid Level Measurement
Liquid Level Measurement

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)
{
uint16_t count;
float dis;
DDRC|=(1<<PC3); //OUTPUT
DDRC&=~(1<<PC4); //INPUT
//GIVE A PULSE TO THE PC3
PORTC|=(1<<PC3);
_delay_us(10);
PORTC&=~(1<<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)

return -1.0;

TCCR1B=0x00;
dis=(count)*(0.0085);
return dis;
}

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.


/*
UltraSonic based load control
Connections:
trigger- pc3
echo- pc4

load 1(3 feet)- PB0
load 2 (1.5 feet)-PB1

*/
//trigger pc3, echo pc4
#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#include<math.h>
float get_front()
{
uint16_t count;
float dis;
DDRC|=(1<<PC3); //OUTPUT
DDRC&=~(1<<PC4); //INPUT
//GIVE A PULSE TO THE PC3
PORTC|=(1<<PC3);
_delay_us(10);
PORTC&=~(1<<PC3);

while(!(PINC&(1<<PC4)));
TCNT1=0;
TCCR1B=0x02; //START TIMER(8 prescaler)
// START TIMER
while((PINC&(1<<PC4))&&(TCNT1<60000));
count=TCNT1;//LOAD TIMER
TCCR1B=0x00;
dis=(count/200)*(1.7);
return dis;
}

int main()
{
DDRB=0x03;
PORTB=0x00;
while(1)
{
float dis=get_front();
if(dis<91.44)//greater than 3 feet(91.44)
{
PORTB|=(1<<PB0);
}
else
{
PORTB&=~(1<<PB0);
}

if(dis<45.72)//1.5 feet
{
PORTB|=(1<<PB1);
}
else
{
PORTB&=~(1<<PB1);
}
_delay_ms(1000);//1 sec delay to avoid blinking
}
}

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *