如何使用Arduino开发板读取KY-037声音检测传感器 转载自 https://www.yiboard.com/thread-1232-1-1.html(留着备用)

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

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

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

<ignore_js_op>

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

所需的材料

●    Arduino UNO R3开发板

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

●    LED光条

●    330欧电阻

●    公对母跳线

●    Arduino IDE

<ignore_js_op>

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

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

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

<ignore_js_op>

电路连接

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

<ignore_js_op>

代码

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

第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中显示的数字。

<ignore_js_op>

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

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

<ignore_js_op>

第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. }

猜你喜欢

转载自www.cnblogs.com/Lonelychampion/p/12204871.html