This example will explain how to use a piezo as a knock sensor. You can knock on it directly, or tape it to a surface and knock on the surface.


  • 1 Arduino Uno board
  • 1 Basic Education shield
  • 1 piezo
  • 1 1m ohm resistor
  • 4 jumper wires


  1. Connect the resistor to A0 and ground.
  2. Connect the piezo to A0 and ground through the resistor.
  3. Open Example>BasicEducationShield>Help>PiezoKnockSensor
Piezo Knock Sensor

#include <BasicEducationShield.h>

PiezoKnockSensor sensor=PiezoKnockSensor(A0);

void setup(){

void loop(){
Serial.println("Please knock...");
//Wait until the button is knocked.

Upload it to the board. Open the serial monitor and knock on the piezo to see if it works. It should print a “1” in the serial monitor window when you knock.

To change the sensitivity of the knock sensor, in setup change the line sensor.config(40,80). 40 is the knock sensing threshold and 80 is the debounce time. Threshold defines how hard you need to knock, debounce time prevents the sensor from returning several hits from one knock, but also limits how rapid you can knock.

Most of the time, it’s easier to improve the construction than finding a good threashold/debounce value. If you tape the piezo to a surface, it’s better to knock the surface close to the piezo. Removing the plastic casing of piezo also helps increasing the sensitivity.

It’s not working?

  1. Make sure everything is connected correctly.
  2. Make sure you have connected the piezo to the same pin as you’ve stated in your code.
  3. If you are using piezo for both Melody and knock sensor, make sure you distinguish them two!

Note: Remember not to use A4 orA5