// Reading Accelerometer, Gyroscope & Temperature data from the MPU6050
// Include the necessary libraries
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// define the LiquidCrystal constant
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 3
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Create an object for the MPU6050 sensor
Adafruit_MPU6050 mpu;
void setup() {
// put your setup code here, to run once:
// Initialize the serial communication
Serial.begin(9600);
// Initialize the Liquid Cristal display
lcd.init();
lcd.backlight();
// Check if the MPU6050 sensor is detected
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while(1) {
delay(10);
}
}
Serial.println("MPU Found! ");
// Set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// Set gyro range to +- 500 deg/sec
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// Set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Add a delay for stability
delay(100);
}
void loop() {
// put your main code here, to run repeatedly:
//Get new sensor events with the readings
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Display the acceleration readings in m/sˆ2
lcd.setCursor(0, 0);
lcd.print("Acceleration: (m/sˆ2) ");
lcd.setCursor(0,1);
lcd.print("X: ");
lcd.print(a.acceleration.x);
lcd.setCursor(0,2);
lcd.print("Y: ");
lcd.print(a.acceleration.y);
lcd.setCursor(0,3);
lcd.print("Z: ");
lcd.print(a.acceleration.z);
// Add a delay to avoid the display twinkle
delay(1250);
// Initialize the Liquid Cristal display
lcd.init();
// Display the rotation readings in rad/s
lcd.setCursor(0, 0);
lcd.print("Rotation: (rad/s)");
lcd.setCursor(0,1);
lcd.print("X: ");
lcd.print(g.gyro.x);
lcd.setCursor(0,2);
lcd.print("Y: ");
lcd.print(g.gyro.y);
lcd.setCursor(0,3);
lcd.print("Z: ");
lcd.print(g.gyro.z);
lcd.println(" (rad/s)");
// Add a delay to avoid flooding the serial monitor
delay(1250);
}