Reputation: 1
I'm currently using an ultrasonic proximity sensor to measure how much of the water in a tank is filled up to. I'm starting by simply trying to receive a distance measurement between the sensor and the object in which the wave is reflected by. I've tried building my own code (in C language) however I'm not quite sure where is my issue. I have the code written to display on the LCD. The numbers in which I am getting are 669, A45, h45. Below is the commented function that I have written to perform this task.
void distance_sensor(void)
{
DDRC= 0x0F; //enables only one nybble to be an output
double timecnt;
double distance;
int echo;
char distb [16] = {"Container up to:"};
lcd_write(distb, 16, line_1); //displays banner on LCD
echo = (PINC & 0x10); //declares the input echo from the 2nd nybble
distance= 0; //initialization
timecnt = 0;
PORTC = 0x00;
_delay_ms(1.0);
PORTC = 0x0F;
_delay_ms(5.2); //creating a 10 us output square wave and sending it out through port c
PORTC = 0x00;
while(echo==0)
{
echo = (PINC & 0x10); //rechecking the input
timecnt = timecnt + 1; //time taken for the wave to reflect back to the sensor
_delay_us(100.0);
}
timecnt= timecnt*100;
distance = (timecnt*0.000343)/2;
hextodec(distance); //function made to split the value into msd, nsd, lsd and displays on LCD
return;
}
Upvotes: -1
Views: 167
Reputation: 4654
As soon as a high level on the TRIG input appears, the ultrasonic module sets the low level at the ECHO output and emits a burst of sound pulses. If there is an obstacle, the sound burst is being reflected back as an echo. As soon as the module detects the echo, it sets the high level at the ECHO output.
Therefore, the distance equals time between emitting sound and receiving echo multiplied by the speed of sound in air and divided by two (because the sound travels the distance forth and back)
FYI in dry air at +20 °C, the speed of sound is 343 meters per second.
The formula is your code is correct
But there are several issues in the code.
1)
PORTC = 0x0F;
_delay_ms(5.2); //creating a 10 us output square wave and sending it out through port c
PORTC = 0x00;
Do not confuse ms (milliseconds) and μs (microsecods).
within 5.2 ms a sound travels 178 cm, therefore you can get wrong results when you start counting echo.
By the why, why you set the high level on 4 outputs? You need to set only at that pin, where TRIG
is connected. (I don't know which is it, let's say it is PC2)
PORTC = 0x0F;
PORTC |= (1 << 2); // set high level at PC2
_delay_us(10); //creating a 10 us output square wave and sending it out through port c
PORTC &= ~(1 << 2); // set low level at PC2
2) You're initializing echo
as
echo = (PINC & 0x10); //declares the input echo from the 2nd nybble
That is wrong. First, because of the ECHO
signal has no sense before TRIG
is triggered, and second, because ECHO
remains high since the previous measurement. Therefore the echo
variable will be initialized to non-zero and while(echo==0)
loop will never be entered.
3) If the module was not able to detect the reflected echo, it will never set the high level on the ECHO
output. Therefore you need to limit somehow how long the loop is waiting, otherwise, it could never end.
4) I don't know what is hextodec
and how it works when a floating-point number is passed to it. Are you sure it will correctly display floating-point numbers less than 1.0? I'm pretty sure it will not try to scale the number up and convert it to an integer:
hextodec(lround(distance * 1000));
Upvotes: 0