There are two types of sound sensors, the first type is a ‘knock detector’ – this sensor connects the microphone to a comparator ic and when the sound level exceeds a certain level the Digital output will go High. The second type, shown above, has the same digital output but the analogue output from the microphone is also available.
The second type has four pins AO is the analogue output, G or Gnd, + or Vcc connects to 5volts and DO which is the Digital Output. The Digital Output will either be HIGH or LOW.
The following sketch only uses the Analogue Output to produce a Sound Meter. Six LEDs are use to show the output. The Value of the sound level, together with maximum and minimum values can be seen on the Serial Monitor.
Sensitivity can be set inside the program, or a potentiometer connected between Gnd and 5 volts with the centre pin connected to an analogue port, could be used to set the sensitivity.
/* Sound Sensor Used as a sound meter
needs a sound sensor with an Analogue Output
connect Gnd to Arduino Gnd
connect + to Arduino 5 volts
connect AO to Arduino Analogue Pin A0
*/
int mic=A0; // connect AO to Analogue Pin A0
float sensorvalue=0,lastsensorvalue=0,lastminsensorvalue=1024;
int i;
int led[]={11,12,7,6,5,4};
float val;
//
void setup()
{
Serial.begin(9600);
for (int pin=0; pin < 6; pin++)
{
pinMode(led[pin], OUTPUT);
}
for (int colu=2; colu < 6; colu++)
{
digitalWrite(led[colu], HIGH);
}
//
// light LEDs 1 and 2 up
digitalWrite(led[0], HIGH);
digitalWrite(led[1], HIGH);
}
//
void loop()
{
// Set sensitivity
// this variable can be changed to potentiometer input
//
val=130;
sensorvalue=analogRead(mic);
sensorvalue=sensorvalue/val;
//
// Get MAX
//
if (sensorvalue > lastsensorvalue)
{
lastsensorvalue=sensorvalue;
}
//
// Get MIN
//
if( sensorvalue < lastminsensorvalue)
{
lastminsensorvalue=sensorvalue;
}
//
Serial.print("MAX: ");
Serial.print(lastsensorvalue);
Serial.print(" ");
Serial.print("LAST: ");
Serial.print(sensorvalue);
Serial.print(" ");
Serial.print("MIN: ");
Serial.print(" ");
Serial.print(lastminsensorvalue);
Serial.print("Sensitivity: ");
Serial.println(val);
delay(1);
//
// Clear led matrix
//
if (sensorvalue == lastminsensorvalue)
{
for(int i=6;i>1;i--)
{
digitalWrite(led[i], HIGH);
delay(130);
}
}
else
{
for (i=2;i<sensorvalue; i++)
//turn on the leds up to volume level
{
digitalWrite(led[i], LOW);
delay(1);
}
for(i=i;i<6;i++)
// turn off the leds above the voltage level
{
digitalWrite(led[i], HIGH);
}
}
}