int piezo_out = A0; // Piezo sensor connected to analog pin A0
int led_out = 13; // LED connected to pin 13
int threshold = 100; // Threshold for vibration detection
void setup() {
pinMode(led_out, OUTPUT); // Set LED as output
Serial.begin(9600); // Initialize Serial Monitor
}
void loop() {
int value = analogRead(piezo_out); // Read analog value from piezo sensor
Serial.println(value); // Print sensor value to Serial Monitor
if (value >= threshold) { // If vibration exceeds threshold
digitalWrite(led_out, HIGH); // Turn ON LED
} else {
digitalWrite(led_out, LOW); // Turn OFF LED
}
delay(100); // Short delay for stability
}
Instruction Details
Wiring: Connect piezoelectric sensor positive to Analog Pin A0, negative to GND.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: Onboard LED (Pin 13) turns ON when vibration exceeds threshold. Open Serial Monitor at 9600 baud to see analog values. Tap sensor to test.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output