#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
const int ledPin = 13;
const int buzzerPin = 12;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU6050 Initialized!");
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Print accelerometer values
Serial.print("Accelerometer X: "); Serial.print(a.acceleration.x); Serial.print(" m/s^2, ");
Serial.print("Y: "); Serial.print(a.acceleration.y); Serial.print(" m/s^2, ");
Serial.print("Z: "); Serial.println(a.acceleration.z); Serial.print(" m/s^2");
// Vibration detection (adjust threshold as needed)
if (abs(a.acceleration.x) > 2.0 || abs(a.acceleration.y) > 2.0 || abs(a.acceleration.z) < 8.0) {
Serial.println("Vibration/Collapse Detected!");
digitalWrite(ledPin, HIGH); // Turn on LED
tone(buzzerPin, 1000, 500); // Sound alarm
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(500);
}