Arduino Knock Lock not working properly


I recall having problems getting the Arduino Knock Lock to work properly. I’ve added my own modified code below – hopefully somebody finds it of use. The issue with the code seemed to be the speed at which the knock evaluations were taking place (meaning that one knock counted as more than one), and the sound sensitivity values in the code that caused the smallest sounds to be counted as knocks.

By modifying the delay() times and the quietKnock and loudKnock values, you should approach a more usable setup. The code could be improved further of course — but this will suffice.






  1. Thank you so much for your code improvements. My project works now.

  2. Thanks, the key for me was in adding a delay after incrementing numberOfKnocks:

    if (checkForKnocks(knockVal) == true) { // Validate the vibration as knocks with a custom function.
    numberOfKnocks++; // if they’re valid knocks, then increment the knockCounter.
    delay(200); // This was key to reducing in the noise and making it detect knocks

    I ended up using these for the knock detection thresholds:
    int quietKnock = 150;
    int loudKnock = 5500;

  3. Forgot to mention, I just built the circuit on the breadboard, rather than attaching the piezo directly to anything. The upshot of that is you need to make sure the breadboard isn’t moving around, and then produce a defined tap on the piezo, probably with something metallic or rigid plastic. For me, tapping it with tweezers worked, but not my finger for instance.

  4. Hey, thank you for putting this up. I couldn’t get piezo to register anything at all and i couldn’t find where the code was wrong. I pasted yours in and kept comparing with mine until I found the extra = sign somewhere. Thanks for the help!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">