"knock" sensor with Piezo

Hi guys,

Essentially I've connected a piezo and an LED to appropriate pins. My code works as the light flashes when a certain threshold is reached on the analog input

I have a few questions

  1. Why do I use a resistor in parallel with the piezo wires

  2. How can i get my sensitivity right - also its strange because the device doesnt really pick up sound - but just more when i tap it or something.

Ive been trying to use this to guide me, and while it works - it doesnt work very well.

Ive tried various resistors, but theres clearly a sweet spot i can't find

  1. how can i find it?

there is also a strange phenomenon to me where if i do tap it, it the analog input just seems to stay at a certain value for quite some time and just slowly go down by 1. I kind of want it to be responsive and quick

in case it is of any use to you: my code

/*

Microphone

*/

int ledPin = 2;
int knockSensor= 5;
byte val = 0 ;
int statePin = LOW;
int THRESHOLD = 100;




void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop()

{
  val = analogRead(knockSensor);
  Serial.println(val);
  delay(100);
  
 
  if(val >= THRESHOLD)
  {
    statePin = HIGH;
    digitalWrite(ledPin, statePin);
    //Serial.println("Knock!");
  }
  delay(100);
  statePin = LOW;
  digitalWrite(ledPin, statePin);
  
    
}

Thanks in advance

Is the sensitivity something i can improve? I can't really figure out the optimum resistance.

Id like to be able to make it such that, it picks up sounds and the reading goes down to zero right away

because at the moment, if i tap it or something it just sits a value and slowly goes back down to zero.

Sound doesnt really seem to work much - it works more if i tap the piezo, or something like that.

First off you do not want any delays in your code.

Second, yes this is the way those sensors behave, they are not much use for sound.

To get proper control of the output from them you need to use several op-amps. To amplify, filter and act as an envelope detector.

I see, so i guess there is no point using a piezo for anything other than making beeps? Otherwise its too much effort for something that can be done with a better suited device.

Thanks Grumpy Mike

Yes it is also not too bad for directly knocking but sound while it is possible is not very useful.