风筝
发表于: 2019-9-23 10:48:27 | 显示全部楼层

在本篇文章中,您将了解超声波模块及其工作原理。通过本文的学习,您将能够将其连接到Arduino开发板,然后将其用作运动检测器,以通过手势控制显示器。


超声波模块基础知识

超声波传感器是接近传感器。该传感器通过以高于人的听觉范围的频率发送短音频脉冲,并在物体表面反射后接收它,来计算空气中超声波的时间和方向。使用此时间,可以高分辨率测量距离。超声波法由于具有高精度和高速度,不与物体接触,无腐蚀且价格合理等优点,是测量距离和检测物体的最佳方法之一。大多数超声波模块包括一个发射器和一个接收器。 SRF04和SRF05模块的接收器和发送器彼此相邻,可以准确检测2到300 cm范围内的物体。 SRF05模块是SRF04的改进版本。该版本支持双模式,并包含5个引脚。但是SRF04模块具有4个引脚,并且仅支持一种模式。这两个模块都有一个称为mode1的共同模式。

motion-modules.jpg


首先在模式1中,应向触发引脚施加至少10微秒的脉冲。模块将自动以40 kHz的频率发送8个脉冲,并将Echo引脚设置为1。Echo引脚保持高电平,直到模块接收到来自物体的反射脉冲。然后,我们可以通过测量Echo引脚处于高逻辑电平的时间来计算发送和接收信号之间的时间。在此模式下,SRF05 OUT引脚不连接。

motion-pinout.jpg

motion-d1.jpg


模式2(仅在SRF05模块中可用)对触发和回波信号使用一个引脚。要使用此模式,请将OUT引脚接地,并使用Echo引脚发送和接收Echo脉冲。如前所述,我们可以通过测量信号持续时间来计算距第一个物体的距离。

motion-d2.jpg


所需的组件

●    Arduino LEONARDO开发板

●    HY-SRF05超声波距离传感器模块

●    跳线

motion-required-material.jpg


超声波模块与Arduino的连接

超声波模块和Arduino之间的连接非常简单。只需将Trig和Echo引脚连接到Arduino的两个引脚,然后将这两个引脚引入Arduino。如前所述,Arduino使用这些引脚计算距模块前面第一个物体的距离。另外,不要忘记将模块的VCC和GND连接到Arduino的5V和GND。


电路连接

motion-circuit-1.jpg

代码

  1.    /*   
  2.    * Ultrasonic Sensor HC-SR05/4 and Arduino
  3.     */
  4.     long duration;
  5.     int distance;
  6.    
  7.     const int trig = 9;
  8.     const int echo = 10;

  9.     void setup() {
  10.     pinMode(trig, OUTPUT); // Sets the trigPin as an Output
  11.     pinMode(echo, INPUT); // Sets the echoPin as an Input
  12.     Serial.begin(9600); // Starts the serial communication
  13.     }
  14.     void loop() {
  15.        distance = calculateDistance();
  16. Serial.println(distance);

  17.     }
  18.     int calculateDistance(){
  19.   
  20.   digitalWrite(trig, LOW);
  21.   delayMicroseconds(2);
  22.   // Sets the trigPin on HIGH state for 10 micro seconds
  23.   digitalWrite(trig, HIGH);
  24.   delayMicroseconds(10);
  25.   digitalWrite(trig, LOW);
  26.   duration = pulseIn(echo, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  27.   distance= duration*0.034/2;
  28.   return distance;
  29. }
复制代码

让我们仔细看一下代码:

  1. digitalWrite(trigPin, HIGH);
  2. delayMicroseconds(10);
  3. digitalWrite(trigPin, LOW);
复制代码

在Trig引脚上发送10微秒的脉冲。

  1. duration = pulseIn(echoPin, HIGH);
复制代码

pulseIn()函数计算Echo引脚为高电平的时间,该时间实际上等于发送和接收信号之间的时间。

  1. distance= duration*0.034/2;
复制代码

通过将duration和声音速度相乘,可以计算声音传播的距离。


使用Arduino和超声波模块控制显示器

假设您的屏幕上正在播放视频。我们仅通过手势控制视频的播放或停止、音量以及向前和向后播放。


为此,将两个超声波模块连接到Arduino Leonardo开发板上,然后将模块放在显示器的左右两侧。


将以下代码上传到您的开发板上。现在,您可以通过将双手放在距离模块20到30厘米的位置来停止或播放视频!


如果将右手放在右模块前面9到15厘米的位置,并将其稍微移开,视频将返回5秒钟。如果您将手稍微靠近模块,视频将向前播放5秒钟。通过将左手放在距模块9至15cm的位置并靠近模块,视频的音量将增加,而将手移开,视频的音量将减小。


电路

motion-circuit-2.jpg


代码

此部分的代码需要用到Keyboard.h库。您可以从以下链接下载:https://www.arduinolibraries.info/libraries/keyboard

  1. /*
  2.     * Control Your PC with Ultrasonic Sensor HC-SR04 and Arduino
  3. */
  4. #include <Keyboard.h>
  5. long duration;
  6. int distance,distLeft,distRight;

  7. const int trigger1 = 2; //Trigger pin of 1st Sesnor
  8. const int echo1 = 3; //Echo pin of 1st Sesnor
  9. const int trigger2 = 4; //Trigger pin of 2nd Sesnor
  10. const int echo2 = 5;//Echo pin of 2nd Sesnor

  11. void setup() {
  12. Serial.begin(9600);
  13.   
  14. pinMode(trigger1, OUTPUT);
  15. pinMode(echo1, INPUT);
  16. pinMode(trigger2, OUTPUT);
  17. pinMode(echo2, INPUT);
  18. }

  19. void loop() {
  20. distance=calculateDistance(trigger1,echo1);
  21. distLeft =distance;

  22. distance=calculateDistance(trigger2,echo2);
  23. distRight =distance;

  24. //Pause Modes -Hold
  25. if ((distLeft >20 && distRight>20) && (distLeft <30 && distRight<30)) //Detect both hands
  26. {Serial.println("Play/Pause");
  27.                         Keyboard.press(KEY_TAB);
  28.                         delay(100);
  29.                         Keyboard.releaseAll();
  30.   delay (500);}

  31. distance=calculateDistance(trigger1,echo1);
  32. distLeft =distance;

  33. distance=calculateDistance(trigger2,echo2);
  34. distRight =distance;

  35. //Control Modes
  36. //Lock Left - Control Mode
  37. if (distLeft>=9 && distLeft<=14)
  38. {
  39.   delay(100); //Hand Hold Time
  40. distance=calculateDistance(trigger1,echo1);
  41.   distLeft =distance;
  42.   if (distLeft>=0 && distLeft<=15)
  43.   {
  44.     Serial.println("Left Hand detected");
  45.     while(distLeft<=20)
  46.     {
  47.      distance=calculateDistance(trigger1,echo1);
  48.       distLeft =distance;
  49.       if (distLeft<5) //Hand pushed in
  50.       {Serial.println ("Volume Up");
  51.                          Keyboard.press(KEY_UP_ARROW); //up  key
  52.                          delay(100);
  53.                          Keyboard.releaseAll();
  54.         delay (300);}
  55.       if (distLeft>17) //Hand pulled out
  56.       {Serial.println ("Volume Down");
  57.        Keyboard.press(KEY_DOWN_ARROW); //down  key
  58.                          delay(100);
  59.                          Keyboard.releaseAll();
  60.        delay (300);}
  61.     }
  62.   }
  63. }

  64. //Lock Right - Control Mode
  65. if (distRight>=9 && distRight<=14)
  66. {
  67.   delay(100); //Hand Hold Time
  68. distance=calculateDistance(trigger2,echo2);
  69.   distRight =distance;
  70.   if (distRight>=0 && distRight<=20)
  71.   {
  72.     Serial.println("Right Hnaad Detected");
  73.     while(distRight<=20)
  74.     {
  75.       distance=calculateDistance(trigger2,echo2);
  76.       distRight =distance;
  77.       if (distRight<5) //Right hand pushed in
  78.       {Serial.println ("Rewind");
  79.        Keyboard.press(KEY_LEFT_ARROW); //left key
  80.                          delay(100);
  81.                          Keyboard.releaseAll();
  82.        delay (300);
  83.       }
  84.       if (distRight>17) //Right hand pulled out
  85.       {Serial.println ("Forward");
  86.        Keyboard.press(KEY_RIGHT_ARROW); //right  key
  87.                          delay(100);
  88.                          Keyboard.releaseAll();
  89.        delay (300);}
  90.   }
  91. }
  92. }

  93. delay(200);
  94. }

  95. int calculateDistance(int trig, int echo){
  96.   
  97.   digitalWrite(trig, LOW);
  98.   delayMicroseconds(2);
  99.   // Sets the trigPin on HIGH state for 10 micro seconds
  100.   digitalWrite(trig, HIGH);
  101.   delayMicroseconds(10);
  102.   digitalWrite(trig, LOW);
  103.   duration = pulseIn(echo, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  104.   distance= duration*0.034/2;
  105. distance = 50;
  106. return distance;
  107. }
复制代码

以上就是本篇文章的全部内容。接下来,您可以在模块上添加一个伺服电机,尝试计算与超声波模块周围每个物体的距离。或者尝试定义其他手势,例如更改视频播放速度。如果遇到问题,请随时在本帖下面进行回复。

跳转到指定楼层
回复

使用道具 举报

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

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-17 06:10 , Processed in 0.144304 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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