// ESP32 with one MPU-6050 sensor
// --------------------------------
// Initial version for a single MPU-6050 sensor.
//
// 21 May 2024, Version 1, User, Public Domain.
// Simplified version for one sensor.
//
// This Wokwi project: https://wokwi.com/projects/394777263927329793
//
// A MPU-6050 sensor can have its I2C address at 0x68 or 0x69.
// This example uses the default address 0x68.
//
// Warning:
// In real life, this is only possible with a 3.3V board.
// A output pin with 5V output may not be connected
// directly to pin AD0 of the sensor.
//
// Attention:
// Wokwi keeps the settings in the sensor, which are seen
// as a offset when the calibration function runs.
//
// SU DUNG TRUC NAO CUNG DUOC, PHU THUOC VAO CACH MINH LAP RAP VA THAY DOI CODE
#include <Wire.h>
#include <MPU6050_light.h>
// Create the MPU6050 object. The sensor is on the "Wire" I2C bus.
MPU6050 mpu(Wire);
unsigned long previousMillis;
const unsigned long interval = 1000;
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin(); // default pins: SDA=21, SCL=22.
// Initialize the MPU-6050 sensor.
Serial.println(F("Initializing the sensor and calculating offsets. Do not move MPU-6050 sensor."));
Serial.println(F("Attention: Calculating the offset is good in real life, but it is better to skip this function in Wokwi."));
delay(500);
// Run the library initialization function .begin
byte status = mpu.begin();
Serial.print(F("Initializing MPU-6050, error = "));
Serial.print(status);
if(status == 0)
Serial.print(F(" (no error)"));
Serial.println();
mpu.calcOffsets(true,true); // gyro and accelerometer
Serial.println("Initialization done");
// A message to click on a sensor to change
// the settings when running in Wokwi.
Serial.println("Click on the MPU-6050 module and change the acceleration and gyro.");
// The setup() took so long,
// set previousMillis to a fresh value.
previousMillis = millis();
}
void loop()
{
unsigned long currentMillis = millis();
// Update, this keeps the library going
mpu.update();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
Serial.print(F("T="));
Serial.print(mpu.getTemp(),0);
Serial.print(F(",ACC="));
Serial.print(mpu.getAccX());
Serial.print(",");
Serial.print(mpu.getAccY());
Serial.print(",");
Serial.print(mpu.getAccZ());
Serial.print(F(",GYRO="));
Serial.print(mpu.getGyroX(),0);
Serial.print(",");
Serial.print(mpu.getGyroY(),0);
Serial.print(",");
Serial.print(mpu.getGyroZ(),0);
Serial.print(F(",ACC ANGLE="));
Serial.print(mpu.getAccAngleX(),0);
Serial.print(",");
Serial.print(mpu.getAccAngleY(),0);
Serial.print(F(",ANGLE="));
Serial.print(mpu.getAngleX(),0);
Serial.print(",");
Serial.print(mpu.getAngleY(),0);
Serial.print(",");
Serial.print(mpu.getAngleZ(),0);
Serial.println();
Serial.println(F("======================================================================================\n"));
}
}