CODE | Piezo Knock Sensor - Arduino Project 013

RonWang2 years ago (2023-08-28)电子编程 COD2

A piezo disc works when an electric current is passed over the ceramic material in the disc, causing it to change shape and hence make a sound (a click). The disc also works in reverse: when the disc is knocked or squeezed, the force on the material causes the generation of an electric current. You can read that current using the Arduino and you are going to do that now by making a Knock Sensor.

项目Project 13  Piezo Knock Sensor 压电振动传感器

/* Coding Ron Wang
   Aug.13rd 2024
   Autaba support for coding hardware
 */
// Project 13 - 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
}

arduino Pieizo Knock Sensor Circuit

arduino Pieizo Knock Sensor Schematic

Share with Friends:

Related Articles

CODE | LED Chase Lights - Arduino Project 005

CODE | LED Chase Lights - Arduino Project 005

You’re going to use a string of LEDs (10 in total) to make an LED chase effect, similar to that used…

How to Understand and Select the Right Wire Specifications

How to Understand and Select the Right Wire Specifications

如何读懂和选用合适的电线的规格?初级电线标准和规格入门知识American Wire Gauge “AWG” Chart – Wire Size & Ampacity TableAmerica…

CODE | Piezo Sounder Alarm - Arduino Project 011

CODE | Piezo Sounder Alarm - Arduino Project 011

Arduino project - sounder and sensors : Include project 11 Piezo Sounder Alarm ,Project piezo sounde…

CODE | Line Following Robot - Arduino Project 030

CODE | Line Following Robot - Arduino Project 030

Project 30 Line Following RobotA line following robot is an autonomous vehicle that detects and foll…

CODE | LED Dot Matrix Display Basic Animation - Arduino Project 019

CODE | LED Dot Matrix Display Basic Animation - Arduino Project 019

So far you have dealt with individual 5mm LEDs. LEDs can also be obtained in a package known as a do…

CODE | Liquid Crystal Displays Autoscroll - Arduino Project 023A

CODE | Liquid Crystal Displays Autoscroll - Arduino Project 023A

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14…

Post a Comment

Anonymous

Feel free to share your thoughts and opinions here.