Learning   Examples | Foundations | Hacking | Links

Knock Sensor

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 other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the input. We also have to connect a resistor in the range of the Megaohms in parallel to the Piezo element; in the example we have plugged it directly in the female connectors. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc and are easier to use as input sensors.

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. In order to see this text you could either use the Arduino's serial monitor, or make your own program in e.g. Processing. Later in this article we propose a program that works for the software designed by Reas and Fry.

Example of connection of a Piezo to analog pin 0 with a resistor

/* Knock Sensor
 * ----------------
 * Program using a Piezo element as if it was a 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.
 * (cleft) 2005 D. Cuartielles for K3
 * edited by Scott Fitzgerald 14 April 2013

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

Representing the Knock in Processing

If, e.g. we would like to capture this "knock" from the Arduino board, we have to look into how the information is transferred from the board over the serial port. First we see that whenever there is a knock bigger that the threshold, the program is printing (thus sending) "Knock!" over the serial port. Directly after sends the byte 10, what stands for EOLN or End Of LiNe, and byte 13, or CR - Carriage Return. Those two symbols will be useful to determine when the message sent by the board is over. Once that happens, the processing program will toggle the background color of the screen and print out "Knock!" in the command line.

// Knock In 
// by David Cuartielles <http://www.0j0.org>
// based on Analog In by Josh Nimoy <http://itp.jtnimoy.com>

// Reads a value from the serial port and makes the background 
// color toggle when there is a knock on a piezo used as a knock
// sensor. 
// Running this example requires you have an Arduino board
// as peripheral hardware sending values and adding an EOLN + CR 
// in the end. More information can be found on the Arduino 
// pages: http://www.arduino.cc

// Created 23 November 2005
// Updated 23 November 2005

import processing.serial.*;

String buff = "";
int val = 0;
int NEWLINE = 10;

Serial port;

void setup()
  size(200, 200);

  // Open your serial port
  port = new Serial(this, "COMXX", 9600);  // <-- SUBSTITUTE COMXX with your serial port name!!

void draw()
  // Process each one of the serial port events
  while (port.available() > 0) {

void serialEvent(int serial) 
  if(serial != NEWLINE) { 
    buff += char(serial);
  } else {
    buff = buff.substring(1, buff.length()-1);
    // Capture the string and print it to the commandline
    // we have to take from position 1 because 
    // the Arduino sketch sends EOLN (10) and CR (13)
    if (val == 0) {
      val = 255;
    } else {
      val = 0;
    // Clear the value of "buff"
    buff = "";