Knock Detection Sensor Arduino

Knock Detection

Here we use a Piezo element to detect sound, what will allow us to use it as a knock sensor. We are taking advantage of the processors capability to read analog signals through its ADC – analog to digital converter. These converters read a voltage value and transform it into a value encoded digitally. In the case of the Arduino boards, we transform the voltage into a value in the range 0..1024. 0 represents 0volts, while 1024 represents 5volts at the input of one of the six analog pins.

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the analog input pin number 0, that supports the functionality of reading a value between 0 and 5volts, and not just a plain HIGH or LOW.

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string “Knock!” back to the computer over the serial port and also blinks the Pin 13 LED.

Piezo Knock Sensor

Piezo Knock Sensor
Piezo Knock Sensor

Arduino Knock Detection Circuit

Arduino Piezo Knock Detection Circuit
Arduino Piezo Knock Detection Circuit

Arduino Knock Detection Code

/* Knock Sensor
* We have to basically listen to an analog pin and detect 
 * if the signal goes over a certain threshold. It writes
 * "knock" to the serial port if the Threshold is crossed,
 * and toggles the LED on pin 13.

int ledPin = 13;
int knockSensor = 0;               
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;

void setup() {
 pinMode(ledPin, OUTPUT); 

void loop() {
  val = analogRead(knockSensor);     
  if (val >= THRESHOLD) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
  delay(100);  // we have to make a delay to avoid overloading the serial port

Knock Detection Result

                Open serial monitor, when you tap on the table or sensor Red LED (13) will blink and on serial monitor “Knock!” will be displayed.

Knock Detection Result
Knock Detection Result

One thought on “Knock Detection Sensor Arduino

  1. We used a piezo-electric vibration sensor to detect knocks. It connects to the ground and one of the analog pins on the Arduino Uno, and generates a small voltage whenever it experiences a vibration.

Leave a Reply