MPU6050 stops working if ESP32 is disconnected from PC (Hot-Plugging)
Posted: 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)
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.
Can someone tell me what might be the issue here? What is the arduino code doing that my code is not doing?
Here's the code I am using in ESP-IDF (Latest Stable Version 5.1.2)
- #include <stdio.h>
- #include <stdint.h>
- #include "esp_log.h"
- #include "driver/i2c.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #define I2C_MASTER_FREQ_HZ 400000
- #define MPU6050 0x68
- static const char *TAG = "i2c-simple-example";
- const i2c_config_t config={
- .mode = I2C_MODE_MASTER,
- .sda_io_num = 21,
- .sda_pullup_en = GPIO_PULLUP_ENABLE,
- .scl_io_num = 22,
- .scl_pullup_en = GPIO_PULLUP_ENABLE,
- .master.clk_speed = I2C_MASTER_FREQ_HZ
- };
- esp_err_t ret;
- void app_main(void)
- {
- uint8_t reg_address_gyro_x = 0x3B;
- uint8_t data[2];
- i2c_param_config(I2C_NUM_0, &config);
- i2c_driver_install(I2C_NUM_0, config.mode, 0, 0, 0);
- while(true){
- i2c_master_write_read_device(I2C_NUM_0, MPU6050, ®_address_gyro_x, 1, data, 1, 1000 / portTICK_PERIOD_MS);
- ESP_LOGI(TAG, "%X", data[0]);
- vTaskDelay(1000/portTICK_PERIOD_MS);
- }
- }
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
}