Go Down

Topic: Questions about piezos. (Read 833 times) previous topic - next topic

melloedu

Hi,

I am fairly familiar with the Arduino and very at home with programming, but lacking an engineering background, I get stumped at times, so please bear with me for the following:

1. I bought some piezos for a project (https://www.sparkfun.com/products/10293), studied the Knock example and read everything I could find in the Forum. Put together and tested one of the piezos. The sketch seems to function according to decription, but for my application it won't work  because I need to:

a) read a knock at any moment in time (with the best possible resolution) but, with the delay at the end of the sketch, I miss some hits that happen in between readings; a smaller delay doesn't do it either.

b) get one (and only one) hit for each time the piezo is struck, when at peak voltage value; but every time I get a hit, no matter how hard or soft, I get more than one knock (previous hit dropping from peak voltage?)

2. Initially I thought of taking various readings until a delay had elapsed, as in a debouncing algorithm, but that only works with buttons because the voltage eventually gets stable (correct?). So I came up with the following algorithm, which is based on the idea that "if voltage just started dropping, it means we just left peak value":

void loop()
{
   currentReading = analogRead(knockSensor);
   if(currentReading >= threshold)
   {
      if(currentReading >= previousReading)   // RISING OR REPEATING...
      {
         // if we are rising again, we reset to allow for the next hit...
         if(hit) hit  = false;
      }
      else
      {
         if(!hit)         // DROPPING...
         {
            Serial.println("Knock!");
            hit = true;
         }
      }
      
      previousReading = currentReading;
   }
}

3. I would appreciate any critique to this solution. Is my basic assumption correct? Any caveats or limitations? It seems to work, but I am not sure how reliable this is...

4. How long does it take for one of those piezos to get back to zero after being hit? Couldn't find it in the datasheet...

5. How strong are these components? How long do they last? How much abuse can they take? My intention is to use them attach to a wooden plate to be struck by (hard and soft) mallets.

Sorry about the long post.( I didn't split these questions to avoid repeating the  same the story over and over...)






RIDDICK

Hi!

can u make a picture of the response with an oscilloscope?
maybe the arduino can b the oscilloscope by do a lot of
Code: [Select]
Serial.println(analogRead(x));?

Have fun
-Arne

melloedu

Here is what I managed to get. This is a plot of the readings from the A0 as I tap at various strengths with my fingers on a wooden plate taped to the piezo. The loop used for this was a bare minimum:

void loop()
{
       currentReading = analogRead(knockSensor);
       Serial.println(currentReading);
}


melloedu


RIDDICK

the sharp raising edge should b easy to detect...

if the reading jumps in a short time (e. g. between two readings) by 1 stripe, u can b sure that it is a tap...
after a tap u can just stop detection for some time...

like this:
Code: [Select]

void loop() {
  uint16_t lst=0;
  for (;;) {
    const uint16_t now = analogRead(x);
    if (now>lst && now-lst>thresh) {
      Serial.print(millis());
      Serial.printtln("TAP");
      delay(100); // no more than 10 taps/second?
    }
    lst=now;
  }
}
?
-Arne

melloedu

Thanks RIDDICK!

adjusting the delay and threshold, your cold did the trick.

melloedu


RIDDICK

*wag tail*

PS: if u want u may try the "Modify" icon above ur posting... :)
-Arne

dhenry

You can use interrupts to free up your loop(): you can either read the sensor digitally (for a rising edge), or adc it continuously to detect a certain kind of rising edge.

The isr will set a flag and the loop() will process that flag -> no missing knocks.

Go Up