MPU6050 stops working if ESP32 is disconnected from PC (Hot-Plugging)

Yash28
Posts: 4
Joined: Tue Dec 19, 2023 9:29 pm

MPU6050 stops working if ESP32 is disconnected from PC (Hot-Plugging)

Postby Yash28 » Sun Feb 04, 2024 12:34 am

I am trying to get raw data from MPU6050 continuously, and for the sake of simplicity, I'm only reading a single register from it. It works fine when I build and flash the code. However, if I unplug the ESP32 from the PC, and plug it back again to simulate power failure, all I see is zero from the register. Even if I re-flash the following code after disconnecting and reconnecting several times, nothing changes. I keep getting 0.

Here's the code I am using in ESP-IDF (Latest Stable Version 5.1.2)
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "esp_log.h"
  4. #include "driver/i2c.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7.  
  8. #define I2C_MASTER_FREQ_HZ 400000
  9. #define MPU6050 0x68
  10.  
  11. static const char *TAG = "i2c-simple-example";
  12.  
  13. const i2c_config_t config={
  14.     .mode = I2C_MODE_MASTER,
  15.     .sda_io_num = 21,
  16.     .sda_pullup_en = GPIO_PULLUP_ENABLE,
  17.     .scl_io_num = 22,
  18.     .scl_pullup_en = GPIO_PULLUP_ENABLE,
  19.     .master.clk_speed = I2C_MASTER_FREQ_HZ
  20. };
  21.  
  22. esp_err_t ret;
  23.  
  24. void app_main(void)
  25. {
  26.     uint8_t reg_address_gyro_x = 0x3B;
  27.     uint8_t data[2];
  28.  
  29.     i2c_param_config(I2C_NUM_0, &config);
  30.     i2c_driver_install(I2C_NUM_0, config.mode, 0, 0, 0);
  31.  
  32.     while(true){
  33.         i2c_master_write_read_device(I2C_NUM_0, MPU6050, &reg_address_gyro_x, 1, data, 1, 1000 / portTICK_PERIOD_MS);
  34.         ESP_LOGI(TAG, "%X", data[0]);
  35.         vTaskDelay(1000/portTICK_PERIOD_MS);
  36.     }
  37. }

Now the funny thing is, if I then use an example from Arduino MPU6050 library, and then come back and upload this code again, it starts to work. So there is something happening in the arduino IDE code that is fixing the issue caused due to hot-plugging of the microcontroller, but I don't know what it is. I'm also uploading the arduino code here.

Code: Select all

#include "I2Cdev.h"
#include "MPU6050.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif
MPU6050 accelgyro;

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

#define OUTPUT_READABLE_ACCELGYRO

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    // initialize serial communication
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED pin for output
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    #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

    #ifdef OUTPUT_BINARY_ACCELGYRO
        Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
        Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
        Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
        Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
        Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
        Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
    #endif
}

Can someone tell me what might be the issue here? What is the arduino code doing that my code is not doing?

Yash28
Posts: 4
Joined: Tue Dec 19, 2023 9:29 pm

Re: MPU6050 stops working if ESP32 is disconnected from PC (Hot-Plugging)

Postby Yash28 » Fri Feb 16, 2024 5:08 am

I have figured out the solution for this issue. It was with the MPU6050 itself.

When the esp32 is disconnected from the computer, the MPU6050 goes to sleep mode. So when I connect it again, it needs to be woken up.

It's pretty straightforward. Just write a 0 on the 6th bit of the power management register using i2c write.

Who is online

Users browsing this forum: Baidu [Spider], Majestic-12 [Bot] and 129 guests