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:
- int soundSensorPin = A1;
- int soundReading = 0;
- int soundThreshold = 500;
- int intensity[3] =
- {
- 0,
- 0,
- 0
- };
- int LEDPins[3] =
- {
- 13,
- 12,
- 11
- };
- int numberOfPins = 3;
- int currentPin = 0;
- int fadeCounter = 0;
- int fadeDelay = 50;
- boolean switcher = true;
-
- void setup()
- {
- pinMode(soundSensorPin, INPUT);
- for (int i = 0; i < numberOfPins; i++)
- {
- pinMode(LEDPins[i], OUTPUT);
- }
- }
-
- void loop()
- {
- soundReading = analogRead(soundSensorPin);
- if (soundReading > soundThreshold)
- {
- if (switcher)
- {
- aboveThreshold(currentPin);
- switcher = true;
- }
- } else
- {
- if (switcher)
- {
- belowThreshold();
- switcher = true;
- }
- }
- }
-
- void aboveThreshold(int cPin)
- {
- switcher = false;
- if (intensity[cPin] < 10)
- {
- intensity[cPin] = 255;
- delay(50);
- currentPin = currentPin + 1;
- }
-
- if (currentPin == numberOfPins)
- {
- currentPin = 0;
- }
- }
-
- void belowThreshold()
- {
- switcher = false;
- fadeCounter++;
- if (fadeCounter == fadeDelay)
- {
- fadeCounter = 0;
- for (int i = 0; i < numberOfPins; i++)
- {
- analogWrite(LEDPins[i], intensity[i]);
- }
- for (int i = 0; i < numberOfPins; i++)
- {
- intensity[i]--;
- if (intensity[i] < 0)
- {
- intensity[i] = 0;
- }
- }
- }
- }
Output:
Figure 2: Output
Read more articles on Arduino: