I am using a pre-amp circuit and electret mic as a sound sensor. Assuming the circuit is correctly installed, the code must be at fault because the data is not changing more than a -/+ one or two figures. Would you please advise the problem.
int micPin = 0;
int val;
void setup() {
Serial.begin(9600);
pinMode (micPin, INPUT);
}
void loop () {
val = analogRead (micPin);
val = val/5;
Serial.println(val);
delay (500);
First a minor point. You don't need the pinMode (micPin, INPUT);
in the setup function because that is needed only for setting up digital pins. Tha analogRead function handles the configuring the pin mode.
More major point. A audio signal is an AC voltage. A Arduino analog input pin can only handle signals in a 0-+5vdc range, no negitive voltages allowed. So the audio voltage levels would need to be rectified and filtered to be able to give you a decent average level of the audio signal. Also taking one audio sample every 1/2 second is not going to give you any meaning full audio signal level information, just random values.
So lots of problems to do what you are attempting to do. There is much electrical analog signal processing needed to make for a useful measurement.
What is the eventual usage of this signal measurement, that is what to you want to do with the measurement once you have processed it?
It's somewhat easy to built a 'clap detector' type noise switch but anything more envolved using a audio level measuement signal needs some thought and design details.
One thing you migh try is to change the Arduino's analog reference to use the internal 1.1volt reference. This would have the same effect as amplifiying the input signal by X5. A mic with simple preamp usually only outputs around 1 volt max peak.
I am designing a noise control system for our school cafeteria with a series of ten lights, green to red. I assumed that the data could be collected over time, averaged, and then control the lights accordingly.