Arduino Program (The Code) - piezo_sensor_with_graph.ino
Written in C++ on the Arduino IDE
const int knockSensorPin = A3; // Pin connected to the piezo sensor
const int ledPin = 13;
// Pin connected to the LED
//LED Connected on Pin 13 of the PWM - pulse-width modulation on the Arduino
int threshold = 200;
// Adjust this value to set the knock sensitivity.
//A higher threshold means that a stronger or more pronounced knock would be required to exceed the threshold and trigger the action.
//On the other hand, a lower threshold would make the system more sensitive, allowing even weaker knocks to be detected.
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
Serial.begin(9600); // Initialize the serial communication at a baud rate of 9600.
}
//Baud rate is the rate at which the number of signal elements or changes to the signal occurs per second when it passes through a transmission medium.
void loop() {
int sensorValue = analogRead(knockSensorPin); // Read the sensor value. Reads the voltage level from the analog pin and converts it into a digital value. This digital value represents the sensor reading, which we refer to as the "sensor value" in this code.
//"Sensor Value" refers to the reading obtained from the piezo sensor connected to the Arduino's analog pin.
if (sensorValue >= threshold) {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(100); // Delay for LED visibility
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Send the sensor value to the serial plotter
Serial.println(sensorValue);
delay(50); // Delay between readings for stability
}Last updated