user27416: 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 > 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.

user37523: Based on your code, it looks like you are trying to use the analog signal to obtain a digital output (turn an LED on or off). The sound sensor board does this for you and has a digital output pin called "D0". 
Heres a simple sketch using the serial monitor to show you what the sensor "D0" pin is putting out. Connect ground and 5 volts as you did before, and connect sensor D0 to Arduino pin 3.
const byte DigitalInputPin = 3;

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.print("Digital Pin = " );
  Serial.println(digitalRead(DigitalInputPin));
  delay(50);
}

You don't have to connect the sensor to an Arduino to adjust it or test it. There are 2 LEDs on the sensor circuit board. The one closest to the potentiometer will turn on when the D0 pin is high and off when it's low. You use the potentiometer to set the "trigger point" based on the level of sound you want to make the output go high.

user10794: 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 – 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.

27416: <p>I am very new to this community. My first project with Arduino was using a LDR (light dependent resistor) in a circuit like this:</p>

<p><a href="https://i.stack.imgur.com/Rxoi1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rxoi1.jpg" alt="enter image description here"></a></p>

<p>The input I got was as expected between 0 - 1023.</p>

<p>I then tried a similar project this time using a sound sensor like this:</p>

<p><a href="https://i.stack.imgur.com/GiUuC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GiUuC.jpg" alt="enter image description here"></a></p>

<p>My circuit is as follows:</p>

<ol>
<li>A0 on mic to A0 on Arduino</li>
<li>GND to GND</li>
<li>+ve to 5V</li>
<li>LED along with resistor to digital pin 9 on Arduino</li>
</ol>

<p>My program is as follows:</p>

<pre><code>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);
}
</code></pre>

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

<pre><code>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
...
...
</code></pre>

<p>I can't figure out the problem myself so I came here.</p>
 37523: <p>Based on your code, it looks like you are trying to use the analog signal to obtain a digital output (turn an LED on or off). The sound sensor board does this for you and has a digital output pin called "D0". </p>

<p>Heres a simple sketch using the serial monitor to show you what the sensor "D0" pin is putting out. Connect ground and 5 volts as you did before, and connect sensor D0 to Arduino pin 3.</p>

<pre><code>const byte DigitalInputPin = 3;

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.print("Digital Pin = " );
  Serial.println(digitalRead(DigitalInputPin));
  delay(50);
}
</code></pre>

<p>You don't have to connect the sensor to an Arduino to adjust it or test it. There are 2 LEDs on the sensor circuit board. The one closest to the potentiometer will turn on when the D0 pin is high and off when it's low. You use the potentiometer to set the "trigger point" based on the level of sound you want to make the output go high.</p>
 10794: <p>I plotted the values you got in a spreadsheet:</p>

<p><a href="https://i.stack.imgur.com/M1a6r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M1a6r.png" alt="Microphone input"></a></p>

<p>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.</p>

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



<p><img src="https://i.stack.imgur.com/TVylT.png" alt="schematic"></p>



<p><sup><a href="/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2fTVylT.png">simulate this circuit</a> &ndash; Schematic created using <a href="https://www.circuitlab.com/" rel="nofollow">CircuitLab</a></sup></p>

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

<hr>

<p>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.</p>
