#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MPU6050.h>
#include <math.h>
// Set the LCD address (you can use the default address or change it based on your module)
#define I2C_ADDR 0x27
// Set the LCD dimensions (16 columns and 2 rows for a 16x2 display)
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// Set the SDA and SCL pins on the ESP32
#define SDA_PIN 21
#define SCL_PIN 22
// Create a LiquidCrystal_I2C object and pass the I2C address, columns, and rows
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
// Create an MPU6050 object
MPU6050 mpu;
void setup() {
// Start the I2C communication
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the LCD
lcd.begin(LCD_COLUMNS, LCD_ROWS);
// Initialize the MPU6050
mpu.initialize();
// Print a welcome message
lcd.print("MPU6050 Reading");
delay(2000);
}
void loop() {
// Clear the LCD screen
lcd.clear();
// Read accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Compute vibration rating
float vibration = sqrt(pow(ax, 2) + pow(ay, 2) + pow(az, 2));
// Print vibration rating on the LCD
lcd.setCursor(0, 0);
lcd.print("Vibration: ");
lcd.print(vibration);
// Wait for a moment
delay(2000);
}