在Arduino中使用超声波雷达(HC-SR04)作为距离传感器

在Arduino中使用超声波雷达(HC-SR04)作为距离传感器

编码文章call10242024-12-19 11:41:3928A+A-

距离传感器是机器人项目最有用的传感器之一。HC-SR04 是一种廉价的超声波距离传感器,可以帮助您的机器人在房间内导航。稍加改装再加上一些小组件,它也可以用作测量设备。在本文中,您将了解将这款出色的小设备与 Arduino 配合使用所需的一切知识。

本文英文原作者: DroneBotWorkshop 翻译整理:DIY百事

目录

1. HC-SR04 超声波距离传感器
2. HC-SR04 的工作原理
3. 连接 HC-SR04
4. 基本 Arduino 演示
5. Arduino 库函数
6. 提高测量精度
7. 使用 3 线模式
8. 使用多个 HC-SR04 传感器
9. 留意超声波对动物的影响!
10 总结

HC-SR04 超声波距离传感器

HC-SR04 超声波距离传感器是一种价格低廉的设备,对机器人和测试设备项目非常有用。这种微型传感器能够测量自身与最近的固体物体之间的距离,如果您想避障,这是非常有用的信息!

HC-SR04 可以直接连接到 Arduino 或其他微控制器,并在 5 伏电压下运行。它也可以与 Raspberry Pi 一起使用,但是由于 HC-SR04 需要 5 伏逻辑,您需要几个电阻器将其与 Pi 的 3.3 伏 GPIO 端口连接。

这种超声波距离传感器能够测量 2 厘米到 400 厘米之间的距离。它是一种低电流设备,因此适用于电池供电设备。它甚至看起来很酷,就像您最新机器人发明的一双 Wall-E 机器人眼睛!

我将向您展示如何连接和使用 HC-SR04 超声波距离传感器。我们还将对其进行一些测试,以了解它的准确度,并研究如何改进该准确度。当然,我会提供一些示例代码和项目供您试用。

HC-SR04 的工作原理

超声波距离传感器使用超声波脉冲(高于人类听觉范围的声音)来检测它们与附近固体物体之间的距离。传感器由两个主要部件组成:

  • 超声波发射器– 发射超声波脉冲,工作频率为 40 KHz
  • 超声波接收器– 接收器侦听传输的脉冲。如果它接收到它们,它会产生一个输出脉冲,其宽度可用于确定脉冲行进的距离。

HC-SR04 有以下四种连接方式:

  • VCC – 这是 5 伏正电源。
  • Trig—这是“触发”引脚,被驱动发送超声波脉冲。
  • Echo – 这是在接收到反射信号时产生脉冲的引脚。脉冲的长度与检测到传输信号所用的时间成正比。
  • GND – 这是接地引脚。


设备操作如下:

  1. 将持续时间至少为 10 uS(10 微秒)的 5 伏脉冲施加到触发引脚。
  2. HC-SR04 通过以 40 KHz 的频率传输 8 个脉冲串来响应。这种 8 脉冲模式使设备的“超声波特征”独一无二,使接收器能够区分传输的模式和超声波背景噪声。
  3. 八个超声波脉冲在空气中远离发射器传播。同时,Echo 引脚变为高电平以开始形成回声信号的开头。
  4. 如果脉冲未反射回来,则回声信号将在 38 毫秒(38 毫秒)后超时并返回低电平。这会产生一个 38 毫秒的脉冲,表明传感器范围内没有障碍物。
  5. 如果脉冲被反射回来,当接收到信号时,Echo 引脚变低。这会产生一个宽度在 150 微秒到 25 毫秒之间变化的脉冲,具体取决于接收信号所用的时间。
  6. 接收到的脉冲宽度用于计算到反射物体的距离。请记住,脉冲表示信号发出并反射回来所需的时间,是2倍的实际的距离。


下图显示了 HC-SR04 超声波距离传感器的尺寸以及有效操作角度。如您所见,当要检测的物体直接位于传感器正前方时,传感器最准确,但您也能探测到45 度范围内的物体。建议将该窗口限制在 30 度(两侧各 15 度)以获取准确读数。

连接 HC-SR04

将 HC-SR04 连接到 Arduino 非常简单。您将需要几个数字 I/O 端口以及与 Arduino 的 5 伏和接地引脚的连接。


实际上,如果您缺少引脚,您甚至可以将 HC-SR04 的 Trigger 和 Echo 引脚连接到 Arduino 上的一个数字 I/O 引脚,并使用代码在输出之间切换引脚(发送 10 uS 脉冲) 和输入(接收回波脉冲)。一些超声波传感器实际上只有一个引脚可以同时触发和回声。我将讨论这个问题并在下面给出一个例子,所以请继续阅读。

我将在此处向您展示的大多数示例都使用更传统的两引脚方法。可以使用任何 Arduino 和任何没被占用的数字 I/O 引脚,因此如果您希望将其连接到一组不同的 I/O 引脚,那么只需更改代码以反映这些更改。对于我们的演示,我将使用 Arduino Uno,将引脚 10 用于Trig(触发),将引脚 13 用于Eco(回声)。

HC-SR04 的应用说明强调您需要在连接 VCC(5 伏)之前连接接地引脚,因此如果您在无焊面包板上进行“实时”试验,您可能需要记住这一点.

现在我们已经连接了我们的超声波距离传感器,是时候编写一些代码并进行测试了。

基本的 Arduino 演示

在我们的第一个演示中,我们将简单地测试传感器以查看它是否正常工作。代码非常简单,它使用串口监视器显示它检测到的距离,以厘米为单位。让我们详细看一下。

为了测试超声波距离传感器的准确性,我设置了一个测试板,一端安装了一个传感器(我使用 Velcro 来安装它)。我在板上放了一根 1 米长的棍子,这样我就可以在 2 -100 厘米范围内测试传感器。如果您想查看此演示和其他演示的结果,可以观看与本文相关的视频。

基本的 HC-SR04 代码

有关其工作原理的详细分解,只需查看视频或阅读代码中的注释即可。

/*
  HC-SR04 Basic Demonstration
  HC-SR04-Basic-Demo.ino
  Demonstrates functions of HC-SR04 Ultrasonic Range Finder
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// This uses Serial Monitor to display Range Finder distance readings
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
 
#define trigPin 10
#define echoPin 13
 
float duration, distance;
 
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
  
  // Write a pulse to the HC-SR04 Trigger Pin
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the response from the HC-SR04 Echo Pin
 
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
 
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
     Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

Arduino 代码库

在我们的第一个代码中,我们没有使用任何代码库,我们只是使用 Arduino 的delayMicrosecond命令来创建我们的 10 uS 脉冲用于触发,并使用pulseIn命令来测量接收到的信号脉冲宽度。但是,还有其他使用特殊代码库的方法可以做到这一点。有很多可用的,最通用的是一种叫做“NewPing”的。

如果您没有在 Arduino 代码中使用库的任何经验,那么这是您真正需要学习的技能。库为特定任务提供代码功能,实际上有数百个库可用于 Arduino 用于任务并支持各种外部硬件。

Arduino 网站提供了在 Arduino IDE 中安装新库的说明

NewPing 库由Tim Eckel编写,它取代了由 Caleb Zulawski 编写并主要为视差 Ping 超声波传感器设计的旧 Ping 库(尽管如果您在 3 针模式下使用它,它将与 HC-SR04 一起使用) .

NewPing 库非常先进,它大大提高了我们原始代码的准确性。它还同时支持多达15个超声波传感器,可以直接以厘米、英寸或持续时间为单位输出。

这是我们重写的代码以使用 NewPing 库:

/*
  HC-SR04 NewPing Library Demonstration
  HC-SR04-NewPing.ino
  Demonstrates functions of NewPing Library for HC-SR04 Ultrasonic Range Finder
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// This uses Serial Monitor to display Range Finder distance readings
 
// Include NewPing Library
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float distance;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  distance = sonar.ping_cm();
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

上面的代码很简单,效果很好,但它的分辨率只有一厘米。如果您想恢复小数点值,您可以在持续时间模式而不是距离模式下使用 NewPing。然后我们可以使用持续时间来计算距离,就像我们在第一个代码中所做的那样。

这是我们重写的代码 NewPing 以使用持续时间而不是距离。


/*
  HC-SR04 NewPing Duration Demonstration
  HC-SR04-NewPing-Duration.ino
  Demonstrates using Duration function of NewPing Library for HC-SR04 Ultrasonic Range Finder
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// This uses Serial Monitor to display Range Finder distance readings
 
// Include NewPing Library
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float duration, distance;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  duration = sonar.ping();
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

NewPing 的另一个功能是“迭代” 迭代意味着不止一次地重复某件事。它需要多次持续测量而不是一次,丢弃任何无效读数,然后对剩余的读数求平均值。默认情况下需要 5 个读数,但您实际上可以指定任意数量的读数。

在这里,我们再次使用为使用迭代而编写的 NewPing 代码。正如您所看到的,它与之前的代码几乎相同,所添加的只是一个用于指定迭代次数的变量。

/*
  HC-SR04 NewPing Iteration Demonstration
  HC-SR04-NewPing-Iteration.ino
  Demonstrates using Iteration function of NewPing Library for HC-SR04 Ultrasonic Range Finder
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// This uses Serial Monitor to display Range Finder distance readings
 
// Include NewPing Library
#include "NewPing.h"
 
// Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
// Maximum Distance is 400 cm
 
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
float duration, distance;
 
int iterations = 5;
 
void setup() {
  Serial.begin (9600);
}
 
void loop() {
  
  duration = sonar.ping_median(iterations);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

提高准确性

HC-SR04 相当准确,它的基本形式非常适用于机器人、入侵者检测或接近传感器。但有时您可能需要更高的精度,例如您可能正在构建一个测量工具或可能正在使用您的机器人来绘制房间的周长。如果是这样,您可以采取一些措施来提高 HC-SR04 的准确性。

正如我在上一节中提到的,NewPing 库已经实施了许多内部技术来提高传感器的精度。

如果您正在设计要在户外或异常炎热或寒冷的环境中使用的设备,您可能需要考虑这样一个事实,即空气中的声速会随温度、气压和湿度而变化。由于声速会影响我们的 HC-SR04 距离计算,如果温度比室温高或低很多,这可能会影响我们的读数。

为了考虑温度和湿度,我决定使用 DHT22 传感器,它相对便宜但非常准确。你也可以使用更便宜的 DHT-11 来做这个实验,但精度稍低。这是我如何连接它:

DHT22 需要几个代码库才能运行,Adafruit 有两个库可以很好地与 DHT22 和 DHT11 配合使用。Adafruit AM2315 库和 Adafruit Unified Sensor库都可以使用库管?理器直接安装在 Arduino IDE 中。

安装 Adafruit 库后,我编写了一个快速测试代码,以确保我的 DHT22 正常工作。

/*
  DHT22 Basic Demonstration
  DHT22-Basic-Demo.ino
  Demonstrates functions of DHT22 Digital Temperature & Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
 
#include "DHT.h";
 
// Define Constants
 
#define DHTPIN 7     // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
 
// Define Variables
 
float hum;  //Stores humidity value
float temp; //Stores temperature value
 
void setup()
{
  Serial.begin(9600);
  dht.begin();
}
 
void loop()
{
    delay(2000);  // Delay so sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Print temperature and humidity values to serial monitor
    
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
  
}

下面是一个考虑了温度和湿度,以提高使用 DHT22 的精度的代码。

/*
  HC-SR04 with Temp and Humidity Demonstration
  HC-SR04-Temp-Humid-Demo.ino
  Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  With DHT22 Temperature and Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(2000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Sound: ");
    Serial.print(soundsp);
    Serial.print(" m/s, ");
    Serial.print("Humid: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.print(" C, ");
    Serial.print("Distance: ");
 
    if (distance >= 400 || distance <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance);
    Serial.print(" cm");
    delay(500);
    }
  
  Serial.println(" ");
}

在我在工作台上的测试中,我发现它确实提高了读数的准确性。

使用 3 线模式

正如我之前提到的,可以在“3 线模式”下使用 HC-SR04。在这种模式下,您只需要一个 Arduino 数字 I/O 引脚。还有一些别的超声波传感器只能在 3 线模式下运行。

在 3 线模式下,单个 I/O 引脚同时用作输入和输出。这是可能的,因为从来没有同时使用输入和输出的时间。通过消除一个 I/O 引脚要求,我们可以保存与 Arduino 的连接并将其用于其他用途。当使用像 ATtiny85 这样的 I/O 引脚数量有限的芯片时,它也很有用。

这是我将 HC-SR04 连接到 Arduino 的方法。


如您所见,我所做的只是将Trig(触发器)和Echo(回声)连接到 Arduino 引脚 10。

这是我写的使用它的代码。请注意,此代码与上一个代码之间的唯一区别是,我已为 Trigger 和 Echo 引脚值指定了引脚 10。代码的其余部分是相同的。


/*
  HC-SR04 in 3-Wire Mode with Temp and Humidity Demonstration
  HC-SR04-3Wire-Temp-Humid-Demo.ino
  Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  With DHT22 Temperature and Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN  10  // Trigger and Echo both on pin 10
#define ECHO_PIN     10
#define MAX_DISTANCE 400
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(2000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Sound: ");
    Serial.print(soundsp);
    Serial.print(" m/s, ");
    Serial.print("Humid: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.print(" C, ");
    Serial.print("Distance: ");
 
    if (distance >= 400 || distance <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance);
    Serial.print(" cm");
    delay(500);
    }
  
  Serial.println(" ");
}

使用多个 HC-SR04 传感器

在许多应用中,您需要在设计中使用多个 HC-SR04 超声波传感器。当您想要从机器人或项目的不同侧面监控与外部物体的距离时,就会发生这种情况。其中两个可用于前后传感器,或连接其中的 6 个并监控立方体的每一面 - 由您决定!

使用多个传感器时,一个明显的考虑是您需要防止一个传感器发出的信号被另一个传感器拾取和测量。完成此操作的最简单方法是简单地在一个传感器上触发触发器,并等到您收到回波后再继续下一个传感器。明智的做法是在读数之间留一点延迟,以防之前的传感器声波仍在房间内反弹!

这是我如何在 Arduino 中使用两个 HC-SR04 超声波传感器。请注意,我以 3 线模式连接它们,如果您愿意,也可以以传统的 4 线方式连接它们。如果您这样做,则只需修改代码以指定不同的触发和回声引脚。

这是一个使用 NewPing 库和两个传感器的代码。与所有其他代码一样,它将结果输出到串口监视器。

/*
  Dual HC-SR04 with Temp and Humidity Demonstration
  HC-SR04-Temp-Humid-Dual-Demo.ino
  Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  With DHT22 Temperature and Humidity Sensor
  Displays results on Serial Monitor
 
  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/
 
// Include DHT Libraries from Adafruit
// Dependant upon Adafruit_Sensors Library
#include "DHT.h";
 
// Include NewPing Library
#include "NewPing.h"
 
// Define Constants
 
#define DHTPIN 7       // DHT-22 Output Pin connection
#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN_1  10
#define ECHO_PIN_1     10
#define TRIGGER_PIN_2  5
#define ECHO_PIN_2     5
#define MAX_DISTANCE 400
 
NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
 
// Define Variables
 
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration1; // Stores First HC-SR04 pulse duration value
float duration2; // Stores Second HC-SR04 pulse duration value
float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
 
// Initialize DHT sensor for normal 16mhz Arduino
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin (9600);
  dht.begin();
}
 
void loop()
{
 
  delay(1000);  // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();  // Get Humidity value
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
                 soundcm = soundsp / 10000;
                
                 // Measure duration for first sensor
    
  duration1 = sonar1.ping_median(iterations);
  
  // Add a delay between sensor readings
  
  delay(1000);
  
  // Measure duration for first sensor
  
  duration2 = sonar2.ping_median(iterations);
  
  // Calculate the distances for both sensors
  
  distance1 = (duration1 / 2) * soundcm;
  distance2 = (duration2 / 2) * soundcm;
  
  // Send results to Serial Monitor
  
    Serial.print("Distance 1: ");
 
    if (distance1 >= 400 || distance1 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance1);
    Serial.print(" cm ");
    }
    
    Serial.print("Distance 2: ");
 
    if (distance2 >= 400 || distance2 <= 2) {
    Serial.print("Out of range");
    }
    else {
    Serial.print(distance2);
    Serial.print(" cm");
    }
  
  Serial.println(" ");
}

留意超声波对动物的影响!

在围绕任何超声波发射器(如 HC-SR04)设计设备时需要考虑的一件事是,许多动物都可以听到超声波。根据维基百科,这包括狗、猫、沙鼠和兔子。

如果您有毛茸茸的 4 腿朋友在您的房子里闲逛,您可能希望避免让它们受到传感器发出的声音的影响,因为这可能会让它们非常烦人。想象一下,每隔一秒就会听到一连串高亢的“哔哔声”,你可以理解你的宠物可能正在经历的事情。

还有其他测量距离的方法,如红外光和激光雷达,它们不涉及超声波,因此如果您家中需要考虑动物,您可能需要研究一下。

总结

如您所见,HC-SR04 超声波距离传感器是一种价格低廉但实用的设备,可用于多种应用。如果您正在构建机器人,这是您工具包中的重要组成部分。

希望这篇文章对您有所帮助。如果您对 HC-SR04 或我在此处展示的代码有任何疑问,请在下面的评论部分写下它们。如果您想出一个基于 HC-SR04 的项目,欢迎共享给大家。

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4