//Include Libraries: Use libraries for MPU6050 and LCD. For example:
#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//Initialize the Sensor and LCD:
MPU6050 mpu;
//LiquidCrystal lcd(12, 11, 10, 5, 4, 3);
//Setup Function: Initialize the sensor and LCD in the setup() function:
void setup() {
//lcd.init();
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello, World!");
delay(1000);
Wire.begin();
mpu.initialize();
//lcd.begin(16, 2);
}
//Read and Calculate Angles: In the loop() function, read the accelerometer and gyroscope data, //calculate the angles, and display them:
void loop() {
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// float angleX = atan2(ay, az) * 180 / PI;
// float angleY = atan2(ax, az) * 180 / PI;
float angleX = atan2(gy, gz) * 180 / PI;
float angleY = atan2(gx, gz) * 180 / PI;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("X: ");
lcd.print(angleX);
lcd.setCursor(0, 1);
lcd.print("Y: ");
lcd.print(angleY);
delay(500);
}