test code for piezo

Your right I had the resistors switched, tried my old el-cheep-o camera but still won't upload without editing the resolution. So here's what I have now. With the 470 resistor the Piezo Peak Capture shows up and then under that peak=228 only 1 time no other prints, nothing when I tap the piezo or connect the pot and turn it.
Dt.jpg
 
Last edited:
So I tried this several times now, I open the serial monitor, it reads
Piezo Peak Capture
peak=333 and that's it, I get nothing taping on the piezo, what am I looking for?
 
First, before more fiddling with your code, take an oscilloscope and make sure that the piezo pulse is arriving correctly at the analog input pin of the Teensy. Post pictures here.

Second, from what you see on the oscilloscope in terms of timing (after making sure that the amplitude is optimal), develop a strategy to analyze the pulse and find its local maximum. Then, after having solved the problem on the hardware side and in your mind, you might start coding in an efficient manner.

As long as you can't be sure about the input and as long as you don't know where to go and how, your coding attempts are prone to fail systematically.
 
if you don't have a scope, you can run this little sketch with the "Serial Plotter" of the IDE (under Tools)
Code:
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A0));
  delay(5);
}

close the monitor and select Serial Plotter under Tools in the IDE. As you touch and tap the piezo you should see the response in the real-time plot. The plot should give you a hint as to thresholds and pulse durations. Max value of A0 is 1023

piezoplot.png
 
Last edited:
It's just because of the previous history that one can not be sure if this specific Teensy is still working correctly, that I recommended using an oscilloscope.

A combined handheld 10MHz oscilloscope and multimeter can be bought for less than $180 and is a strictly necessary equipment when it comes to designing and debugging analog circuitry.
 
Ahhh, Thank you!! finally some progress, I don't have an oscilloscope, I see there is an Arduino sketch for one, maybe I'll try that. How do I post a screen shot of the plotter? Mine has the line at 231 and the spikes go down farther than up when I tap the piezo.
 
Last edited:
Ahhh, Thank you!! finally some progress, I don't have an oscilloscope, I see there is an Arduino sketch for one, maybe I'll try that. How do I post a screen shot of the plotter? Mine has the line at 231 and the spikes go down farther than up when I tap the piezo.
whilst tapping on the piezo, hit "print screen" on your keyboard. you may want to use an image editing tool to paste out the window of interest.

"the line at 231" sounds suspicious. with no tapping on the piezo, the values plotted should be < 10.

maybe re-run the DAC-A0 sketch in post #23, you said column 2 went to 0 if you connect A0 to GND (good), but you didn't confirm that you get close values for each column as reported in post #25 with A0 connected to DAC0
 
Last edited:
That’s why I insist on checking with a real oscilloscope. The line at 231 might be an indicator for the presence of a wiring error in the external circuit, or it might point to a defect Teensy. You will never know without an external independent differential diagnose.

Without any trigger from the piezo, the line should be close to 0, at least below 8, considering some noise.
 
My keyboard doesn't have print screen labeled, I tried the sketch in 23 with the circuit t you have ( the 1m resistor and 3.3 diode) and with the monitor there is 1 column of 0's and an occasional 1 or 2 without touching the piezo. When I tap it the numbers go up to as much as the 400's It shows in the plotter as spikes going up from the 0 line, There is some noise that calms down eventually. I'll try to figure out how to get a screen shot later.
 
My keyboard doesn't have print screen labeled, I tried the sketch in 23 with the circuit t you have ( the 1m resistor and 3.3 diode) and with the monitor there is 1 column of 0's and an occasional 1 or 2 without touching the piezo. When I tap it the numbers go up to as much as the 400's It shows in the plotter as spikes going up from the 0 line, There is some noise that calms down eventually. I'll try to figure out how to get a screen shot later.

for the DAC-A0 sketch in post #23, you should have no piezo connected to the T3.6, just one jumper from A0 to the DAC0 pin (A21).

if you have a way to capture and save a window on your machine, i can show you how to modify the tiny serial plotter sketch to freeze the output. Your narrative description of how the serial plotter behaves sounds like it may be working. You should use a 10K resistor (as in Paul's design) and not the 1Mohm resistor.
 
Last edited:
I redid the test in #23 and the numbers were the same in both columns. The screenshot is Paul's circuit using your code from #55. disregard the bottom 2 attachments I can't seam to delete them.

pl3.PNG
 

Attachments

  • plotter1.PNG
    plotter1.PNG
    91.2 KB · Views: 71
  • pl2.PNG
    pl2.PNG
    38.3 KB · Views: 138
OK, your sketch #23 results indicate the T3.6 A0 is working. yeah.;)

your plots seem to look OK as well, though if you tap piezo hard can you get higher numbers? I can get values in the plot > 900. Are you using 10K ohm pulldown resistor as in Paul's circuit?

The plots would indicate that for Paul's sketch in post #11, it should report peaks greater than 12. is that sketch still not working for you ?
 
The numbers in your plot are very small, peaks reaching up to only single digit numbers. The peaks should be in the 100 to 1000 range.

Maybe the resistors are *still* wrong? The circuit seems to be working, but giving a very tiny signal. That's very likely a problem with the resistors. Check them with a multimeter. Also check the colors with the resistor color code. A 10K resistor should be brown-black-orange-gold. Compare your build against the photo I showed in message #49.
 
Also possible piezo is defective. what happens if you untape the piezo so it has more room to vibrate?
 
The 10k is correct the 470 reads 464, these piezos don't put out a lot of power and I just tapped them with my finger. I have some bigger ones but I used these because they don't produce as much cross talk. they work fine and are very similar in power to the pads in my Alesis sample pad pro, It has 2 inputs for pads and I used these and cannot tell a difference from the ones in the unit. So you mean 100 being a soft tap and 1000 a hard hit? Should I try to adjust for that? https://www.youtube.com/watch?v=0MVZJtWbqsU about 29 minutes in he starts talking about how the hit looks on the scope. If you haven't seen this may be worth a watch.
 
I' believe the piezo's fine but I have plenty more. I explained to Paul in#66, The tape makes no difference in fact I tried many things when building the pads including hot glue all over and made no noticeable difference.
 
Maybe try a 100K resistor instead of 10K.

Look, everyone else uses these same piezo sensors, which give plenty of voltage output when tapped with a finger or object. So you *must* be doing something differently. After 67 messages we've helped you correct numerous electrical mistakes. You're so very close, but you're not going to get this working with an attitude that your circuit is ok. Something must be connected incorrectly. The circuit worked here, and Manitou had it work, and you can find numerous projects all over the internet where this works.
 
Here's another test using the DAC-A0 sketch in #23. We'll test A0, the resistors etc without the piezo. Remove the positive piezo lead from the 470 ohm resistor and replace it with a jumper from DAC0 (A21). A0 is still connected as for the piezo test. So now the DAC is providing various voltages to pass through the resistor divider. Of interest are the high end values. Give it a try. Here is what i see from sketch #23

Code:
3872 3700
3904 3730
3936 3760
3968 3792
4000 3821
4032 3852
4064 3871
0 2
32 33
64 63
96 94
128 124
160 155
With the 10K pulldown, the voltage divider should be 10000/10470 = .955 , and .955*4064 = 3881 (close enough to what's measured).



(@PaulStoffregen, Side note (I'm EE-challenged) : the diode clamp (1N4004) shouldn't be in play for this test. For high voltages, i measure the clamped max at about 4v (3.3 + 0.7), a wee bit higher the T3.6 max of 3.6v. The voltage divider helps some. Not to worry?)
piezo470b.png a clamped pulse, max 3.88v
 
Last edited:
Driving the pins over 3.6 and under -0.3 isn't great. Technically that's out of spec. But the specs are very conservative.

The clamping diodes should be "small signal" type diodes. 1N4148 and 1N914 are the most common. These types of diodes have very low capacitance when reverse biased. They (usually) have very low leakage current too. All diodes suffer from "reverse recovery time", and again this effect is minimized with small signal diodes. For clamping signals, fast reverse recovery is the most important spec. Typically signal diodes have fairly low current rating, like 100 mA.

1N4004 is what's called a "rectifier diode". They're designed for the lowest cost to turn 50 or 60 Hz AC into pulsing DC current. These are not good for clamping signals. They have higher capacitance and leakage current, which tends to corrupt the signal. The also have slow reverse recovery, which makes the fairly poor for clamping (but still far better than nothing).

Two other types of diodes are commonly sold.

"Fast rectifier" diodes are similar to 1N4004 but with better reverse recovery and usually lower capacitance. These aren't as good as small signal diodes, but they're a pretty decent 2nd choice. These diodes are main intended to be used as rectifiers inside switching power supplies, where the AC frequency is dozens or hundreds of kHz.

Schottky diodes have a very different mix of specs. They have much, much more reverse leakage current than any other type of diode. So a Schottky with a high current spec (like 1 amp or more, such as 1N5817) is usually a terrible choice for clamping signals. But much smaller ones like BAT54 can work in some cases. If you ignore the leakage, everything else about Schottky diodes looks wonderful. They have very fast reverse recovery, and the forward voltage is less. Many people, myself included, have tried using Schottky for clamping small signals because all the other specs looked so good, but the reverse leakage can ruin everything. The reverse leakage is also highly temperature sensitive, so what's not great at room temperature can become far, far worse when you put the whole project into a small sealed box and it heats up slightly.
 
this can't be good
so this is just the output of A0 with the piezo hooked up and idle?

if you have a constant voltage like that, you can take the teensy out of the loop. disconnect the jumper from the T3.6 A0 pin and hook it up to your voltmeter. you should see 3.3*386/1023 = 1.2 v. Where is that voltage coming from?? maybe time for another photo of your wiring.

you could also disconnect the positive jumper of piezo from the 470 ohm resistor and see if the plot drops back to 0.

some long breadboards are known to have problems (discontinuous power rails), though that seems unlikely in this case.

if things get closer to normal, try test in post #69.
 
OK, your first photo looks like it is wired up correctly for the DAC-A0 test using the sketch in post #23, which prints out 2 columns of numbers. How does your sketch #23 output compare with the table in post #69
 
I don't understand the connection to the 3v positive from board, all the examples I've seen are just analog inputs and gnd.
 

Attachments

  • s6.PNG
    s6.PNG
    43.5 KB · Views: 115
Back
Top