1

I'm using 1mOhm resistor in parallel to a piezo that is connected to analog1 pin on the Due,

enter image description here

and the following code to observe the readings in the serial monitor:

int piezo = A0;
int threshold = 200;//anything over 200 means I've hit the piezo

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

//this part checks if the threshold has been reached and then writes the strenght of the signal 

void loop(){
    int piezoVal = analogRead(piezo);

    if (piezoVal>threshold){
        Serial.println(piezoVal);

        delay(10); //delay is used to prevent from getting multiple values one after another, usually 3-4
    }
}

The things that bother me are:

  • if I do not use threshold, then I am constantly getting readings from the piezo, even if the threshold is slightly below 200.

  • if I do not use delay, then I'm getting getting multiple values one after another

  • and the strangest thing is while I hit the mouse pad that covers the piezo, the readings in the serial monitor are not matching the force being used to trigger the piezo. Sometimes they stay the same, sometimes they reach 6-700 after I directly touch the piezo, sometimes I hit really hard and I only get some slight increase (for ex. from 225 to 229).

This doesn't make sense to me. I think the stronger the vibration, the higher the values should be. Cause I've tried with two different piezos and different resistors (1mOhm, 1.2 mOhm and 1.5 mOhm) and I'm still getting the same result. I bought the Due few days ago in order to build a drum kit, and I have no problem with sending midi notes to a DAW, but this piezo sensitivity thing is driving me crazy. Any suggestions?

1

The problem is timing. When you tap or hit the sensor it produces an impulse response: a signal that increases to a peak, then decays back to zero. You are expecting the code to capture the peak value. Your code is spinning in loop() as quickly as possible, but captures just the first point above the threshold. The signal can still be increasing when you capture and print.

So imagine this stream of signal values

time  signal   analogRead()
      at pin   occurs
----  ------   --------
 1     190      190
 2     190
 3     199      
 4     210    
 5     220      220 - trips threshold, prints and then waits
 6     230
 7     260
 8     280
 9     320      (this point occurs during delay())
 10    280
 11    260

The same exact signal, but at a slightly different time could be read different by the code:

time  signal   analogRead()
      at pin   occurs
----  ------   --------
 1     190      
 2     190
 3     199      199
 4     210    
 5     220      
 6     230
 7     260      260
 8     280
 9     320      (closer, but still did not read the peak)
 10    280
 11    260

So the strategy of catching the first point above the threshold makes it unlikely you can capture the peak value. There are two ways forward: hardware or software.

You could use electronics that latches the maximum value. If you search "peak hold circuit" you will find many circuits that would capture and hold the peak. That frees the Arduino to somewhat lazily spin in the loop and still be able to accurately capture the peak. Once it sees the value is no long increasing, it resets the peak hold and goes back to waiting.

You could use code to capture the peak. Something along the lines of this psuedo code:

lastreading = analogread()
if(lastreading > threshold) {
  peakreading = lastreading
  keeplooking = true
  while(keeplooking) {
    nextreading = analogRead()
    if(nextreading > lastreading) {
      // the signal is increasing, save this and see if next point is even higher
      peakreading = nextreading
      lastreading = nextreading
    }
    else {
      //signal is decreasing, the peak is over
      keeplooking = false
    }
  }

This software approach could still miss a sharp peak that occurs between analogRead()'s, but may be good enough for your use.

  • 1
    I very much appreciate your help. Since I'm a total beginner, I expect things to run smoothly :). I'll definitely dive into this peak filtering and share the results. – Neurotic Oct 31 '14 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.