Some knock sensor code to try.
Multiple A/D readings, so it doesn't miss knock peaks.
Very sensitive if you set the threshold to 1.
With the bare piezo flat on a desk, with some soft weight on it, it can detect a pin dropping on the desk.
Leo..
// knock sensor/alarm
// Piezo, with 1Megohm load resistor across, connected to A0 and ground
// optional 5volt buzzer on pin 13
int threshold = 100; // alarm threshold from 1 (very sensitive) to 1022 <<<<<<<<
int alarmDuration = 100; // alarm duration in milliseconds <<<<<<<<
const byte piezoPin = A0;
int rawValue; // raw A/D readings
int piezoValue; // peak value
const byte onboardLED = 13; // onboard LED and/or buzzer
void setup() {
analogReference(INTERNAL); // remove this line if too sensitive
Serial.begin(9600); // serial monitor for raw piezo output
pinMode (onboardLED, OUTPUT);
}
void loop() {
// reset
piezoValue = 0;
// read
for (int x = 0; x < 250; x++) { // multiple A/D readings
rawValue = analogRead(piezoPin);
if (rawValue > piezoValue) {
piezoValue = rawValue; // store peaks
}
}
// print
if (piezoValue > 0) {
Serial.print(F("Piezo value is "));
Serial.println(piezoValue);
}
// action
if (piezoValue > threshold) {
Serial.print(F("The knock was over the threshold of "));
Serial.println(threshold);
digitalWrite (onboardLED, HIGH);
delay(alarmDuration);
digitalWrite (onboardLED, LOW);
}
}