1

I'm trying to creature an instrument out of a few boards I have. The plan is to have piezo sensors wait for a hit, then be read by a digital pin, then execute code based on what piezo sensor was hit.

I know how to make a note play, but I don't know how to read a signal from a piezo that's between 3V and 5V. I'm not worried about velocity measurements, I just want there to be a hit, register the hit through arduino, then relay the pin number to a track number.

I read that pin change interrupts can read a signal instantly.

Below is some example code for my audio output board that plays audio files. This specific bit of code is for "notes" on an instrument.

          wTrig.masterGain(-8);                 // Lower main volume
          wTrig.trackPlayPoly(6);               // Play first note
          delay(1000);
          wTrig.trackPlayPoly(7);               // Play second note
          delay(1000);
          wTrig.trackPlayPoly(8);               // Play third note
          delay(1000);
          wTrig.trackFade(6, -50, 5000, 1);     // Fade Track 6 to -50dB and stop
          wTrig.trackFade(7, -50, 5000, 1);     // Fade Track 7 to -50dB and stop
          wTrig.trackFade(8, -50, 5000, 1);     // Fade Track 8 to -50dB and stop

I don't have experience with code, but this is how I think activating a note would work:

wTrig.trackPlayPoly(pin number)
delay(100);
wTrig.trackFade(pin number, -50, 5000, 1);

As far as limiting voltage, don't I just need to put some resistors in circuit with the piezo sensors?

In psuedo-code, this is what I want to make happen:

Listen for signal from any pin
Store that pin number as a variable
wTrig.trackPlayPoly(pin number)
delay(100);
wTrig.trackFade(pin number, -50, 5000, 1);

2 Answers 2

1

A piezo will output a decaying sinusoid when you feed it an impulse (a hit on the attached surface). Problem with a high frequency sinusoid is that one: half the time it's negative, and two: it forces a near-real-time monitoring of the analog voltage there, ie no or small delays in your code.

I've made a pattern recognition 'drum' with piezos, and the general approach was to first start with plotting the analog voltage of the piezo. This is done by putting the positive of the piezo to an analog pin and the negative to ground, and a 1M resistor between the leads (this value worked for me, play with different values to see what works best). Then do a serial plot (ctrl shift L in the IDE) and observe:

  • how high the peak is for different distances and strengths of 'knocks'
  • how many times a significant peak occurs before decaying.

Your end goal is to have the Arduino recognize a peak by seeing when the analog reading is above a threshold value , then have a dead period where it ignores subsequent peaks (so it doesn't read multiple events on a single sinusoid), and then play your note. Longer dead period means less false-positives, shorter means more responsive.

I apologize for the long-windedness of my somewhat ambiguous response, I'll try to remember to paste my code when I'm at my computer tomorrow.

1
  • I actually found a tutorial on how to plot a serial plot of a piezo before, but that's as far as I've gotten. I understand what you're saying.
    – Mason
    Commented Mar 26, 2017 at 15:44
1

If the piezo is mounted to a surface, and you're hitting the surface to create vibrations, it is complicated as mentioned in the other answer, and it would be a good idea to connect piezo brass to a stable 2.5V and use rail clamping diodes on the pin. For this purpose, you may be okay with just a couple 100 ohm resistors as a divider and a capacitor between ground and the 2.5V. The proper audio way is more involved.

But if you're tapping the piezo directly with your finger, or tapping a thin plate and foam on top of the piezo, it's a lot simpler. I've done this with a 100k ohm resistor in parallel (better response and consistency than 1 M ohm) and 5V zener diode in parallel for a 5V processor. An analog pin with AnalogRead function can determine the voltage. Brass wired to ground, center area wired to pin. I've found that placing thin foam underneath everything gets rid of the crosstalk, especially if you don't need velocity sensitivity.

I would avoid using the delay function because it will prevent other piezos from working during that time. Millis() calculations allow other actions to be performed.

2
  • Perhaps you could use the schema editor to enhance the answer?
    – MatsK
    Commented Sep 23, 2017 at 19:56
  • Perhaps. I searched for this schema editor (here and Google) and did not find it. Commented Sep 23, 2017 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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