Bug in "Knock" Example code

I believe that the "delay(100)" should be moved up into the "if" block, right after the "Serial.println("Knock!")" statement.

This way the analog 0 port is polled very frequently until it receives a knock, and only then delays. Without this, the sketch misses most knocks.

Could you give a link, please?


Sorry. Here's the link:


Before this can be answered (by me), I need to test with oscilloscope, what kind of burst piezo gives, with different parallel resistors.

Good question, maybe there is something we can’t understand, yet!


For what it's worth, I was looking at the piezo output on a scope with a 1 M resistor in parallel with the piezo leads. Of course, the exact output depends on what surface the piezo is mounted on and how hard you knock. I had mine mounted on 1/4 inch plywood. The dominant frequency of vibration appeared to be about 500 Hz. It decayed rapidly over a few cycles. At 500 Hz, one cycle is just 2 milliseconds, so having a delay of 100 milliseconds between each reading of the analog input is way too long.

The comment on the code indicates that the delay is to "avoid overloading the serial port buffer". If that is so, then the delay only need occur when a knock is detected and serial output is sent.

As I worked on my first project using a piezo as a knock sensor, this bug caused me lots of extra work. I was using the sample code to test that the knock sensor was working. It appeared to be very insensitive. So I made several modifications including an op-amp added to the piezo output, and a mass glued to the piezo. In reading the forums, it appeared that others were also having trouble with low or inconsistent knock sensitivity when using the code from this knock tutorial.

Thanks for your attention to this. I'm hoping to help others avoid this same problem.


hey thespiffer1. as a newbie i do not fully understand all the electronics involved in that one, so i'm not totally sure... however, your comment was actually the solution to the problem i was facing with that sensor - it seemed to be too insensitive and only triggering here and then with heavy vibration. i could follow your argument and changed the code accordingly and now it works perfectly. so thanks for your help. would be great to have a closer look at that and change the tutorial coding if it proves to be right. however, i wonder why others did not face that problem? i was searching for a solution on the net quite extensively before i found your comment.

Hi Dynamicboost,

I'm glad my post was able to help you out. I'm pretty sure the error in this code is just a typo. From what I can gather on these forums, there are a limited number of people who have privileges to make changes on the main arduino site, and those people have lots of more pressing projects to deal with. So unfortunately small, irksome errors like this persist long after they are identified.

Best of luck on your arduino projects,



This is brilliant! I've never even considered that to be a factor but it makes perfect sense!!! Thanks for discovering that and sharing with everyone. I was already getting ready to build a preamp for my contact mic thinking that it simply isn't sensitive enough. Have you tried to contact whoever wrote that sample code? It would save people a lot of headache :) Thanks again!