Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Mechatronics LAB
Published © Apache-2.0

Arduino Workshop-Piezo Knock Sensor

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 cer

BeginnerFull instructions provided1 hour13,382
Arduino Workshop-Piezo Knock Sensor

Things used in this project

Story

Read more

Schematics

Arduino Workshop-Piezo Knock Sensor

Code

Code snippet #1

Arduino
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
}

Credits

Mechatronics LAB
75 projects • 45 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments