Close

Arduino Workshop-Piezo Knock Sensor

mr-sarful-hassanMr. Sarful hassan wrote 06/15/2020 at 13:45 • 5 min read • Like
Today in this project we are going to create a Piezo Knock Sensor, A piezo disc works when an electric field (voltage) is applied across ceramic material in the disc, causing it to change shape and therefore make a sound (a click). The disc also works in reverse in that when the disc is knocked or squeezed, the force on the material causes an electric charge (voltage) to be generated. We can read that current using the Arduino and we are going to do that now by making a knock sensor. Required Component : 1.Arduino 2. Piezoelectric Sensor 3.  5mm  LED  4. Resistors 5. connecting wire 6. Breadboard
This book will help you to gain more knowledge about Arduino
Beginning Arduino Circuit diagram Arduino Piezo Knock Sensor  : First, make sure your Arduino is powered off by unplugging it from the USB cable. Then connect up your parts so that you have the circuit in Piezo Knock Sensor. A piezo disc works better for this project than a piezo sounder. The resistor on the piezo is there to discharge any piezo capacitance built up in the piezo while it is being used. [caption id="attachment_442" align="alignnone" width="300"]Piezo Knock Sensor Piezo Knock Sensor[/caption] Code Arduino Piezo Knock Sensor :
int ledPin = 9; // LED on Digital Pin 9
int piezoPin = 5; // Piezo on Analog Pin 5
int threshold = 120; // The sensor value to reach before
activation
int sensorValue = 0; // A variable to store the value read from
the sensor
float ledValue = 0; // The brightness of the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the ledPin to an OUTPUT
// Flash the LED twice to show the program has started
digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin,
LOW); delay(150);
digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin,
LOW); delay(150);
}
void loop() {
sensorValue = analogRead(piezoPin); // Read the value from
the sensor
if (sensorValue >= threshold) { // If knock detected set
brightness to max
ledValue = 255;
}
analogWrite(ledPin, int(ledValue) ); // Write brightness
value to LED
ledValue = ledValue - 0.05; // Dim the LED slowly
if (ledValue <= 0) { ledValue = 0;} // Make sure value does
not go below zero
}
After you have uploaded your code, the LED will flash quickly twice to show you that the program has started. You can now knock the sensor or squeeze it between your fingers. Every time the Arduino detects a knock or squeeze, the LED will light up and then gently fade back down to off. The threshold value in the code was set for the piezo disc I used when building the project. You may need to set this to a higher or lower value depending on the type and size of the piezo you have used for your project. Lower is more sensitive and higher is less ALL ARDUINO TUTORIAL 
Like

Discussions