Need help on a project including Sound sensor + speaker

My project consists of using a speaker and a sound sensor module to play a recording once a noise above a certain noise level is made.

I am using an Arduino Uno, an SD card holder (+SD card with recording on it), and a sound sensor module. Name of audio file is "whisper"

SD card holder is connected such that

  • CS -> D10
  • SCK - > D13
  • MOSI -> D11
  • MISO -> D12
  • VCC -> 5V
  • GND -> Ground

Sound sensor connected such that

  • G -> Ground
    • -> 3.3V
  • DO -> A1

Speaker connected to D9 and Ground

The two problems I am facing and do not know how to solve is as follows

  • Despite being in an "if" command, the circuit repeats itself, regardless of whether there is a sound or not
  • At first, the recording plays at very good quality, but as time goes on and as it repeats itself, the quality of sound gets worse and worse until it seems like there is nothing more than a screeching sound

Code for entire circuit is as follows

(start of code)

#define SD_ChipSelectPin 10   
#include <TMRpcm.h>           
#include <SPI.h>

TMRpcm tmrpcm;  

const int SPEAKERpin = 9;
const int SOUNDpin = A1;
const int threshold = 200; //can be modified
const int wait=5000;




void setup(){
  tmrpcm.speakerPin = 9; 
  Serial.begin(9600);

  
  pinMode(SPEAKERpin, OUTPUT);
  pinMode(SOUNDpin,INPUT);



  if (!SD.begin(SD_ChipSelectPin)) {  
    Serial.println("SD fail");  
    return;   
  }
  else{   
    Serial.println("SD ok");   
  }
   
}

void loop(){
delay(wait);
int soundsens=analogRead(SOUNDpin);
if(soundsens>=threshold){
Serial.println("Sound on");
tmrpcm.play("whisper.wav"); //file name "whisper", .wav is added automatically 
  tmrpcm.volume(6);
  delay(wait);


    }
    else{
      delay(wait);
      }
 
  }

(end of code)

1 Like

what do you see in the Serial monitor opened at 115200 bauds if you run this

const int SOUNDpin = A1;
const int threshold = 200; 

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

void loop() {
  if (analogRead(SOUNDpin) >= threshold) {
    Serial.println("ON");
  } else {
    Serial.println("OFF");
  }
  delay(100);
}

if you constantly see "ON" it means that your 200 level is not good enough

tried this and it would constantly be off until i made a noise when it would turn on

when I incorporate this into the actual project, it will either be constantly OFF or constantly ON

So that kinda worked

What does it mean integrated in your code? Post what you had

I created a new code where it worked, constant "off" until i made a noise

when i integrated it into my project code below (switching baud and inserting print on/off) it would either stay constantly at off or constantly at on.

#define SD_ChipSelectPin 10   
#include <TMRpcm.h>           
#include <SPI.h>

TMRpcm tmrpcm;  

const int SPEAKERpin = 9;
const int SOUNDpin = A1;
const int threshold = 500; //can be modified
const int wait=5000;




void setup(){
  tmrpcm.speakerPin = 9; 
  Serial.begin(115200);

  
  pinMode(SPEAKERpin, OUTPUT);
  pinMode(SOUNDpin,INPUT);



  if (!SD.begin(SD_ChipSelectPin)) {  
    Serial.println("SD fail");  
    return;   
  }
  else{   
    Serial.println("SD ok");   
  }
   
}

void loop(){

int soundsens=analogRead(SOUNDpin);
if(analogRead(SOUNDpin)>=threshold){
Serial.println("ON");
tmrpcm.play("whisper.wav"); //file name "whisper", .wav is added automatically 
  tmrpcm.volume(6);
  delay(wait);


    }
    else{
      Serial.println("OFF");
      delay(1000);
     
      }
 
  }

Why do you capture the value in a variable, and then immediately toss it in the garbage?

Here, you create a symbolic constant for the speaker pin, and then toss it in the garbage...

const int SPEAKERpin = 9;
...
  tmrpcm.speakerPin = 9;

Doesn't the library do the pin initialization by itself?

  pinMode(SPEAKERpin, OUTPUT);

Why do you configure an analog input pin for digital use?

  pinMode(SOUNDpin, INPUT);

Okay, wait, I see you are using the digital output from the sound sensor. But then why are you using analogRead() instead of digitalRead()?