Published © GPL3+

Liquid Level Measurement Using UltraSonic Sensor

Here we will learn how to measure the liquid level using ultrasonic sensor.

Full instructions provided742
Liquid Level Measurement Using UltraSonic Sensor

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Story

Read more

Code

Code snippet #1

Plain text
//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;
}



														 
													

Code snippet #2

Plain text
//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;
}

Credits

Comments