风筝
发表于: 2019-8-17 21:58:38 | 显示全部楼层

在本篇文章中,您将学习如何在Arduino中使用KY-037声音检测传感器。您可以使用此模块测量环境中声音强度的变化。


什么是KY-037声音探测传感器?

该模块由用于检测声音的敏感电容式麦克风和放大器电路组成。该模块的输出可以是模拟值和数字值。数字输出充当开关,当声音强度达到某个阈值时激活。可以通过传感器上的电位器调节灵敏度阈值。

KY037-Arduino_sensor.jpg

模拟输出电压随麦克风接收的声音强度而变化。您可以将此输出连接到Arduino模拟引脚并处理输出电压。


所需的材料

●    Arduino UNO R3开发板

●    KY-037声音检测传感器模块

●    LED光条

●    330欧电阻

●    公对母跳线

●    Arduino IDE

KY037-Arduino-required-materials.jpg


将KY-037声音检测模块与Arduino连接

要将此模块与Arduino一起使用,只需连接模块的电源电压,然后根据需要将其模拟或数字引脚连接到Arduino。


这里我们使用的是模拟输出。

KY037-Arduino-pinout.jpg


电路连接

将传感器连接到Arduino开发板,如下图所示

KY037-Arduino_fritzing-1.jpg


代码

连接电路后,请执行以下步骤:

第1步:在Arduino板上上传以下代码:

  1. void setup() {

  2. Serial.begin(9600); // setup serial

  3. }

  4. void loop() {

  5. Serial.println(analogRead(A0));

  6. delay(100);

  7. }
复制代码

第2步:打开“Serial Monitor”窗口。 现在转动电位器关闭数字输出上的LED。 在LED熄灭后立即记下Serial Monitor中显示的数字。

KY037-Arduino_AnalogRead.png

小提示:在图表上显示传感器的模拟输出

将传感器的模拟输出连接到Arduino A0引脚,并在Arduino板上上传以下代码。 然后从“Tools”菜单中选择“Serial plotter”。

KY037-Arduino_Analog.png

第3步:在下面的代码(作为阈值变量)中写下您之前记下的数字,并将代码上传到开发板上。

  1. /*

  2. KY-037 Sound Detection Sensor + Arduino

  3. modified on 16 Apr 2019

  4. by Mohammadreza Akbari @ Electropeak

  5. https://electropeak.com/learn/

  6. */

  7. int sensor_value = 0;

  8. int threshold = 540; //Enter Your threshold value here

  9. int abs_value = 0;

  10. int ledCount = 10; //number of Bargraph LEDs

  11. int bargraph[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // Bargraph pins

  12. void setup() {

  13. Serial.begin(9600); // setup serial

  14. for (int i = 0; i <= ledCount; i++) // Define bargraph pins OUTPUT

  15. {

  16. pinMode(bargraph[i], OUTPUT);

  17. }

  18. for (int i = 0; i <= 9; i++)

  19. {

  20. digitalWrite(i, LOW);

  21. }

  22. }

  23. void loop() {

  24. sensor_value = analogRead(A0);

  25. abs_value = abs(sensor_value - threshold);

  26. int ledLevel = map(abs_value, 0, (1024 - threshold), 0, ledCount);

  27. for (int i = 0; i < ledCount; i++) {

  28. // if the array element's index is less than ledLevel,

  29. // turn the pin for this element on:

  30. if (i < ledLevel) {

  31. digitalWrite(bargraph[i], HIGH);

  32. Serial.println(i);

  33. }

  34. // turn off all pins higher than the ledLevel:

  35. else {

  36. digitalWrite(bargraph[i], LOW);

  37. }

  38. }

  39. }
复制代码

以上就是本篇文章的全部内容。您可以在本文的基础之上制作一台间谍设备。 当声音达到特定阈值时,设备自动开始存储声音。 如果遇到问题,请随时在本贴下面进行回复。

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

本版积分规则

主题 700 | 回复: 1480



手机版|

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

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

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