风筝
发表于: 2018-2-3 15:48:20 | 显示全部楼层

距离传感器是机器人项目最有用的传感器之一。 HC-SR04超声波距离传感器价格便宜,并且可以帮助您的机器人在房间周围导航。通过一些努力和额外的组件,它也可以用作测量设备。在本篇文章中,您将学习到通过Arduino开发板使用这个美妙的小设备所需要知道的一切。


HC-SR04超声波距离传感器

HC-SR04超声波距离传感器是一种便宜的设备,对于机器人和测试设备项目非常有用。这个微小的传感器能​​够测量自身和最近的固体物体之间的距离,如果您试图避免进入墙壁,这是非常好的信息!


HC-SR04可以直接连接到Arduino或其他微控制器,它的工作电压为5伏。它也可以与Raspberry Pi一起使用,但是由于HC-SR04需要5V电压,因此需要一对电阻来连接Pi的3.3V GPIO端口。


这种超声波距离传感器能够测量2厘米到400厘米之间的距离(对于那些不会说“公制”的人来说,大约在1英寸到13英尺之间)。这是一个低电流设备,所以它适用于电池供电设备。作为奖励,它甚至看起来很酷,就像一对Wall-E机器人的眼睛为您的最新机器人发明!


那么请继续阅读,并告诉您如何连接和使用HC-SR04超声波距离传感器。我们也会通过一些测试来看看它是多么准确,我们将看看我们如何能够提高精度。当然,我会有一些示例代码和项目供您试用。让我们开始吧!


HC-SR04的工作原理

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

●     超声波发射机 - 发射超声波脉冲,它工作在40KHz。

●     超声波接收机 - 接收发射的脉冲。如果接收到它们,则会产生一个输出脉冲,其宽度可用于确定脉冲行进的距离。


HC-SR04有以下四个连接:

●     VCC - 5V正电源。

●     Trig - “触发器”引脚,驱动该引脚发送超声波脉冲。

●     Echo - 当接收到反射信号时产生脉冲的引脚。脉冲的长度与发送信号被检测到的时间成正比。

●     GND - 接地引脚。

HC-SR04-Pinout-e1504971009877.jpg


该设备的工作过程如下:

1.    一个持续时间至少10微秒的5伏脉冲施加到Trigger引脚。

2.    HC-SR04通过以40KHz发送8个脉冲来响应。这个8脉冲模式使得设备的“超声波签名”是独一无二的,使接收机能够区分发射模式和超声背景噪声。

3.    八个超声波脉冲通过远离发射机的空气传播。同时,Echo引脚变为高电平,开始形成回波信号的开始。

4.    如果脉冲信号不反射回来,则回声信号将在38毫秒(38毫秒)后超时并返回低电平。这会产生一个38毫秒的脉冲,表明传感器范围内没有任何障碍物。

5.    如果脉冲被反射回来,当收到信号时,Echo引脚变为低电平。这产生了一个脉冲,其宽度在150μs到25mS之间变化,这取决于信号被接收所花费的时间。

6.    接收到的脉冲宽度用于计算到反射物体的距离。请记住,脉冲表示信号被发送出去并反射回来的时间,所以你需要将结果除以2来得到距离。

HC-SR04-Timing1-e1504971036309.jpg


HC-SR04-Timing2-e1504971067156.jpg


下图显示了HC-SR04超声波距离传感器的尺寸以及有效操作角度。正如你所看到的,当待检测物体直接位于其前面时,传感器是最准确的,但是你可以在45度“视角”内得到物体的响应。文件建议将窗口限制在30度(两边15度)以获得准确的读数。

HC-SR04_2-e1504971094490-1.png


连接HC-SR04

将HC-SR04连接到Arduino非常简单。您将需要一些数字I / O端口,并连接到Arduino的5V和接地引脚。

HC-SR04-Basic-1-e1504971116271.jpg


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


我将在这里向大家展示的大多数示例都使用更传统的双引脚方法。任何免费的Arduino和任意数字I / O引脚都可以使用,所以如果你希望把它连接到一组不同的I / O引脚,只需要改变示例来反映这些变化。演示程序中我将使用Arduino Uno,引脚10为Trigger,引脚13为Echo。


HC-SR04的应用笔记强调,在连接VCC之前,您需要连接接地引脚,所以如果您在面包板上进行“实时”实验,则可能需要牢记这一点。


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

跳转到指定楼层
风筝
发表于: 2018-2-3 15:56:36 | 显示全部楼层

基本的Arduino演示程序

在我们的第一个演示中,我们将简单地测试传感器,看它是否工作。示例程序非常简单,它使用串行监视器来显示它检测到的距离,以厘米为单位。我们仔细查看一下。


为了测试超声波距离传感器的精确度,我制作了一个测试电路板,一端安装传感器(我使用魔术贴来安装它)。我在电路板上放了一根1米长的棒,这样我就可以在2-100厘米范围内测试传感器。


如果你想以英寸而不是厘米显示你的结果。有两种方法可以做到这一点:

1.    使用英制值作为声速而不是公制值。在20摄氏度(68华氏度)的海平面上,声音以每秒343米的速度传播,即每秒1125英尺或每秒13500英寸。

2.    保持代码不变,但在最后转换为英寸。一英寸有2.54厘米。这是我个人比较喜欢使用的一种方法,因为它可以让我显示英制和公制值的结果。


否则,只需使用公制系统,它在全世界使用,(更重要的是)它被用于《星际迷航:下一代》(Star Trek: The Next Generation)。如果对皮卡德船长来说足够好,那对我来说就够了。

以下是基本的HC-SR04示例程序,关于它如何工作的细节分析只需要阅读代码中的注释。

  1. /*
  2.   HC-SR04 Basic Demonstration
  3.   HC-SR04-Basic-Demo.ino
  4.   Demonstrates functions of HC-SR04 Ultrasonic Range Finder
  5.   Displays results on Serial Monitor

  6.   DroneBot Workshop 2017
  7.   http://dronebotworkshop.com
  8. */

  9. // This uses Serial Monitor to display Range Finder distance readings

  10. // Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13

  11. #define trigPin 10
  12. #define echoPin 13

  13. float duration, distance;

  14. void setup() {
  15.   Serial.begin (9600);
  16.   pinMode(trigPin, OUTPUT);
  17.   pinMode(echoPin, INPUT);
  18. }

  19. void loop() {
  20.    
  21.   // Write a pulse to the HC-SR04 Trigger Pin
  22.   
  23.   digitalWrite(trigPin, LOW);
  24.   delayMicroseconds(2);
  25.   digitalWrite(trigPin, HIGH);
  26.   delayMicroseconds(10);
  27.   digitalWrite(trigPin, LOW);
  28.   
  29.   // Measure the response from the HC-SR04 Echo Pin

  30.   duration = pulseIn(echoPin, HIGH);
  31.   
  32.   // Determine distance from duration
  33.   // Use 343 metres per second as speed of sound
  34.   
  35.   distance = (duration / 2) * 0.0343;
  36.   
  37.   // Send results to Serial Monitor

  38.   Serial.print("Distance = ");
  39.   if (distance >= 400 || distance <= 2) {
  40.      Serial.println("Out of range");
  41.   }
  42.   else {
  43.     Serial.print(distance);
  44.     Serial.println(" cm");
  45.     delay(500);
  46.   }
  47.   delay(500);
  48. }
复制代码

Arduino代码库

在我们的第一个示例程序中,我们没有使用任何代码库,只是使用Arduino的delayMicrosecond命令创建我们的10 uS脉冲触发,使用pulseIn命令测量接收到的信号脉冲宽度。但是还有其他的方法可以使用特殊的代码库。其中有不少是可用的,最通用的是“NewPing”。


如果您在Arduino示例中没有使用库的经验,这是您真正需要学习的技能。库提供了特定任务的代码功能,并且有几百个库可用于Arduino的任务和支持各种外部硬件。Arduino网站提供了在Arduino IDE中安装新库的说明


NewPing库是由Tim Eckel编写的,它取代了旧的Ping软件库,后者由Caleb Zulawski编写,主要为Parallax Ping超声波传感器设计的(但是如果您在3引脚模式下使用HC-SR04,也是可以正常工作) 。


NewPing库是相当先进的,它大大改善了我们原始示例的准确性。它同时支持多达15个超声波传感器,可以以厘米、英寸或时间间隔直接输出。


以下是使用NewPing库的示例:

  1. /*
  2.   HC-SR04 NewPing Library Demonstration
  3.   HC-SR04-NewPing.ino
  4.   Demonstrates functions of NewPing Library for HC-SR04 Ultrasonic Range Finder
  5.   Displays results on Serial Monitor

  6.   DroneBot Workshop 2017
  7.   http://dronebotworkshop.com
  8. */

  9. // This uses Serial Monitor to display Range Finder distance readings

  10. // Include NewPing Library
  11. #include "NewPing.h"

  12. // Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
  13. // Maximum Distance is 400 cm

  14. #define TRIGGER_PIN  10
  15. #define ECHO_PIN     13
  16. #define MAX_DISTANCE 400

  17. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

  18. float distance;

  19. void setup() {
  20.   Serial.begin (9600);
  21. }

  22. void loop() {
  23.    
  24.   distance = sonar.ping_cm();
  25.   
  26.   // Send results to Serial Monitor
  27.   Serial.print("Distance = ");
  28.   if (distance >= 400 || distance <= 2) {
  29.     Serial.println("Out of range");
  30.   }
  31.   else {
  32.     Serial.print(distance);
  33.     Serial.println(" cm");
  34.     delay(500);
  35.   }
  36.   delay(500);
  37. }
复制代码

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


以下示例以持续时间模式使用NewPing库,,而不是距离模式。

  1. /*
  2.   HC-SR04 NewPing Duration Demonstration
  3.   HC-SR04-NewPing-Duration.ino
  4.   Demonstrates using Duration function of NewPing Library for HC-SR04 Ultrasonic Range Finder
  5.   Displays results on Serial Monitor

  6.   DroneBot Workshop 2017
  7.   http://dronebotworkshop.com
  8. */

  9. // This uses Serial Monitor to display Range Finder distance readings

  10. // Include NewPing Library
  11. #include "NewPing.h"

  12. // Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
  13. // Maximum Distance is 400 cm

  14. #define TRIGGER_PIN  10
  15. #define ECHO_PIN     13
  16. #define MAX_DISTANCE 400

  17. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

  18. float duration, distance;

  19. void setup() {
  20.   Serial.begin (9600);
  21. }

  22. void loop() {
  23.    
  24.   duration = sonar.ping();
  25.   
  26.   // Determine distance from duration
  27.   // Use 343 metres per second as speed of sound
  28.   
  29.   distance = (duration / 2) * 0.0343;
  30.   
  31.   // Send results to Serial Monitor
  32.   Serial.print("Distance = ");
  33.   if (distance >= 400 || distance <= 2) {
  34.     Serial.println("Out of range");
  35.   }
  36.   else {
  37.     Serial.print(distance);
  38.     Serial.println(" cm");
  39.     delay(500);
  40.   }
  41.   delay(500);
  42. }
复制代码

NewPing的另一个功能是“迭代”。迭代意味着不止一次地重复某些事情,这正是迭代模式所做的。 它需要很多的持续时间测量,而不是一个,扔掉任何无效的读数,然后平均剩下的。 默认情况下,它需要5个读数,但实际上你可以指定任意数量。


在这里,我们再次写一个NewPing示例来使用迭代。 正如你所看到的,它与前面的示例几乎相同,添加的是一个变量来指定迭代次数。

  1. /*
  2.   HC-SR04 NewPing Iteration Demonstration
  3.   HC-SR04-NewPing-Iteration.ino
  4.   Demonstrates using Iteration function of NewPing Library for HC-SR04 Ultrasonic Range Finder
  5.   Displays results on Serial Monitor

  6.   DroneBot Workshop 2017
  7.   http://dronebotworkshop.com
  8. */

  9. // This uses Serial Monitor to display Range Finder distance readings

  10. // Include NewPing Library
  11. #include "NewPing.h"

  12. // Hook up HC-SR04 with Trig to Arduino Pin 10, Echo to Arduino pin 13
  13. // Maximum Distance is 400 cm

  14. #define TRIGGER_PIN  10
  15. #define ECHO_PIN     13
  16. #define MAX_DISTANCE 400

  17. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

  18. float duration, distance;

  19. int iterations = 5;

  20. void setup() {
  21.   Serial.begin (9600);
  22. }

  23. void loop() {
  24.    
  25.   duration = sonar.ping_median(iterations);
  26.   
  27.   // Determine distance from duration
  28.   // Use 343 metres per second as speed of sound
  29.   
  30.   distance = (duration / 2) * 0.0343;
  31.   
  32.   // Send results to Serial Monitor
  33.   Serial.print("Distance = ");
  34.   if (distance >= 400 || distance <= 2) {
  35.     Serial.println("Out of range");
  36.   }
  37.   else {
  38.     Serial.print(distance);
  39.     Serial.println(" cm");
  40.     delay(500);
  41.   }
  42.   delay(500);
  43. }
复制代码
回复

使用道具 举报

风筝
发表于: 2018-2-3 16:02:28 | 显示全部楼层

改进准确性

HC-SR04相当准确,这种基本形式对机器人、入侵者检测或接近报警非常有用。但有些时候您可能需要更高的准确性,例如您可能正在构建一个测量工具,或者可能正在使用您的机器人来绘制房间的边界。如果是这样的话,您可以做一些事情来改善HC-SR04的准确性。


正如我在上节所提到的,NewPing库已经实施了许多内部技术来提高传感器的准确性。在大多数情况下,你只需要这些来提升准确性。


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


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

HC-SR04-DHT22-e1504971149299.jpg


DHT22需要一些代码库才能正常工作,Adafruit有两个库可以同时用于DHT22和DHT11。 Adafruit AM2315库和Adafruit Unified Sensor库都可以在Arduino IDE中使用库管理器直接安装。

在安装Adafruit库后,我写了一个快速测试的示例程序,以确保DHT22工作正常。

  1. /*
  2.   DHT22 Basic Demonstration
  3.   DHT22-Basic-Demo.ino
  4.   Demonstrates functions of DHT22 Digital Temperature & Humidity Sensor
  5.   Displays results on Serial Monitor

  6.   DroneBot Workshop 2017
  7.   http://dronebotworkshop.com
  8. */

  9. // Include DHT Libraries from Adafruit
  10. // Dependant upon Adafruit_Sensors Library

  11. #include "DHT.h";

  12. // Define Constants

  13. #define DHTPIN 7     // DHT-22 Output Pin connection
  14. #define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)

  15. // Initialize DHT sensor for normal 16mhz Arduino

  16. DHT dht(DHTPIN, DHTTYPE);


  17. // Define Variables

  18. float hum;  //Stores humidity value
  19. float temp; //Stores temperature value

  20. void setup()
  21. {
  22.   Serial.begin(9600);
  23.   dht.begin();
  24. }

  25. void loop()
  26. {
  27.     delay(2000);  // Delay so sensor can stabalize
  28.    
  29.     hum = dht.readHumidity();  // Get Humidity value
  30.     temp= dht.readTemperature();  // Get Temperature value
  31.    
  32.     // Print temperature and humidity values to serial monitor
  33.    
  34.     Serial.print("Humidity: ");
  35.     Serial.print(hum);
  36.     Serial.print(" %, Temp: ");
  37.     Serial.print(temp);
  38.     Serial.println(" Celsius");
  39.   
  40. }
复制代码

最后,以下程序中使用DHT22,在温度和湿度两个因素,提高了准确性。

  1. /*
  2.   HC-SR04 with Temp and Humidity Demonstration
  3.   HC-SR04-Temp-Humid-Demo.ino
  4.   Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  5.   With DHT22 Temperature and Humidity Sensor
  6.   Displays results on Serial Monitor

  7.   DroneBot Workshop 2017
  8.   http://dronebotworkshop.com
  9. */

  10. // Include DHT Libraries from Adafruit
  11. // Dependant upon Adafruit_Sensors Library
  12. #include "DHT.h";

  13. // Include NewPing Library
  14. #include "NewPing.h"

  15. // Define Constants

  16. #define DHTPIN 7       // DHT-22 Output Pin connection
  17. #define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
  18. #define TRIGGER_PIN  10
  19. #define ECHO_PIN     13
  20. #define MAX_DISTANCE 400

  21. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

  22. // Define Variables

  23. float hum;    // Stores humidity value in percent
  24. float temp;   // Stores temperature value in Celcius
  25. float duration; // Stores HC-SR04 pulse duration value
  26. float distance; // Stores calculated distance in cm
  27. float soundsp;  // Stores calculated speed of sound in M/S
  28. float soundcm;  // Stores calculated speed of sound in cm/ms
  29. int iterations = 5;

  30. // Initialize DHT sensor for normal 16mhz Arduino

  31. DHT dht(DHTPIN, DHTTYPE);

  32. void setup() {
  33.   Serial.begin (9600);
  34.   dht.begin();
  35. }

  36. void loop()
  37. {

  38.   delay(2000);  // Delay so DHT-22 sensor can stabalize
  39.    
  40.     hum = dht.readHumidity();  // Get Humidity value
  41.     temp= dht.readTemperature();  // Get Temperature value
  42.    
  43.     // Calculate the Speed of Sound in M/S
  44.     soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
  45.    
  46.     // Convert to cm/ms
  47.    
  48.     soundcm = soundsp / 10000;
  49.    
  50.   duration = sonar.ping_median(iterations);
  51.   
  52.   // Calculate the distance
  53.   distance = (duration / 2) * soundcm;
  54.   
  55.   // Send results to Serial Monitor
  56.   
  57.     Serial.print("Sound: ");
  58.     Serial.print(soundsp);
  59.     Serial.print(" m/s, ");
  60.     Serial.print("Humid: ");
  61.     Serial.print(hum);
  62.     Serial.print(" %, Temp: ");
  63.     Serial.print(temp);
  64.     Serial.print(" C, ");
  65.     Serial.print("Distance: ");

  66.     if (distance >= 400 || distance <= 2) {
  67.     Serial.print("Out of range");
  68.     }
  69.     else {
  70.     Serial.print(distance);
  71.     Serial.print(" cm");
  72.     delay(500);
  73.     }
  74.   
  75.   Serial.println(" ");
  76. }
复制代码

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


使用3线模式

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


在3线模式下,单个I / O引脚既用作输入又用作输出。这是可能的,因为不会出现同时使用输入和输出的情况。通过消除一个I / O引脚的要求,我们可以保留一个连接到我们的Arduino,并用它来做其他事情。当使用像ATtiny85这样具有有限数量的I / O引脚的芯片时,这也很有用。


下面是我如何将HC-SR04连接到Arduino。

HC-SR04-3-Wire-Mode-e1504971175123.jpg


正如你所看到的,我所做的只是将TriggerEcho连接到Arduino引脚10。

这是我写的使用它的示例程序。请注意,这个示例和前一个示例的唯一区别在于,我已经为TriggerEcho引脚值指定了引脚10。示例的其余部分是相同的。

  1. /*
  2.   HC-SR04 in 3-Wire Mode with Temp and Humidity Demonstration
  3.   HC-SR04-3Wire-Temp-Humid-Demo.ino
  4.   Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  5.   With DHT22 Temperature and Humidity Sensor
  6.   Displays results on Serial Monitor

  7.   DroneBot Workshop 2017
  8.   http://dronebotworkshop.com
  9. */

  10. // Include DHT Libraries from Adafruit
  11. // Dependant upon Adafruit_Sensors Library
  12. #include "DHT.h";

  13. // Include NewPing Library
  14. #include "NewPing.h"

  15. // Define Constants

  16. #define DHTPIN 7       // DHT-22 Output Pin connection
  17. #define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
  18. #define TRIGGER_PIN  10  // Trigger and Echo both on pin 10
  19. #define ECHO_PIN     10
  20. #define MAX_DISTANCE 400

  21. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

  22. // Define Variables

  23. float hum;    // Stores humidity value in percent
  24. float temp;   // Stores temperature value in Celcius
  25. float duration; // Stores HC-SR04 pulse duration value
  26. float distance; // Stores calculated distance in cm
  27. float soundsp;  // Stores calculated speed of sound in M/S
  28. float soundcm;  // Stores calculated speed of sound in cm/ms
  29. int iterations = 5;

  30. // Initialize DHT sensor for normal 16mhz Arduino

  31. DHT dht(DHTPIN, DHTTYPE);

  32. void setup() {
  33.   Serial.begin (9600);
  34.   dht.begin();
  35. }

  36. void loop()
  37. {

  38.   delay(2000);  // Delay so DHT-22 sensor can stabalize
  39.    
  40.     hum = dht.readHumidity();  // Get Humidity value
  41.     temp= dht.readTemperature();  // Get Temperature value
  42.    
  43.     // Calculate the Speed of Sound in M/S
  44.     soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
  45.    
  46.     // Convert to cm/ms
  47.    
  48.     soundcm = soundsp / 10000;
  49.    
  50.   duration = sonar.ping_median(iterations);
  51.   
  52.   // Calculate the distance
  53.   distance = (duration / 2) * soundcm;
  54.   
  55.   // Send results to Serial Monitor
  56.   
  57.     Serial.print("Sound: ");
  58.     Serial.print(soundsp);
  59.     Serial.print(" m/s, ");
  60.     Serial.print("Humid: ");
  61.     Serial.print(hum);
  62.     Serial.print(" %, Temp: ");
  63.     Serial.print(temp);
  64.     Serial.print(" C, ");
  65.     Serial.print("Distance: ");

  66.     if (distance >= 400 || distance <= 2) {
  67.     Serial.print("Out of range");
  68.     }
  69.     else {
  70.     Serial.print(distance);
  71.     Serial.print(" cm");
  72.     delay(500);
  73.     }
  74.   
  75.   Serial.println(" ");
  76. }
复制代码
回复

使用道具 举报

风筝
发表于: 2018-2-3 16:16:00 | 显示全部楼层

使用多个HC-SR04传感器

在许多应用中,您需要在设计中使用多个HC-SR04超声波传感器。当您想要监视从机器人的不同侧面到外部物体的距离时,可能会出现这种需求。其中两个可用于前后传感器,或连接6个,并监视周围的每一侧 - 这取决于你!


当使用多个传感器时,一个明显的考虑是您需要保持由一个传感器发出的信号被另一个传感器采集和测量。最简单的方法是只需要在一个传感器上触发触发器,然后等到您接收到回波后再继续下一个传感器。一个明智的做法是在两次读取保留一段短延迟,以防上一次的传感器声波仍然在房间里跳动。


以下是我如何使用Arduino连接两个HC-SR04超声波传感器的方法。请注意,我使用三线模式进行连接,如果您愿意,也可以用传统的四线方式连接它们。如果你那样做,那么只需修改示例程序来指定不同的触发和回声引脚。

HC-SR04-Two_Sensor-e1504971202818.jpg


以下示例程序使用NewPing库和两个传感器。 与其他所有的示例程序一样,它将结果输出到串行监视器。

  1. /*
  2.   Dual HC-SR04 with Temp and Humidity Demonstration
  3.   HC-SR04-Temp-Humid-Dual-Demo.ino
  4.   Demonstrates enhancements of HC-SR04 Ultrasonic Range Finder
  5.   With DHT22 Temperature and Humidity Sensor
  6.   Displays results on Serial Monitor
  7. */

  8. // Include DHT Libraries from Adafruit
  9. // Dependant upon Adafruit_Sensors Library
  10. #include "DHT.h";

  11. // Include NewPing Library
  12. #include "NewPing.h"

  13. // Define Constants

  14. #define DHTPIN 7       // DHT-22 Output Pin connection
  15. #define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
  16. #define TRIGGER_PIN_1  10
  17. #define ECHO_PIN_1     10
  18. #define TRIGGER_PIN_2  5
  19. #define ECHO_PIN_2     5
  20. #define MAX_DISTANCE 400

  21. NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
  22. NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);

  23. // Define Variables

  24. float hum;    // Stores humidity value in percent
  25. float temp;   // Stores temperature value in Celcius
  26. float duration1; // Stores First HC-SR04 pulse duration value
  27. float duration2; // Stores Second HC-SR04 pulse duration value
  28. float distance1; // Stores calculated distance in cm for First Sensor
  29. float distance2; // Stores calculated distance in cm for Second Sensor
  30. float soundsp;  // Stores calculated speed of sound in M/S
  31. float soundcm;  // Stores calculated speed of sound in cm/ms
  32. int iterations = 5;

  33. // Initialize DHT sensor for normal 16mhz Arduino

  34. DHT dht(DHTPIN, DHTTYPE);

  35. void setup() {
  36.   Serial.begin (9600);
  37.   dht.begin();
  38. }

  39. void loop()
  40. {

  41.   delay(1000);  // Delay so DHT-22 sensor can stabalize
  42.    
  43.     hum = dht.readHumidity();  // Get Humidity value
  44.     temp= dht.readTemperature();  // Get Temperature value
  45.    
  46.     // Calculate the Speed of Sound in M/S
  47.     soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
  48.    
  49.     // Convert to cm/ms
  50.    
  51.     soundcm = soundsp / 10000;
  52.         
  53.   // Measure duration for first sensor
  54.    
  55.   duration1 = sonar1.ping_median(iterations);
  56.   
  57.   // Add a delay between sensor readings
  58.   
  59.   delay(1000);
  60.   
  61.   // Measure duration for first sensor
  62.   
  63.   duration2 = sonar2.ping_median(iterations);
  64.   
  65.   // Calculate the distances for both sensors
  66.   
  67.   distance1 = (duration1 / 2) * soundcm;
  68.   distance2 = (duration2 / 2) * soundcm;
  69.   
  70.   // Send results to Serial Monitor
  71.   
  72.     Serial.print("Distance 1: ");

  73.     if (distance1 >= 400 || distance1 <= 2) {
  74.     Serial.print("Out of range");
  75.     }
  76.     else {
  77.     Serial.print(distance1);
  78.     Serial.print(" cm ");
  79.     }
  80.    
  81.     Serial.print("Distance 2: ");

  82.     if (distance2 >= 400 || distance2 <= 2) {
  83.     Serial.print("Out of range");
  84.     }
  85.     else {
  86.     Serial.print(distance2);
  87.     Serial.print(" cm");
  88.     }
  89.   
  90.   Serial.println(" ");
  91. }
复制代码

善待动物!

当设计一个带有超声波发射器的设备时,如HC-SR04,需要考虑的一件事是,许多动物可以听到超声波的声音。根据维基百科的介绍,这当中包括狗、猫、沙鼠和兔子。


如果在你的房子里养着毛茸茸的四条腿的朋时,你可能想避免让他们听到传感器发出的声音,因为这可能是非常烦人的。想象一下,每秒钟都会听到一连串的“哔哔声”,你可以体会到你的宠物可能会经历什么。


还有其他一些测量距离的方法,比如红外线和激光雷达,这些方法不涉及超声波。如果你需要考虑家里的动物,那么你可能要考虑一下这些方式。一般来说,将Rover或Fifi放在车间内是一个不错的主意!


继续

正如您所看到的,HC-SR04超声波距离传感器是一种便宜而实用的设备,可用于无数的应用。如果您正在建造一个机器人,这是您工具箱中的一个重要组件。


希望你已经发现这篇文章是有用的。如果您对HC-SR04或我在这里提供的示例程序有任何疑问,请将它们写在下面的评论部分。如果你想出一个基于HC-SR04的项目,我很乐意听到。


参考链接

◾    本文中使用的所有的示例代码:HC-SR04示例

◾    Arduino官方网站上描述的NewPing库:Arduino网站NewPing库相关的文章

◾    GitHub上的NewPing库(在这里获取最新版本):GitHub上的NewPing库

◾    GitHub上的Adafruit DHT22库。你也可以从Arduino IDE的库管理器进行安装:Adafruit DHT22库

◾    GitHub上的Adafruit统一传感器库。你仍然可以从库管理器进行安装:Adafruit统一传感器库

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-20 13:24 , Processed in 0.150455 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表