[英]What metric unit is the distance equal to when using an ultrasonic sensor?
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.我已经尝试构建自己的代码(用 C 语言),但是我不太确定我的问题出在哪里。 I have the code written to display on the LCD.我已经编写了在 LCD 上显示的代码。 The numbers in which I am getting are 669, A45, h45.我得到的数字是 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;
}
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.一旦TRIG输入出现高电平,超声波模块就会将ECHO输出设置为低电平,并发出一连串声音脉冲。 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.一旦模块检测到回声,它就会在ECHO输出上设置高电平。
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.仅供参考,在 +20 °C 的干燥空气中,声速为每秒 343 米。
The formula is your code is correct公式是你的代码是正确的
But there are several issues in the code.但是代码中有几个问题。
1) 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).不要混淆 ms(毫秒)和 μs(微秒)。
within 5.2 ms a sound travels 178 cm, therefore you can get wrong results when you start counting echo.在 5.2 毫秒内,声音会传播 178 厘米,因此当您开始计算回声时,您可能会得到错误的结果。
By the why, why you set the high level on 4 outputs?为什么,为什么你在 4 个输出上设置高电平? You need to set only at that pin, where TRIG
is connected.您只需要在连接TRIG
那个引脚上进行设置。 (I don't know which is it, let's say it is PC2) (我不知道是哪个,假设是PC2)
PORTC = 0x0F; 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 2)您将echo
初始化为
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.首先,因为在TRIG
之前ECHO
信号没有意义,其次,因为ECHO
自上次测量以来一直保持高电平。 Therefore the echo
variable will be initialized to non-zero and while(echo==0)
loop will never be entered.因此echo
变量将被初始化为非零, while(echo==0)
永远不会进入while(echo==0)
循环。
3) If the module was not able to detect the reflected echo, it will never set the high level on the ECHO
output. 3) 如果模块无法检测到反射回波,它将永远不会在ECHO
输出上设置高电平。 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. 4)我不知道什么是hextodec
以及当一个浮点数传递给它时它是如何工作的。 Are you sure it will correctly display floating-point numbers less than 1.0?您确定它会正确显示小于 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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.