Problem with simple sound detection circuit using a microphone I am very new to this community. My first project with Arduino was using a LDR (light dependent resistor) in a circuit like this:



The input I got was as expected between 0 - 1023.

I then tried a similar project this time using a sound sensor like this:



My circuit is as follows:


A0 on mic to A0 on Arduino
GND to GND
+ve to 5V
LED along with resistor to digital pin 9 on Arduino


My program is as follows:

const int analogInPin = A0; 
int sensorValue = 0;            

void setup() {
  pinMode(analogInPin, INPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  if(sensorValue &gt; 900)
    digitalWrite(9,HIGH);
  else
  digitalWrite(9,LOW);
}


The problem is the serial monitor gives a weird reading, randomly giving 1023 and 0 after regular intervals.

sensor = 124
sensor = 0
sensor = 1023
sensor = 39
sensor = 352
sensor = 1023
sensor = 142
sensor = 0
sensor = 1023
sensor = 44
sensor = 338
sensor = 1023
sensor = 163
sensor = 0
...
...


I can't figure out the problem myself so I came here.
I plotted the values you got in a spreadsheet:



You must admit that looks a bit like a sound wave! If you want to know if it is a soft sound or a loud sound you really need to see the maximum values (1023 in your case). If the maximum was 512 then clearly the incoming sound isn't as loud.

An alternative, if you just want loudness would be to filter the incoming signal, eg.







simulate this circuit &ndash; Schematic created using CircuitLab

This would smooth the incoming voltage so that consecutive readings would be in a narrower range.



Warning: Judging by the graph, you may be getting negative input into A0. The Arduino pins are not designed for negative voltage. You may want to use a diode to eliminate the negative component.