MPU6050 has i2c error,how can fix this?

#include <Arduino.h>
#include <TFT_eSPI.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

#define WIDTH  128
#define HEIGHT 160

#define OUTPUT_READABLE_ACCELGYRO

int16_t ax, ay, az;
int16_t gx, gy, gz;

MPU6050 accelgyro;
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft); 


void setup() {
  Serial.begin(115200);
  Wire.begin();

  accelgyro.setXGyroOffset(220);
  accelgyro.setYGyroOffset(76);
  accelgyro.setZGyroOffset(-85);

  spr.createSprite(WIDTH, HEIGHT); 
  tft.init();
  tft.setRotation(1);
  spr.fillScreen(TFT_BLACK);
  spr.setTextFont(2);
  spr.setTextSize(1);
  spr.setTextColor(TFT_GREEN);
  
  accelgyro.initialize();
  accelgyro.testConnection();
  
  if (accelgyro.testConnection())
   {
    Serial.println("MPU6050 connection successful");}
    
    else
    {Serial1.println("MPU6050 connection failed");}
  

}
  
  
void loop() {

  // accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  accelgyro.getAcceleration(&ax, &ay, &az);
  accelgyro.getRotation(&gx, &gy, &gz);
  

  spr.fillScreen(TFT_BLACK);
  spr.setCursor(0, 0);
  
  #ifdef OUTPUT_READABLE_ACCELGYRO
    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);
  #endif

  spr.drawString("X: " +String(ax), 0, 0);
  spr.drawString("Y: " +String(ay), 0, 40);
  spr.drawString("Z: " +String(az), 0, 80);
  spr.drawString("GX: " +String(gx), 70, 0);
  spr.drawString("GY: " +String(gy), 70, 40);
  spr.drawString("GZ: " +String(gz), 70, 80);


  spr.pushSprite(0,0);
  
  delay(1000);
  
}

Hi everyone, this is my code. After uploading, I successfully read the sensor data. When I unplug the Type-C plug from the esp32 and plug it back in, I get an error and cannot read the data. This is what I see from the serial monitor. Error received: [8200][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned error -1

I've tried re-uploading the code and pressing rest but it didn't fix the error, thanks for the help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.