Sound Module Silence

Greetings. I am working on a project where silence needs to be detected for a certain number of seconds (let's say 10) and then it activates a relay. There will be ambient noise like a relay and people talking in a room. When everything goes silent (Or the baseline of silence in the room) the timer starts and activates the relay. If noise is made it starts the timer over again. I am using one of These sound modules.

Currently this is my code to activate the relay (Currently an LED). The problem is everything I search for is on how to detect noise and not silence. I know to use millis but I can't seem to implement it properly. Any help would be appreciated.

const int ledpin = 13;
const int soundpin = A2;
const int threshold = 500;
int sensorValue = 0;
void setup() {
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(soundpin, INPUT);
}
void loop() {
  currentMillis = millis();
  int soundsens = analogRead(soundpin);
  if (soundsens >= threshold) {
    digitalWrite(ledpin, HIGH);
    delay(1000);
  }
  else {
    digitalWrite(ledpin, LOW);
  }
}

That's AFAIK a sensor with digital output only, so you should use a digital pin and digitalRead.
The pot on the module sets the sound/nosound threshold (sensitivity).
Leo..

Is the LED blinking?

It would be nice to know something about the sound sensor.* When you buy cheap stuff off eBay sometimes you don’t get specs or a datasheet. It wouldn’t hurt to send the soundsens value out to the serial port so you can see what you’re getting. (See the Analog Read Serial Example.)

You’re not using millis() and you can’t read the sensor during the delay() time. Take-out the delay().

…The idea would be to read millis() every time through the loop but save the timer value (in a variable you create) only if sound is present. When there is no sound don’t update that saved timer value.

When there is sound, the millis() value (current time) and the saved value will be very-close. When the sound stops, millis() will keep counting-up (as it always does) but the saved value won’t be updated.

Subtract those two values (every time through the loop). If the time difference is greater than 10,000ms, turn-on the relay.

If you do nothing else and just keep running the loop, the time difference between the current time and the saved time will continue to grow and the relay will remain on. When there’s sound again the time difference will go back to (almost) zero and the relay will turn off.

  • There are 3 kinds of sound sensors (and some that work more than one way).:
  • Some put-out a digital-high when the signal is above the threshold and a digital-low when below the threshold.

  • Some put-out a biased & amplified analog audio waveform. (They are biased because the Arduino can’t directly read the negative half of an AC audio signal. These will put-out 2.5V with silence which reads about 512 on the ADC).

  • Some put-out a varying 0 - 5V DC voltage that’s proportional to loudness.

P.S.
You might want to START with a simple program that turns-on the LED whenever sound is present (with no timing). Then reverse the logic to turn the LED off when sound is present. Then add the timing to ignore short periods of silence.

The analogue+digital version of that module has four pins.
OP posted a link to a 3-pin module.
Leo..

Hi.

Currently this is my code to activate the relay (Currently an LED). The problem is everything I search for is on how to detect noise and not silence

If the codes you have looked at have a variable that is TRUE or 1 when noise is detected
Then when that variable is FALSE or 0, there must be NO Noise or SILENCE...

Your sensor reacts/detects sound.

So no reaction/detection == silence.

So write your code to detect sound...

silencedetected != noisedetected;

Tom.. :slight_smile:

DVDdoug:
P.S.
You might want to START with a simple program that turns-on the LED whenever sound is present (with no timing). Then reverse the logic to turn the LED off when sound is present. Then add the timing to ignore short periods of silence.

That's what I already have in the original code. I can get stuff to activate with sound (or without it)

Wawa:
That's AFAIK a sensor with digital output only, so you should use a digital pin and digitalRead.
The pot on the module sets the sound/nosound threshold (sensitivity).
Leo..

Yes I realized that later.

A friend was helping me and got me a little further. The problem I am now having is the microphone is not sensitive enough. With a very basic code, the mic can hear me across the room. Now it can barely hear me right next to it. I either need a better module or something in the code is affecting the sensitivity.

byte soundSensor = 2;
byte LED =  LED_BUILTIN;

// timer
const unsigned long period = 12000;
unsigned long currentTime;
unsigned long startTime;
unsigned long finishTime;
bool running = true;

// debouncing
int buttonState;             
int lastButtonState;         


unsigned long lastDebounceTime = 0; 
unsigned long debounceDelay = 5;  

void setup() {


  pinMode(LED, OUTPUT);
  pinMode(soundSensor, INPUT);


  buttonState = digitalRead(soundSensor);
  lastButtonState = buttonState;


  Serial.begin(9600);
  finishTime = millis() + period;

}

// Timer Code

void timer_start() {
  // get current time
  startTime = millis();
  running = true;

  Serial.print("Timer started... ms remaining: ");
  Serial.println(period);
}

void debounce() {
  
  int reading = digitalRead(soundSensor);

 
  if (reading != lastButtonState) {

    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;
    }
  }
  lastButtonState = reading;
}

// MAIN LOOP

void loop() {
  currentTime = millis(); 
  debounce();
  if (buttonState == HIGH) {
    running = true;
  }
  if (running) {
    // timer is running
    if (buttonState == HIGH) {
          Serial.print("Button Pressed.");
          finishTime = millis() + period;
    }
    if (finishTime > currentTime) {
      Serial.print("Timer ms remaining: ");
      Serial.println(finishTime - currentTime);
    } else {
      Serial.println("Time Elapsed.");
      running = false;
    }
  }
}

Hi,

A friend was helping me and got me a little further. The problem I am now having is the microphone is not sensitive enough. With a very basic code, the mic can hear me across the room. Now it can barely hear me right next to it. I either need a better module or something in the code is affecting the sensitivity.

What did you do, hardware or software change?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.. :slight_smile:

This is from online but it’s essentially my setup. Nothing else is connected because I was testing it with the serial monitor to make sure it restarted when it detected sound.

Hi,
Have you adjusted the threshold potentiometer on the PCB?

Tom.. :slight_smile:

I have indeed. It’s very precise when and where it wants to work.

If I hook the module up to a non-arduino device (Like a relay) it works much better from a distance. I might need a better module for use with the arduino.