Using sound sensor to serial print high,low

Hey new here and i’m currently undergoing a project for school. Have just bought sound sensors for a bigger project that i wish to start however want to make sure that the sound sensor is working and test the values that i can expect to be able to sense. There is something wrong with my code and will not print any values in the serial monitor. Any help would be greatly appreciated.

int soundSensor = 0;
int threshhold = 500;

void setup() {
Serial.begin(9600);
pinMode(soundSensor, INPUT);

}

void loop() {

Serial.println(digitalRead(soundSensor));

if (digitalRead(soundSensor) <500){

digitalWrite( LOW );
}

else{

digitalWrite ( HIGH );

}

}

 if (digitalRead(soundSensor) <500){

Digital read returns a 0 (LOW) or a 1 (HIGH) so the if statement is always true.

digitalWrite( LOW );

The syntax for digitalWrite is digitalWrite(pin, level), level being LOW or HIGH.

Please post a data sheet (or at least a photo) of your sound sensor.

Please read the “how to use the forum-please read” stickies to see how to properly format and post code.

You normally connect a sound sensor to an analogue input and use analogRead to return a value.

You normally connect a sound sensor to an analogue input and use analogRead to return a value.

Some that I have seen have an analog comparitor (LM393) that has a pot to set a threshold. They would connect to a digital port. Some have both analog and digital outputs. Remains to be seen what OP is working with.

Yes but

if (digitalRead(soundSensor) <500){

Makes perfect sense with an analogue read but none with a digital one.

digitalWrite( LOW );

Well that won't even compile and it isn't very clear what the OP thought that it would do.

Assuming your sensor has an analog output, run the [u]Analog Read Serial Example[/u] (with the sound sensor instead of the pot) to see what kind of readings you get. That will help you set the threshold.

Most Arduino compatible analog sound sensors are biased at 2.5V so they can read the negative half of the waveform and you'll read about 512 (half the ADC range) with silence. If you wish you can subtract-out the bias to get the true positive & negative readings.

Note that your readings will appear to be somewhat 'random" because you are [u]sampling a wave[/u] that has one positive peak, one negative peaks, and two zero-crossings per cycle, and you don't know where you are sampling it. And with the with the (short) delay in the program you may not "catch" the peak with short duration sounds (if you clap your hands, etc.).