Knock Sensor with a Piezo Element

KnockSens_piezotri


Piezo elements are quite interesting parts and useful for a lot of more purposes then making beep sounds. In this case we use a piezo element “reversed” as a kind of microphone for sensing subaudio sounds like knocks, vibrations or other impact sound sources. The setup is quite easy. Take a piezo disk and connect two wires one soldered to the middle electrode and the other to metall plate on the edge of the disk. These wires are connected to ground and analog input of the Arduino or you better use our SensorAktor Shield which has an built-in high impedance amplifier. The piezo disk has to be supported on the edge of the disk by some stands for instance like a tripod superglue some plastic rods to the edge. The most important thing is the reaction force which is provided by an 50 gram fishing plumb  weight glued in the middle of the disk. This setup is able to detect footsteps in a room or even keyboard strokes when the sensor is placed on the same table.

The Software averages the incoming audio signal and subtracts the actual audio signal to get rid of any dc offset. If the signal level is over a threshold adjustable by an potentiometer a led is turned on. A metro timer sets  the sampling rate to 5 ms to measure deep frequencies.



KnockSens_SA



Fingers  drumming gently on a table are producing an easy detectable signal.


KnockSens_osc




/* Knock Sensor with Piezo Disk connected to Mic Input

 *
 *  SensorAktor Shield V2
 *

 * Code und Material Workshop
 * Lab3 2010
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 * http://interface.khm.de

 * pin mapping SensorAktor Shield (import  to get already defined mapping)
 *
 * analog inputs: a0 = pin0, a1 = pin1, a2 = pin2, a3 = pin3, a4 = pin4, a5 = pin5
 * built-in poti: poti = pin5
 * amplified input: mic = pin0
 * switches: s1 = pin1, s2 = pin2, s3 = pin3
 * leds: led1 = pin3, led2 = pin5, led3 = pin6
 * power outs: out1 = pin3, out2 = pin5, out3 = pin6
 * motor outs: motor1_1 = pin11, motor1_2 = pin12, motor2_1 = pin13, motor2_2 = pin8
 * motor pwms: pwmMotor1 = pin9, pwmMotor2 = pin10
 * servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7

 */
// import sensor aktor library for pin mapping
#include
#include 

int potiValue;
int aPiezo,aPiezoAverage;
int adiff;
int timer10ms;
int threshold;
int sens;

Metro tMetro = Metro(5); 

void setup () {

  Serial.begin(115400);

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);

}

// this block is executed in a loop after setup is called one time
void loop(){
  if ( tMetro.check() ) {

    aPiezo=analogRead(analog0);      // read piezo Sensor
    potiValue=analogRead(analog5);   // read poti as analog threshold
    threshold=potiValue/10;
    if (aPiezo < aPiezoAverage) aPiezoAverage--;    // analog value average
    if (aPiezo > aPiezoAverage) aPiezoAverage++;

    adiff=aPiezo - aPiezoAverage;    // analog input as difference to average / eliminate DC offset
    adiff=abs(adiff);                // difference absolte value

    if (adiff > threshold ) sens++;  // if difference greater then threshold incremet sensor event

    if (timer10ms++ > 100)  // check within half a second
    {
      digitalWrite(led1,0);
      if (sens > 50) digitalWrite(led1,1);  // if more then x sensor event within half a second trigger

      Serial.print("thresh:");
      Serial.print(threshold);
      Serial.print("  aPiezo:");
      Serial.print(aPiezo);
      Serial.print("  Mittel:");
      Serial.print(aPiezoAverage);
      Serial.print(" diff:");
      Serial.print(adiff);
      Serial.print(" sens:");
      Serial.print(sens);
      Serial.println("");
      sens=0;
      timer10ms=0;
    }
  }

}


Martin Nawrath  / KHM 2010