|
您是否见过一个机器人项目,带有一副看起来像大卡通眼睛的设备,并想知道那部分是做什么的?有可能你看到的是超声波传感器。在本篇文章中,我们将主要介绍HC-SR04超声波传感器,包括如何使用Arduino连接它来制作电子卷尺。
超声波传感器如何工作? 超声波传感器是使用超声波测量物体距离的装置。超声波换能器发送和接收超高频声波以获得物体的距离或接近度。超高频声波从物体表面反射,形成独特的回波模式。
图1. HC-SR04超声波传感器。
图2显示了超声波传感器的超高频声波从物体表面反射。
图2.发送和反射波:超声波传感器的基本操作。
使用TinkerCad电路将超声波传感器连接到Arduino 通过对超声波传感器工作原理的基本了解,您现在可以将设备连接到Arduino。要探索超声波传感器的操作,您可以使用TinkerCad电路制作虚拟功能电路。
TinkerCad Circuits是一款免费的在线电路仿真器,可以在将它们连接到真正的面包板上之前模拟各种电气和电子电路。您甚至可以使用TinkerCad Circuits测试Arduino项目(包括代码)。在制作物理电路之前,您可以通过实验获得宝贵的电子知识。
图3显示了使用TinkerCad Circuits制作的功能超声波传感器Arduino项目。
图3.在线超声波传感器和Arduino TinkerCard电路。
如果您有面包板来试验超声波传感器,请使用图4作为参考。
图4. TinkerCad Circuits内置的面包板布线版本。
在面包板上用Arduino连接超声波传感器 您可以使用TinkerCad Circuits内置的超声波传感器Arduino电路或图5所示的电气接线图来构建您的传感设备。
图5. Arduino实际超声波传感器的电气接线图。
如果您使用的是4针超声波传感器,则常闭引脚(NC)接地。您可以如图所示放置面包板上的超声波传感器,并使用跳线完成Arduino的接线。
这是我使用4线跳线将超声波传感器连接到Arduino的电路。
4线跳线线束采用彩色编码。图7显示了Arduino和超声波传感器之间的接线连接。
图7. 4线彩色编码跳线接线连接。
您现在已成功将超声波传感器连接到Arduino!您现在可以将超声波传感器代码上传到Arduino。
超声波传感器代码 该项目的最后一部分是将超声波传感器代码上传到Arduino。使用USB线将Arduino连接到台式PC或笔记本电脑。在Arduino IDE中,输入如下所示的代码。在IDE中,您可以通过单击水平箭头上载代码。 该草图读取超声波测距仪并返回距离范围内最近的物体的距离。为此,它向传感器发送脉冲以启动读数,然后监听要返回的脉冲。返回脉冲的长度与物体距传感器的距离成比例。 本文使用的完整代码: - // this constant won't change. It's the pin number
- // of the sensor's output:
- const int pingPin = 7;
-
- void setup() {
- // initialize serial communication:
- Serial.begin(9600);
- }
-
- void loop()
- {
- // establish variables for duration of the ping,
- // and the distance result in inches and centimeters:
- long duration, inches, cm;
-
- // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
- // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
- pinMode(pingPin, OUTPUT);
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(2);
-
- digitalWrite(pingPin, LOW);
-
- // The same pin is used to read the signal from the PING))): a HIGH
- // pulse whose duration is the time (in microseconds) from the sending
- // of the ping to the reception of its echo off of an object.
- pinMode(pingPin, INPUT);
- duration = pulseIn(pingPin, HIGH);
-
- // convert the time into a distance
- inches = microsecondsToInches(duration);
- inches = inches +2;// Ultrasonic Calibration factor
- cm = microsecondsToCentimeters(duration);
- cm = cm +2; // Ultrasonic Calibration factor
-
- // Display measured values on the Serial Monitor
- Serial.print(inches);
- Serial.print("in, ");
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
-
- delay(1000);
- }
-
- long microsecondsToInches(long microseconds)
- {
- // According to Parallax's datasheet for the PING))), there are
- // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
- // second). This gives the distance travelled by the ping, outbound
- // and return, so we divide by 2 to get the distance of the obstacle.
- // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
- return microseconds / 74 / 2;
- }
-
- // microsecondsToCentimeters function
- long microsecondsToCentimeters(long microseconds)
- {
- // The speed of sound is 340 m/s or 29 microseconds per centimeter.
- // The ping travels out and back, so to find the distance of the
- // object we take half of the distance travelled.
- return microseconds / 29 / 2;
- }
复制代码
您会立即在IDE中看到距离数据向下滚动。 图8显示了示例距离测量会话的数据。
图8.电子卷尺的示例距离测量会话。
在超声波传感器和测量距离的物体之间放置一个标尺。 您的读数与实际测量距离相比有多准确?
测量值应非常接近相同,这意味着您已成功制作了电子卷尺。 您可以使用电子卷尺观察各种物体及其测量距离。 |