KY - 038 Sound Sensor

Hi everyone!
I'm new to Arduino and am having a problem with this sound sensor. With the code I created for the clapper(modified someone else's), the sensor won't work if I clap more than around 10cm from the sensor, but with a code that I found and only turns on the led light for 1 second and works even when I clap 5m away and I don't know how to do the same with the clapper script.
Thanks in advance for all help!

The clapper:

int Analog_Soundpin = A0;
int ledpin = 9;
boolean flag = true;

void setup()
{
  pinMode(ledpin,OUTPUT); // this pin controlled by flipflop() function
  pinMode (Analog_Soundpin,INPUT_PULLUP); // keeps pin HIGH via internal pullup resistor unless brought LOW with switch
  Serial.begin(9600); // just for debugging, not needed.
}

void loop()
{  
  if (digitalRead(Analog_Soundpin)==LOW);// check the state of switch1 every time we run through the main loop
  {
  delay(5); // I don't REALLY know why this delay helps, but it does.
      flipflop(); // hops out of main loop and runs the flipflop function
  }// end of check for button press.

  // other sketch code here

} // end of main loop.

void flipflop(){  //funtion flipflop
  flag = !flag;  // since we are here, the switch was pressed So FLIP the boolian "flag" state (we don't even care if switch was released yet)
  Serial.print("flag =   " );   Serial.println(flag);   // not needed, but may help to see what's happening.

  if (flag == HIGH){  // Use the value of the flag var to change the state of the pin
    digitalWrite(ledpin,HIGH ); // if the flag var is HIGH turn the pin on
  }
  if (flag == LOW) {
    digitalWrite(ledpin,LOW); // if the flag var is LOW turn the pin off
  }
  while(digitalRead(Analog_Soundpin)==LOW); // for "slow" button release, keeps us in the function until button is UN-pressed
  // If you take out this "while" the function becomes a flipflop oscillator if the button is held down.
  delay(10); // OPTIONAL - play with this value.  It is probably short enough to not cause problems. deals with very quick switch press.
}

The code in which the sound sensor works better:

const int ledpin=9; // ledpin and soundpin are not changed throughout the process
const int soundpin=A0;
const int threshold=40; // sets threshold value for sound sensor
void setup() {
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(soundpin,INPUT);
}
void loop() {
int soundsens=analogRead(soundpin); // reads analog data from sound sensor
if (soundsens>=threshold) {
digitalWrite(ledpin,HIGH); //turns led on
delay(1000);
}
else{
digitalWrite(ledpin,LOW);
}
}

this sound sensor

provide a link please so we can check the specs. You may need to use an amplifier.

Welcome top the group! Have you tried the digital output, it requires a lot less code and is less susceptible to electrical noise. Your clap is probably received as a narrow pulse by the analog input. When the pulse comes and when you read it are not coordinated so it is catch as can. The DO is always active when the clap occur You can use an interrupt to set a flag then in your main loop process the clap. Never do any processing in the interrupt service routine, it will eventually kill your code and you will miss things. Adding a cap of maybe 500 nF or so would help the analog input. Have fun!

gilshultz:
Welcome top the group! Have you tried the digital output, it requires a lot less code and is less susceptible to electrical noise. Your clap is probably received as a narrow pulse by the analog input. When the pulse comes and when you read it are not coordinated so it is catch as can. The DO is always active when the clap occur You can use an interrupt to set a flag then in your main loop process the clap. Never do any processing in the interrupt service routine, it will eventually kill your code and you will miss things. Adding a cap of maybe 500 nF or so would help the analog input. Have fun!

You really should have taken a few seconds and read the code the OP posted. The version that they report working poorly reads the clapper signal as digital signal; the one that they say works much better reads it as analog signal.
The Fritzing image is misleading as usual (OP, do yourself a favour and DUMP THAT POS!) as it shows a 3-pin sensor while the KY038 is a 4-pin one. Very likely the OP is using the analog output for both, meaning the output has to go above roughly 2.5V for it to be detected as HIGH (yes, I know the datasheet says 0.6*Vcc but that's the guaranteed value, and here I'm talking about the actual voltage where the pin will read HIGH). That equates to an analogRead() threshold of ~500... where the more sensitive program uses a threshold of 40.

1: Make a schematic you can post

2: link to the sound sensor please

3: in your code you are using the "DEFAULT" reference. I'm assuming from the (despicable) Fritzing you are using a UNO. So change the reference to "INTERNAL" and you will get 4* better sensitivity on analog inputs.

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