风筝
发表于: 2018-11-25 18:03:43 | 显示全部楼层

在本篇文章中,我们将了解HC-SR04超声波传感器的工作原理以及如何将其与Arduino开发板配合使用。


超声波传感器工作原理

超声波传感器以40 000 Hz的频率发射超声波,通过空气传播,如果路径上有物体或障碍物,它将反射回模块。考虑到行程时间和声音的速度,您可以计算出距离。

Ultrasonic-Sensor-top-view.png


HC-SR04超声波模块有4个引脚,接地、VCC、Trig和Echo。模块的接地和VCC引脚需要分别连接到Arduino电路板上的GND和5V引脚,将Trig和Echo引脚连接到Arduino开发板上的任意数字I / O引脚。

Ultrasonic-Sensor-Diagram.png


为了产生超声波,您需要将Trig设置为高电平并持续10μs。这将发出一个8周期的声波脉冲,它将以速度声音传播,并将在Echo引脚中接收。 Echo引脚将输出声波传播的时间,以微秒为单位。

Ultrasonic-Sensor-Cirucit-Schematics-04.png


例如,如果物体距离传感器10厘米,并且声速为340米/秒或0.034厘米/μs,则声波将需要行进约294微秒。但是你从Echo引脚得到的数字将是这个数字的两倍,因为声波需要前进并向后反射。因此,为了获得以cm为单位的距离,我们需要将来自回波引脚的接收行程时间值乘以0.034并将其除以2。

Ultrasonic-Sensor-Equasions.png


所需的组件

本篇文章使用的组件如下所示:

●    超声波传感器HC-SR04

●    Arduino开发板

●    面包板和跳线


源代码

首先,您需要定义Trig和Echo引脚。本例中,使用的是Arduino开发板上的引脚9和10,并且将它们分别命名为trigPinechoPin。然后你还需要一个Long变量,名为“duration”表示你将从传感器获得的旅行时间,以及一个整型变量来表示距离。


在setup函数中,您必须将trigPin定义为输出,将echoPin定义为输入,然后启动串行通信以在串口监视器上显示结果。


loop()函数中,首先必须确保trigPin被清零,因此您必须将该引脚设置为低电平并持续2μs。现在为了产生超声波,我们必须将trigPin设置为高电平并持续10μs。使用pulseIn()函数,您需要读取行程时间,然后将该值放入变量duration中。该函数有2个参数,第一个是echo引脚的名称,第二个参数可以写入HIGH或LOW。本例中,HIGH意味着pulsIn()函数将等待引脚由反弹声波引起的HIGH时它将开始计时,然后当声波结束时它将等待引脚变为低电平停止计时。最后,该函数将以微秒为单位返回脉冲的长度。为了获得距离,我们将持续时间乘以0.034并将其除以2。最后,我们将在串口监视器上打印距离值。

  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. // defines pins numbers
  9. const int trigPin = 9;
  10. const int echoPin = 10;
  11. // defines variables
  12. long duration;
  13. int distance;
  14. void setup() {
  15. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  16. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  17. Serial.begin(9600); // Starts the serial communication
  18. }
  19. void loop() {
  20. // Clears the trigPin
  21. digitalWrite(trigPin, LOW);
  22. delayMicroseconds(2);
  23. // Sets the trigPin on HIGH state for 10 micro seconds
  24. digitalWrite(trigPin, HIGH);
  25. delayMicroseconds(10);
  26. digitalWrite(trigPin, LOW);
  27. // Reads the echoPin, returns the sound wave travel time in microseconds
  28. duration = pulseIn(echoPin, HIGH);
  29. // Calculating the distance
  30. distance= duration*0.034/2;
  31. // Prints the distance on the Serial Monitor
  32. Serial.print("Distance: ");
  33. Serial.println(distance);
  34. }
复制代码

如果要在LCD上显示HC-SR04超声波传感器的结果,可以使用以下源代码:

  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  9. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
  10. const int trigPin = 9;
  11. const int echoPin = 10;
  12. long duration;
  13. int distanceCm, distanceInch;
  14. void setup() {
  15. lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  16. pinMode(trigPin, OUTPUT);
  17. pinMode(echoPin, INPUT);
  18. }
  19. void loop() {
  20. digitalWrite(trigPin, LOW);
  21. delayMicroseconds(2);
  22. digitalWrite(trigPin, HIGH);
  23. delayMicroseconds(10);
  24. digitalWrite(trigPin, LOW);
  25. duration = pulseIn(echoPin, HIGH);
  26. distanceCm= duration*0.034/2;
  27. distanceInch = duration*0.0133/2;
  28. lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
  29. lcd.print("Distance: "); // Prints string "Distance" on the LCD
  30. lcd.print(distanceCm); // Prints the distance value from the sensor
  31. lcd.print(" cm");
  32. delay(10);
  33. lcd.setCursor(0,1);
  34. lcd.print("Distance: ");
  35. lcd.print(distanceInch);
  36. lcd.print(" inch");
  37. delay(10);
  38. }
复制代码

以上就是本篇文章的全部内容。如果遇到任何问题,请随时在本帖下面进行回复,我会及时回复。

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

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-16 21:30 , Processed in 0.124892 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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