Sound Sensor not working

I've added a sound sensor and LED that lights up when it hears a sound. I've tried the sound sensor code only and it works but when i add the code to the others, it stops working.

#include <dht.h>
#include <Servo.h>

#define DHT11_PIN 2 
#define DHTTYPE DHT11

Servo servo;
dht DHT;
//Servo
int moto = 8;
int angle = 0;
int tempe = 40;

//Sound
int soundpin=6;
int ledpin=7;

void setup() {
  // put your setup code here, to run once: 
  //Sound
  pinMode(ledpin,OUTPUT);
  pinMode(soundpin,INPUT);
  
  Serial.begin(9600);
  servo.attach(moto);

}

void loop() {
  // put your main code here, to run repeatedly:
  //Sound Sensor
  int statusSensor = digitalRead(soundpin);
  if(statusSensor == 1)
  {
    digitalWrite(ledpin,HIGH);
  }
  else{
    digitalWrite(ledpin,LOW);
  }
  //Humidity Sensor
  int chk = DHT.read11(DHT11_PIN);
  
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(2000);
  
  //Servo Motor 
  if(DHT.temperature >= tempe)
  {
    for(angle = 10; angle < 180; angle++)
    {
      servo.write(angle);
      delay(10);
    }
  }
}

What are the spec of the sound sensor ?

it doesn't say on the set but this is the red one with 4 pins.

Try turning on some kind of sound and measureing the digital input pin with a voltmeter.
You may have to set the adjustable pot to get a 1 one out of it. You could also
read it with an adc using analog input should go from 0 to 1023. Use Serial.println statements.

You have a 2 second delay! what if the sound happens during that time (HINT : NOTHING).

Does your sensor have a sensitivity knob? You may need to tune the sensitivity.

Does your sensor have an indicator light? If not you can use this sketch to test the output:

const int soundpin=6;
const int ledpin=7; // or use LED_BUILTIN
void setup() 
{
  pinMode(soundpin, INPUT);
  pinMode(ledpin, OUTPUT);
}
void loop() 
{
  digitalWrite(ledpin, digitalRead(soundpin));
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.