风筝
发表于: 2019-9-9 11:46:14 | 显示全部楼层

您是否见过一个机器人项目,带有一副看起来像大卡通眼睛的设备,并想知道那部分是做什么的?有可能你看到的是超声波传感器。在本篇文章中,我们将主要介绍HC-SR04超声波传感器,包括如何使用Arduino连接它来制作电子卷尺。


超声波传感器如何工作?

超声波传感器是使用超声波测量物体距离的装置。超声波换能器发送和接收超高频声波以获得物体的距离或接近度。超高频声波从物体表面反射,形成独特的回波模式。

An HC-SR04 ultrasonic sensor..png

图1. HC-SR04超声波传感器。


图2显示了超声波传感器的超高频声波从物体表面反射。

Sent and reflected waves.png

图2.发送和反射波:超声波传感器的基本操作。


使用TinkerCad电路将超声波传感器连接到Arduino

通过对超声波传感器工作原理的基本了解,您现在可以将设备连接到Arduino。要探索超声波传感器的操作,您可以使用TinkerCad电路制作虚拟功能电路。


TinkerCad Circuits是一款免费的在线电路仿真器,可以在将它们连接到真正的面包板上之前模拟各种电气和电子电路。您甚至可以使用TinkerCad Circuits测试Arduino项目(包括代码)。在制作物理电路之前,您可以通过实验获得宝贵的电子知识。


图3显示了使用TinkerCad Circuits制作的功能超声波传感器Arduino项目。

An online ultrasonic sensor .png

图3.在线超声波传感器和Arduino TinkerCard电路。


如果您有面包板来试验超声波传感器,请使用图4作为参考。

Figure 4. A breadboard wiring.png

图4. TinkerCad Circuits内置的面包板布线版本。


在面包板上用Arduino连接超声波传感器

您可以使用TinkerCad Circuits内置的超声波传感器Arduino电路或图5所示的电气接线图来构建您的传感设备。

Figure 5. The electrical wiring diagram.png

图5. Arduino实际超声波传感器的电气接线图。


如果您使用的是4针超声波传感器,则常闭引脚(NC)接地。您可以如图所示放置面包板上的超声波传感器,并使用跳线完成Arduino的接线。


这是我使用4线跳线将超声波传感器连接到Arduino的电路。


4线跳线线束采用彩色编码。图7显示了Arduino和超声波传感器之间的接线连接。

Figure 7. The 4-wire color-coded jumper.png

图7. 4线彩色编码跳线接线连接。


您现在已成功将超声波传感器连接到Arduino!您现在可以将超声波传感器代码上传到Arduino。


超声波传感器代码

该项目的最后一部分是将超声波传感器代码上传到Arduino。使用USB线将Arduino连接到台式PC或笔记本电脑。在Arduino IDE中,输入如下所示的代码。在IDE中,您可以通过单击水平箭头上载代码。

该草图读取超声波测距仪并返回距离范围内最近的物体的距离。为此,它向传感器发送脉冲以启动读数,然后监听要返回的脉冲。返回脉冲的长度与物体距传感器的距离成比例。

本文使用的完整代码:

  1. // this constant won't change.  It's the pin number
  2. // of the sensor's output:
  3. const int pingPin = 7;

  4. void setup() {
  5.   // initialize serial communication:
  6.   Serial.begin(9600);
  7. }

  8. void loop()
  9. {
  10.   // establish variables for duration of the ping,
  11.   // and the distance result in inches and centimeters:
  12.   long duration, inches, cm;

  13.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  14.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  15.   pinMode(pingPin, OUTPUT);
  16.   digitalWrite(pingPin, LOW);
  17.   delayMicroseconds(2);
  18.   digitalWrite(pingPin, HIGH);
  19.   delayMicroseconds(2);

  20.   digitalWrite(pingPin, LOW);

  21.   // The same pin is used to read the signal from the PING))): a HIGH
  22.   // pulse whose duration is the time (in microseconds) from the sending
  23.   // of the ping to the reception of its echo off of an object.
  24.   pinMode(pingPin, INPUT);
  25.   duration = pulseIn(pingPin, HIGH);

  26.   // convert the time into a distance
  27.   inches = microsecondsToInches(duration);
  28.   inches = inches +2;// Ultrasonic Calibration factor
  29.   cm = microsecondsToCentimeters(duration);
  30.   cm = cm +2; // Ultrasonic Calibration factor

  31.   // Display measured values on the Serial Monitor
  32.   Serial.print(inches);
  33.   Serial.print("in, ");
  34.   Serial.print(cm);
  35.   Serial.print("cm");
  36.   Serial.println();

  37.   delay(1000);
  38. }

  39. long microsecondsToInches(long microseconds)
  40. {
  41.   // According to Parallax's datasheet for the PING))), there are
  42.   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  43.   // second).  This gives the distance travelled by the ping, outbound
  44.   // and return, so we divide by 2 to get the distance of the obstacle.
  45.   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  46.   return microseconds / 74 / 2;
  47. }

  48. // microsecondsToCentimeters function
  49. long microsecondsToCentimeters(long microseconds)
  50. {
  51.   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  52.   // The ping travels out and back, so to find the distance of the
  53.   // object we take half of the distance travelled.
  54.   return microseconds / 29 / 2;
  55. }
复制代码

您会立即在IDE中看到距离数据向下滚动。 图8显示了示例距离测量会话的数据。

Figure 8. An example distance.png

图8.电子卷尺的示例距离测量会话。


在超声波传感器和测量距离的物体之间放置一个标尺。 您的读数与实际测量距离相比有多准确?


测量值应非常接近相同,这意味着您已成功制作了电子卷尺。 您可以使用电子卷尺观察各种物体及其测量距离。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-23 23:11 , Processed in 0.038653 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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