arduino uno 和超声波传感器的结果不准确

Inaccurate results for arduino uno and ultrasonic sensor

你好,我有以下用于我正在使用的超声波传感器的代码,但结果不准确,我想以厘米为单位显示与目标的距离。

    #include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
}

void loop() {

  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*63360;    //Convert miles to inches by multipling by 63360 (inches per mile)

  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" inches");  //Print your units.
  delay(250); //pause to let things settle


  }  

如有任何帮助,我将不胜感激。

I'd like to display the distance from the target in centimetres.

Google 说一英寸是 2.54 厘米,所以只需将结果乘以这个即可。

the result is not being accurate

要获得更准确的结果,请取多个样本(3 个或更多)的平均值,并且您可以实施一些试探法,这些试探法会严重丢弃 over/under 范围内的结果。

假设三个读数相距大约 0-5 厘米,那么相距 40 厘米的第四个读数很可能是错误的。所以只做三个相似结果的平均值。

通过乘以 2.54 将英寸转换为厘米

float targetDistanceCentimeters = targetDistance*2.54;

您的代码看起来是正确的,所以我猜测这是一个硬件问题。以我的经验,超声波传感器通常很难测量到任何非墙壁或其他具有相对平坦表面的大型物体的物体的距离。因此,如果您想测试系统的准确性,请针对此类物体(即墙壁或书本)进行测试。 如果您的传感器在进行测量时正在移动,请确保它不会移动得太快。 如果您正在测量到小或薄物体的距离,您将需要过滤并取测量值的平均值。这完全取决于你想要多准确,我想说平均 20 次左右的过滤测量会给你合适的结果。

另一件需要考虑的事情是你有正确的电源电压并且传感器没有以某个角度对准地板,因为这可能会产生不需要的反射。

您在 loop 的每次迭代中执行大量浮点计算。您可以预先计算一个将 pingTime 转换为以厘米为单位的距离的乘数,而不是执行这些计算。

首先我们需要根据您的 776.5 mph 计算公制声速。

776.5 miles/hour * 1609.34 (meters in a mile) = 1249652.51 metres/hour

1249652.51 metres/hour / 3600 = 347.126 metres/second

所以我们可以预先计算乘数如下:

float speedOfSound = 347.126; // metres per second
float speedOfSound_cm_p_us = speedOfSound * 100 / 1000000; // speedOfSound in centimetres per microsecond

显然,这可以简化为:

float speedOfSound_cm_p_us = 0.0347126; // cm per microsecond

接下来我们可以通过将 speedOfSound_cm_p_us 除以 2 来预先计算乘数,因为声音必须传播到反射表面并再次返回。

float pingTime_multiplier = speedOfSound_cm_p_us / 2; // The sound travels there and back so divide by 2.

同样,我们可以简单地将此步骤替换为:

float pingTime_multiplier = 0.0173563;

但是,这个 神奇的 数字应该在您的代码中得到很好的记录。

一旦我们 pingTime_multiplier 计算出来,loop 函数就变得简单多了。只需将 pingTime 乘以 pingTime_multiplier 即可得到以厘米为单位的距离。

pingTime = pulseIn(echoPin, HIGH);  // pingTime is presented in microseconds
targetDistance = pingTime * pingTime_multiplier;

这显着减少了 Arduino 每次循环必须完成的工作量。