This tutorial shows you how to use a Piezo element to detect vibration, in this case, a knock on a door, table, or other solid surface.
A piezo is an electronic device that produces a voltage when it undergoes physical deformation from a vibration, sound wave, or mechanical strain. Likewise, applying voltage to a piezo element causes it to vibrate and generate sound. Piezoelectric devices can be utilized for both producing tones and sensing tones.
The sketch utilizes the analogRead() command to interpret the piezos output, converting the voltage range of 0 to 5 volts to a numerical range of 0 to 1023 through analog-to-digital conversion (ADC).
When the sensor’s output exceeds a specific threshold, the Arduino will transmit the message “Knock!” to the computer through the serial port.
Access the serial monitor in order to view this text.
Circuit
Piezos are polarized, allowing voltage to flow through them (or out of them) in a particular orientation. Attach the low voltage black wire to the ground and the high voltage red wire to analog pin 0. Furthermore, add a 1-megohm resistor in parallel with the Piezo element to constrain the voltage and current generated by the piezo and safeguard the analog input.
Piezo elements can be obtained without a plastic enclosure. These will appear as a metal disc and are simpler to operate as input sensors. PIezo sensors are most effective when they are firmly pressed against, taped to, or glued to their sensing surface.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic:
Code
In the code below, the incoming piezo data is compared to a threshold value set by the user. Try raising or lowering this value to increase your sensor’s overall sensitivity.
/* 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.
Hardware Required
- Arduino Board
- (1) Piezo electric disc
- (1) Megohm resistor
- solid surface
For more detail: How to use a Piezo element to detect vibration using Arduino