#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.initialize();
// Check if the MPU6050 is connected
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed");
while (1);
}
Serial.println("MPU6050 connection successful");
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
// Get accelerometer and gyroscope readings
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Convert to G-force (assuming +/-2g range)
float accelX = ax / 16384.0;
float accelY = ay / 16384.0;
float accelZ = az / 16384.0;
// Calculate the magnitude of the acceleration vector
float magnitude = sqrt(accelX * accelX + accelY * accelY + accelZ * accelZ);
Serial.print("Acceleration magnitude: ");
Serial.println(magnitude);
// Simple fall detection: Check if the magnitude is significantly lower than 1g
if (magnitude < 0.5) {
Serial.println("Fall detected!");
// You can add code here to trigger an airbag inflator, send a signal, etc.
}
delay(500);
}