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 was 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.