Hi, I'm facing a problem while trying to use ESP32 and MPU6050. When I run the code, I get the following error:
assertion "0 && "i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS)"" failed: "file "../main/MPU6050.C", line 86, function: app_main abort() was called at PC 0x400d2a87 on core 0.
And at the 86th line of my code, there is:
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS));
I'm new to ESP32 and its world, so I'm really confused about it.
Here's my code (that I literally copied from Kolban):
#include <driver/i2c.h>
#include <stdio.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <math.h>
#include "sdkconfig.h"
#define PIN_SDA 21
#define PIN_CLK 22
#define I2C_ADDRESS 0x68 // I2C address of MPU6050
#define MPU6050_ACCEL_XOUT_H 0x3B
#define MPU6050_PWR_MGMT_1 0x6B
/*
* The following registers contain the primary data we are interested in
* 0x3B MPU6050_ACCEL_XOUT_H
* 0x3C MPU6050_ACCEL_XOUT_L
* 0x3D MPU6050_ACCEL_YOUT_H
* 0x3E MPU6050_ACCEL_YOUT_L
* 0x3F MPU6050_ACCEL_ZOUT_H
* 0x50 MPU6050_ACCEL_ZOUT_L
* 0x41 MPU6050_TEMP_OUT_H
* 0x42 MPU6050_TEMP_OUT_L
* 0x43 MPU6050_GYRO_XOUT_H
* 0x44 MPU6050_GYRO_XOUT_L
* 0x45 MPU6050_GYRO_YOUT_H
* 0x46 MPU6050_GYRO_YOUT_L
* 0x47 MPU6050_GYRO_ZOUT_H
* 0x48 MPU6050_GYRO_ZOUT_L
*/
static char tag[] = "mpu6050";
#undef ESP_ERROR_CHECK
#define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { ESP_LOGE("err", "esp_err_t = %d", rc); assert(0 && #x);} } while(0);
void app_main(void *ignore) {
printf(tag, ">> mpu6050");
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = PIN_SDA;
conf.scl_io_num = PIN_CLK;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
i2c_cmd_handle_t cmd;
vTaskDelay(200/portTICK_PERIOD_MS);
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1));
i2c_master_write_byte(cmd, MPU6050_ACCEL_XOUT_H, 1);
ESP_ERROR_CHECK(i2c_master_stop(cmd));
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1));
i2c_master_write_byte(cmd, MPU6050_PWR_MGMT_1, 1);
i2c_master_write_byte(cmd, 0, 1);
ESP_ERROR_CHECK(i2c_master_stop(cmd));
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
uint8_t data[14];
short accel_x;
short accel_y;
short accel_z;
while(1) {
// Tell the MPU6050 to position the internal register pointer to register
// MPU6050_ACCEL_XOUT_H.
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, MPU6050_ACCEL_XOUT_H, 1));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_READ, 1));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data, 0));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+1, 0));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+2, 0));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+3, 0));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+4, 0));
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+5, 1));
//i2c_master_read(cmd, data, sizeof(data), 1);
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS));
i2c_cmd_link_delete(cmd);
accel_x = (data[0] << 8) | data[1];
accel_y = (data[2] << 8) | data[3];
accel_z = (data[4] << 8) | data[5];
//ESP_LOGD(tag, "accel_x: %d, accel_y: %d, accel_z: %d", accel_x, accel_y, accel_z);
printf(tag, "accel_x: %d, accel_y: %d, accel_z: %d", accel_x, accel_y, accel_z);
vTaskDelay(500/portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
} // task_hmc5883l
Anything will help!
Thanks, guys!
ESP32 and MPU6050
Re: ESP32 and MPU6050
Hi,
I am facing the same issue. Did you finda asolution to it?
I am facing the same issue. Did you finda asolution to it?
Who is online
Users browsing this forum: No registered users and 65 guests