I am starting on my second project using an UNO R3. The first state will use the knock function, but I could not get my sketch to work. I backtracked and loaded and ran just the knock example. I can not get a knock to register, I have varied the thresholdLOW value and still can not get a knock to register. If I remove the patch cable from A0, the serial monitor begins to repeatedly display "knock" without any input knock. "Knock" will continue to display until I reattach the patch cable to A0. I have doubled checked my wiring and it is correct. Could I have a bad piezo? I can not understand how the Uno thinks that there is a knock when there is no connection at A0.
No code posted
No schematic posted
No help possible
Please follow the advice on posting code given in posting code
Or how you are creating the “knock “ - suggest you tap the sensor fairly hard with a screwdriver
When I made one of these I had to use an op amp to amplify the signal , but as said information needed !
[code]
/*
Knock Sensor
This sketch reads a piezo element to detect a knocking sound.
It reads an analog pin and compares the result to a set threshold.
If the result is greater than the threshold, it writes "knock" to the serial
port, and toggles the LED on pin 13.
The circuit:
- positive connection of the piezo attached to analog in 0
- negative connection of the piezo attached to ground
- 1 megohm resistor attached from analog in 0 to ground
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Knock
*/
// these constants won't change:
const int ledPin = 13; // LED connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
}
delay(100); // delay to avoid overloading the serial port buffer
}
[/code]
Is your "piezo" a buzzer, or a passive disc?
I've built knock sensors and found that you really need to grab some data over time. Then check min/max for amplitude of the knock. Amplitude is the key. (I think)
-jim lee
Actually, there is an example sketch that you can run that prints the analog values to serial. Please load and run that, and then post a cut and paste of the serial output from that. Then we can see what the raw data looks like.
I mounted the piezo to a board with some relief holes for the mounting tabs so that the base of the piezo was completely touching the board and reset the threshold value to 5. Now with 4 or 5 solid knocks on the board, the serial monitor displays a single "knock". Knowing that my parts do work, I can resume working on my original sketch using a more elaborate knock function. Thanks for the suggestions.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.

