I have a door lock project, and I planned to use piezo element sensors as an alternative matrix keypad.
I'll arrange it in matrix pattern and I'll add resistors to endpoints then I'll connected it into one analog pin.
But before that, I tested to read a single piezo element in serial monitor (at 9600). I'm confused by the result because it gave continuous readings and it won't stop. How can I solve this, and make the reading only one?
I followed the fritz below but I placed the 1megohm resistor in the positive line only since it won't work if it is connected both to GND and +.
The code is included below, anyone who can explain this? Is it possible for a single piezo element to give a single reading?
Well, as you have the code in the cunningly named loop() function that should come as no surprise. Move the code into setup() to get one reading per power up or reset or leave it in loop() and set a boolean variable to true after printing and only print if the boolean is false
Of course there are readings, but what I expect you mean is that you don't understand the readings. What range of readings do you get with the knock sensor "open" ? What is the resistance of the sensor when "open" and closed ?
When pressed, the reading becomes 0 then continues to climb up 0000,1111,2222,...1000,1001,... up to 1023 the highest reading.
When the circuit is open, the readings vary, 412, 230, 10, 1000...
Can use the circuit diagram provided, but it could be better to read the piezo multiple times, so you don't miss a knock.
A lockout after a knock detection prevents a double read.
Try this sketch that I wrote some time ago for an alarm system.
Leo..
// 1" Piezo with 1Megohm resistor across, connected to A0 and ground
int threshold = 200; // 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 ledPin = 13; // onboard LED and/or buzzer on pin 13
void setup() {
Serial.begin(9600); // set serial monitor to this baud rate
pinMode (ledPin, OUTPUT);
}
void loop() {
piezoValue = 0; // reset
for (int x = 0; x < 250; x++) { // multiple A/D readings
rawValue = analogRead(piezoPin);
if (rawValue > piezoValue) {
piezoValue = rawValue; // store peaks
}
}
if (piezoValue) { // if > 0
Serial.println(piezoValue);
if (piezoValue >= threshold) {
Serial.print(F("Knock was over the threshold of "));
Serial.println(threshold);
digitalWrite (ledPin, HIGH);
delay(alarmDuration);
digitalWrite (ledPin, LOW);
}
}
}