Page 1 of 2

problem using MPU-6050 Accel/Gyro with ESP32

Posted: Thu Aug 06, 2020 7:24 pm
by what2use
I am having problems getting the MPU-6050 Accelerometer & Gyro to work with my Heltec ESP32 Development board.

I can run the I2C scanner and it detects the MPU-6050 at 0x68

If I run any of the example sketches that came with the MPU6050.h library it does not get data from the MPU-6050

I tried the MPU6050_DMP6.ino and have tried various examples from the web. But it is an issue with the ESP32
The MPU-6050 works fine using the sketch on my Arduino Nano but not the ESP32

I have the MPU-6050 connected as follows:
MPU-6050 o left ESP32 on right
VCC to 3.3V
GND to GND
SDA to Pin 21
SCL to Pin 22

Any help is greatly appreciated

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Fri Aug 07, 2020 6:27 am
by Agree007
Does your mpu-6050 lib support the esp32 ?

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Fri Aug 07, 2020 5:26 pm
by what2use
It says that it does but I do not know if there are any unique things that need to be changed for the ESP32

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Fri Aug 07, 2020 11:08 pm
by what2use
I checked the github for the MPU6050 library and it does support ESP32

https://github.com/ElectronicCats/mpu60 ... /README.md

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sat Aug 08, 2020 6:45 am
by Agree007
I have not try that lib, so cant tell.

I did some test last year, using Kolban's code and it works fine for me:

https://github.com/nkolban/esp32-snippe ... /mpu6050.c

Some times it can be the naming of the I2C pins, so you can try swop them, depending on your mpu6050 module.

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sat Aug 08, 2020 8:24 pm
by what2use
I tried that snippet and on verify/compile it gives tons of errors

Code: Select all

Arduino: 1.8.12 (Windows 10), Board: "WiFi LoRa 32(V2), Disabled, 240MHz (WiFi/BT), 921600, None, REGION_EU868, None"

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino: In function 'void task_mpu6050(void*)':

ESP32_Accel_SNIPPET:43:18: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]

  conf.sda_io_num = PIN_SDA;

                  ^

ESP32_Accel_SNIPPET:44:18: error: invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]

  conf.scl_io_num = PIN_CLK;

                  ^

ESP32_Accel_SNIPPET:93:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data,   0));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

ESP32_Accel_SNIPPET:94:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+1, 0));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

ESP32_Accel_SNIPPET:95:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+2, 0));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

ESP32_Accel_SNIPPET:96:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+3, 0));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

ESP32_Accel_SNIPPET:97:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+4, 0));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

ESP32_Accel_SNIPPET:98:54: error: invalid conversion from 'int' to 'i2c_ack_type_t' [-fpermissive]

   ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data+5, 1));

                                                      ^

C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:37:51: note: in definition of macro '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);

                                                   ^

In file included from C:\Users\Tony\Desktop\ESP32_Accel_SNIPPET\ESP32_Accel_SNIPPET.ino:1:0:

C:\Users\Tony\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5/tools/sdk/include/driver/driver/i2c.h:306:11: note:   initializing argument 3 of 'esp_err_t i2c_master_read_byte(i2c_cmd_handle_t, uint8_t*, i2c_ack_type_t)'

 esp_err_t i2c_master_read_byte(i2c_cmd_handle_t cmd_handle, uint8_t* data, i2c_ack_type_t ack);

           ^

exit status 1
invalid conversion from 'int' to 'gpio_num_t' [-fpermissive]

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sat Aug 08, 2020 8:56 pm
by Agree007
Sorry, selected wrong link, you should use MPU6050 .h & .cpp from this link:
https://github.com/nkolban/esp32-snippe ... /cpp_utils

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sat Aug 08, 2020 11:46 pm
by what2use
How am I supposed to use these two files?
I do not see a download option

Plus if I just download these it will overwrite the other MPU6050 library which I am using for the accelerometer on an Arduino project

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sun Aug 09, 2020 6:43 am
by Agree007
You add the two file a local file to your program and included MPU6050.h in you Main file.

This way you dont overwrite any other files.

Re: problem using MPU-6050 Accel/Gyrp with ESP32

Posted: Sun Aug 09, 2020 8:26 pm
by what2use
Progress. I was able to get the MPU6050 to initialize. I don't know if it will work on any pin but I will have to test that after I get the interrupt to work.

I do not know how to setup and use a pin as an Interrupt on the ESP32. The accel/gyro generates an interrupt when data is available. Then I can view the readings when I figure this out.

I moved the MPU6050 to the first I2C bus, the one that the display is connected to.
This is SDA-Pin4 and SCL-Pin 15

The steps are as follows:
1) Load the heltec library
#include "heltec.h"

2) Now you can enable the built-in OLED display with this line
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, true /*Serial Enable*/);

3) The key to getting I2C to work seems to be that now you have to initialize the display then other devices can be used
Heltec.display -> display();

4) It is time to start the wire. It defaults to SDA-Pin4 and SCL-Pin 15
Wire.begin();

5) Now you can initialize the MPU6050
mpu.initialize();

The next step would be to enable the interrupt. On the Arduino Uno all you have to do is plug the MPU6050 INT pin into Uno pin 2 and it worked right away.

But I am not sure how to setup and use a GPIO pin as an Interrupt on the Heltec board