Introduction

In this article, I will explain the working of a sound sensor with Arduino Mega 2560. When the sound is detected the LED is automatically ON, and then we can easily detect the sound.
Parts:
  • Arduino Mega 2560
  • Sound Sensor
  • BreadBoard
  • HookUp Wires
  • LED-3.
SoundSensor:
Figure 1: Sound Sensor
  • The sound sensor can easily find the sound.
  • It can detect the sound of the environment.
  • They can have the standard of three pins in the sensor.
Connection:
Step 1:
Connection from the Sound Sensor to the Arduino Mega 2560,
  • The Gnd pin can be connected to the Gnd of the Arduino Mega 2560.
  • The Vcc pin can be connected to the Vcc of the Arduino Mega 2560.
  • The Vin pin can be connected to the Analog pin of A1 to the Arduin oMega 2560.
Connection from the LED to the ArduinoMega2560:
First LED connection:
  • Positive pin = 13
  • Negative Pin = Gnd.
Second LED connection:
  • Positive pin = 12
  • Negative Pin = Gnd.
Third LED connection:
  • Postive pin = 11
  • Negative Pin = Gnd
Explanation:
  • The sound is produced and then automatically the LED is ON.
  • It can have the level of noise at places.
Uses:
  • Home
  • School/colleges
  • Forest.....etc
Programming:
  1. int soundSensorPin = A1;
  2. int soundReading = 0;
  3. int soundThreshold = 500;
  4. int intensity[3] =
  5. {
  6. 0,
  7. 0,
  8. 0
  9. };
  10. int LEDPins[3] =
  11. {
  12. 13,
  13. 12,
  14. 11
  15. };
  16. int numberOfPins = 3;
  17. int currentPin = 0;
  18. int fadeCounter = 0;
  19. int fadeDelay = 50;
  20. boolean switcher = true;
  21. void setup()
  22. {
  23. pinMode(soundSensorPin, INPUT);
  24. for (int i = 0; i < numberOfPins; i++)
  25. {
  26. pinMode(LEDPins[i], OUTPUT);
  27. }
  28. }
  29. void loop()
  30. {
  31. soundReading = analogRead(soundSensorPin);
  32. if (soundReading > soundThreshold)
  33. {
  34. if (switcher)
  35. {
  36. aboveThreshold(currentPin);
  37. switcher = true;
  38. }
  39. } else
  40. {
  41. if (switcher)
  42. {
  43. belowThreshold();
  44. switcher = true;
  45. }
  46. }
  47. }
  48. void aboveThreshold(int cPin)
  49. {
  50. switcher = false;
  51. if (intensity[cPin] < 10)
  52. {
  53. intensity[cPin] = 255;
  54. delay(50);
  55. currentPin = currentPin + 1;
  56. }
  57. if (currentPin == numberOfPins)
  58. {
  59. currentPin = 0;
  60. }
  61. }
  62. void belowThreshold()
  63. {
  64. switcher = false;
  65. fadeCounter++;
  66. if (fadeCounter == fadeDelay)
  67. {
  68. fadeCounter = 0;
  69. for (int i = 0; i < numberOfPins; i++)
  70. {
  71. analogWrite(LEDPins[i], intensity[i]);
  72. }
  73. for (int i = 0; i < numberOfPins; i++)
  74. {
  75. intensity[i]--;
  76. if (intensity[i] < 0)
  77. {
  78. intensity[i] = 0;
  79. }
  80. }
  81. }
  82. }
Output:
Figure 2: Output
Read more articles on Arduino: