The Working of Sound Sensor With Arduino Mega 2560

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.   
  22. void setup()  
  23. {  
  24.     pinMode(soundSensorPin, INPUT);  
  25.     for (int i = 0; i < numberOfPins; i++)  
  26.     {  
  27.         pinMode(LEDPins[i], OUTPUT);  
  28.     }  
  29. }  
  30.   
  31. void loop()  
  32. {  
  33.     soundReading = analogRead(soundSensorPin);  
  34.     if (soundReading > soundThreshold)  
  35.     {  
  36.         if (switcher)  
  37.         {  
  38.             aboveThreshold(currentPin);  
  39.             switcher = true;  
  40.         }  
  41.     } else   
  42.     {  
  43.         if (switcher)   
  44.         {  
  45.             belowThreshold();  
  46.             switcher = true;  
  47.         }  
  48.     }  
  49. }  
  50.   
  51. void aboveThreshold(int cPin)  
  52. {  
  53.     switcher = false;  
  54.     if (intensity[cPin] < 10)   
  55.     {  
  56.         intensity[cPin] = 255;  
  57.         delay(50);  
  58.         currentPin = currentPin + 1;  
  59.     }  
  60.   
  61.     if (currentPin == numberOfPins)   
  62.     {  
  63.         currentPin = 0;  
  64.     }  
  65. }  
  66.   
  67. void belowThreshold()  
  68. {  
  69.     switcher = false;  
  70.     fadeCounter++;  
  71.     if (fadeCounter == fadeDelay)   
  72.     {  
  73.         fadeCounter = 0;  
  74.         for (int i = 0; i < numberOfPins; i++)   
  75.         {  
  76.             analogWrite(LEDPins[i], intensity[i]);  
  77.         }  
  78.         for (int i = 0; i < numberOfPins; i++)  
  79.         {  
  80.             intensity[i]--;  
  81.             if (intensity[i] < 0)  
  82.             {  
  83.                 intensity[i] = 0;  
  84.             }  
  85.         }  
  86.     }  
  87. }  
Output:
 
 
Figure 2: Output
 
Read more articles on Arduino: