# InvenSense MPU6050 Sensor [](/LICENSE) [](https://visualstudio.microsoft.com) [](https://platformio.org/) [](https://registry.platformio.org/libraries/k0i05/esp_mpu6050) [](https://components.espressif.com/components/k0i05/esp_mpu6050) This ESP32 espressif IoT development framework (esp-idf) i2c peripheral driver was developed for the InvenSense MPU6050 6-axis motion tracking sensor. Information on features and functionality are documented and can be found in the `mpu6050.h` header file and in the `documentation` folder. ## Repository The component is hosted on github and is located here: <https://github.com/K0I05/ESP32-S3_ESP-IDF_COMPONENTS/tree/main/components/peripherals/i2c/esp_mpu6050> ## General Usage To get started, simply copy the component to your project's `components` folder and reference the `mpu6050.h` header file as an include. The component includes documentation for the peripheral such as the datasheet, application notes, and/or user manual where applicable. ```text components └── esp_mpu6050 ├── CMakeLists.txt ├── README.md ├── LICENSE ├── idf_component.yml ├── library.json ├── documentation │ └── datasheets, etc. ├── include │ └── mpu6050_version.h │ └── mpu6050.h └── mpu6050.c ``` ## Basic Example Once a driver instance is instantiated the sensor is ready for usage as shown in the below example. This basic implementation of the driver utilizes default configuration settings and makes a measurement request from the sensor at user defined interval and prints the results. ```c #include <mpu6050.h> void i2c0_mpu6050_task( void *pvParameters ) { // initialize the xLastWakeTime variable with the current time. TickType_t last_wake_time = xTaskGetTickCount (); // // initialize i2c device configuration mpu6050_config_t dev_cfg = I2C_MPU6050_CONFIG_DEFAULT; mpu6050_handle_t dev_hdl; // // init device mpu6050_init(i2c0_bus_hdl, &dev_cfg, &dev_hdl); if (dev_hdl == NULL) { ESP_LOGE(APP_TAG, "mpu6050 handle init failed"); assert(dev_hdl); } uint8_t sample_rate_divider_reg; mpu6050_config_register_t config_reg; mpu6050_gyro_config_register_t gyro_config_reg; mpu6050_accel_config_register_t accel_config_reg; mpu6050_interrupt_enable_register_t irq_enable_reg; mpu6050_power_management1_register_t power_management1_reg; mpu6050_power_management2_register_t power_management2_reg; mpu6050_who_am_i_register_t who_am_i_reg; /* attempt to read device sample rate divider register */ mpu6050_get_sample_rate_divider_register(dev_hdl, &sample_rate_divider_reg); /* attempt to read device configuration register */ mpu6050_get_config_register(dev_hdl, &config_reg); /* attempt to read device gyroscope configuration register */ mpu6050_get_gyro_config_register(dev_hdl, &gyro_config_reg); /* attempt to read device accelerometer configuration register */ mpu6050_get_accel_config_register(dev_hdl, &accel_config_reg); /* attempt to read device interrupt enable register */ mpu6050_get_interrupt_enable_register(dev_hdl, &irq_enable_reg); /* attempt to read device power management 1 register */ mpu6050_get_power_management1_register(dev_hdl, &power_management1_reg); /* attempt to read device power management 2 register */ mpu6050_get_power_management2_register(dev_hdl, &power_management2_reg); /* attempt to read device who am i register */ mpu6050_get_who_am_i_register(dev_hdl, &who_am_i_reg); // show registers ESP_LOGI(APP_TAG, "Sample Rate Divider Register: 0x%02x (%s)", sample_rate_divider_reg, uint8_to_binary(sample_rate_divider_reg)); ESP_LOGI(APP_TAG, "Configuration Register: 0x%02x (%s)", config_reg.reg, uint8_to_binary(config_reg.reg)); ESP_LOGI(APP_TAG, "Gyroscope Configuration Register: 0x%02x (%s)", gyro_config_reg.reg, uint8_to_binary(gyro_config_reg.reg)); ESP_LOGI(APP_TAG, "Accelerometer Configuration Register: 0x%02x (%s)", accel_config_reg.reg, uint8_to_binary(accel_config_reg.reg)); ESP_LOGI(APP_TAG, "Interrupt Enable Register: 0x%02x (%s)", irq_enable_reg.reg, uint8_to_binary(irq_enable_reg.reg)); ESP_LOGI(APP_TAG, "Power Management 1 Register: 0x%02x (%s)", power_management1_reg.reg, uint8_to_binary(power_management1_reg.reg)); ESP_LOGI(APP_TAG, "Power Management 2 Register: 0x%02x (%s)", power_management2_reg.reg, uint8_to_binary(power_management2_reg.reg)); ESP_LOGI(APP_TAG, "Who am I Register: 0x%02x (%s)", who_am_i_reg.reg, uint8_to_binary(who_am_i_reg.reg)); // // task loop entry point for ( ;; ) { ESP_LOGI(APP_TAG, "######################## MPU6050 - START #########################"); // // handle sensor float temperature; mpu6050_gyro_data_axes_t gyro_data; mpu6050_accel_data_axes_t accel_data; esp_err_t result = mpu6050_get_motion(dev_hdl, &gyro_data, &accel_data, &temperature); if(result != ESP_OK) { ESP_LOGE(APP_TAG, "mpu6050 device read failed (%s)", esp_err_to_name(result)); } else { /* pitch and roll */ float pitch = atanf(accel_data.x_axis / sqrtf(powf(accel_data.y_axis, 2.0f) + powf(accel_data.z_axis, 2.0f))); float roll = atanf(accel_data.y_axis / sqrtf(powf(accel_data.x_axis, 2.0f) + powf(accel_data.z_axis, 2.0f))); ESP_LOGI(APP_TAG, "Accelerometer X-Axis: %fg", accel_data.x_axis); ESP_LOGI(APP_TAG, "Accelerometer Y-Axis: %fg", accel_data.y_axis); ESP_LOGI(APP_TAG, "Accelerometer Z-Axis: %fg", accel_data.z_axis); ESP_LOGI(APP_TAG, "Gyroscope X-Axis: %f°/sec", gyro_data.x_axis); ESP_LOGI(APP_TAG, "Gyroscope Y-Axis: %f°/sec", gyro_data.y_axis); ESP_LOGI(APP_TAG, "Gyroscope Z-Axis: %f°/sec", gyro_data.z_axis); ESP_LOGI(APP_TAG, "Temperature: %f°C", temperature); ESP_LOGI(APP_TAG, "Pitch Angle: %f°", pitch); ESP_LOGI(APP_TAG, "Roll Angle: %f°", roll); } // ESP_LOGI(APP_TAG, "######################## MPU6050 - END ###########################"); // // // pause the task per defined wait period //vTaskDelaySecUntil( &last_wake_time, I2C0_TASK_SAMPLING_RATE / 2 ); vTaskDelaySecUntil( &last_wake_time, 1 ); } // // free resources mpu6050_delete( dev_hdl ); vTaskDelete( NULL ); } ``` Copyright (c) 2024 Eric Gionet (<gionet.c.eric@gmail.com>)
idf.py add-dependency "k0i05/esp_mpu6050^1.1.7"