#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("ESP32 MPU6050 Test");
Wire.begin(21, 22); // SDA, SCL
if (!mpu.begin()) {
Serial.println("MPU6050 not found!");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Initialized!");
}
void loop() {
sensors_event_t accel, gyro, temp;
mpu.getEvent(&accel, &gyro, &temp);
Serial.print("Accelerometer (m/s^2)");
Serial.print("X: ");
Serial.print(accel.acceleration.x);
Serial.print(" Y: ");
Serial.print(accel.acceleration.y);
Serial.print(" Z: ");
Serial.println(accel.acceleration.z);
Serial.print("Gyroscope (rad/s)");
Serial.print("X: ");
Serial.print(gyro.gyro.x);
Serial.print(" Y: ");
Serial.print(gyro.gyro.y);
Serial.print(" Z: ");
Serial.println(gyro.gyro.z);
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" °C");
// Serial.println("-------------------------");
// Gesture detection
if (accel.acceleration.x) > 12 || (accel.acceleration.y) > 12) {
Serial.println("Gesture: SHAKE");
}
else if (accel.acceleration.x > 8) {
Serial.println("Gesture: RIGHT");
}
else if (accel.acceleration.x < -8) {
Serial.println("Gesture: LEFT");
}
else if (accel.acceleration.y > 8) {
Serial.println("Gesture: UP");
}
else if (accel.acceleration.y < -8) {
Serial.println("Gesture: DOWN");
}
delay(2000);
}