Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

I2C stability using Feather ESP32 and MPU6050 #3701

Copy link
Copy link
Closed
@ABKingbird

Description

@ABKingbird
Issue body actions

Hardware:

Board: Adafruit ESP32 Feather Huzzah
Core Installation version: 1.04
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: ?no? ?yes?
Upload Speed: 115200
Computer OS: Windows 10
Debug Level: Verbose

Description:

Hello, I have an MPU6050 Accelerometer connected to my ESP32 Feather via I2C and I am having some problems with the stability of the connection(see below). I can get some values for like seconds/minutes before it crashes due to I2C connection probably.
The sketch I am using makes use of the DMP, FIFO and Interrupt signals of the MPU6050. If I instead don't use the above(DMP, FIFO, and Int) and just get the raw values of the MPU, it works almost perfectly!

Sketch:

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer


// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector


#define INTERRUPT_PIN 12
volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    mpuInterrupt = true;
}


void setup(){
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
                                        //^ doesn't make a difference, still crashes
  
  mpu.initialize();

  // verify connection
  Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  // load and configure the DMP
  devStatus = mpu.dmpInitialize();
  pinMode(INTERRUPT_PIN, INPUT);

  // set Offsets
  mpu.setXAccelOffset(2220);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(380); 
  mpu.setXGyroOffset(13);
  mpu.setYGyroOffset(23);
  mpu.setZGyroOffset(14);

  // set Rate
  mpu.setRate(19);
    
  // make sure it worked (returns 0 if so)
  if (devStatus == 0) {
  
    // turn on the DMP, now that it's ready
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);
    
    // enable Arduino interrupt detection
//    Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
//    Serial.print(digitalPinToInterrupt(INTERRUPT_PIN));
//    Serial.println(F(")..."));
    attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();
        
    // set our DMP Ready flag so the main loop() function knows it's okay to use it
//    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    dmpReady = true;
    
    // get expected DMP packet size for later comparison
    packetSize = mpu.dmpGetFIFOPacketSize();
  }
  else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
    
  }
}

void loop(){
  if (!dmpReady) {
    return;
  }

  while (!mpuInterrupt && fifoCount < packetSize) {
    if (!mpuInterrupt && fifoCount < packetSize) {
      fifoCount = mpu.getFIFOCount();
    }
  }

  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  fifoCount = mpu.getFIFOCount();
  if(fifoCount < packetSize){
//    Serial.println("fifoCount < packetSize");
  }
  
  else if ((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) || fifoCount >= 1024) {
    // reset so we can continue cleanly
    mpu.resetFIFO();
//    Serial.println(F("FIFO overflow!"));
  } 
  
  else if (mpuIntStatus & _BV(MPU6050_INTERRUPT_DMP_INT_BIT)) {

    // read a packet from FIFO
    while(fifoCount >= packetSize){ 
      mpu.getFIFOBytes(fifoBuffer, packetSize);
      fifoCount -= packetSize;
      }

    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetAccel(&aa, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
    mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
//    Serial.print("areal\t");
    Serial.print(aaReal.x);
    Serial.print("\t");
    Serial.print(aaReal.y);
    Serial.print("\t");
    Serial.print(aaReal.z);
    Serial.print("\n");
//    Serial.println(metriseis);
  }
}

Debug Messages:

While working, it shows me this error, but still continues:

18:02:40.032 -> 3	-2	-19
18:02:40.079 -> 3	-3	-20
18:02:40.126 -> 2	-3	-19
18:02:40.173 -> 5	0	-17
18:02:40.207 -> 4	-1	-15
18:02:40.242 -> 3	-5	-19
18:02:40.276 -> 5	-5	-28
18:02:40.318 -> -2	-2	-28
18:02:40.351 -> -4	-4	-25
18:02:40.400 -> -3	-3	-32
18:02:40.466 -> [E][esp32-hal-i2c.c:318] i2cDumpI2c(): i2c=0x3ffbebec
18:02:40.466 -> [I][esp32-hal-i2c.c:319] i2cDumpI2c(): dev=0x60013000 date=0x16042000
18:02:40.466 -> [I][esp32-hal-i2c.c:321] i2cDumpI2c(): lock=0x3ffb843c
18:02:40.466 -> [I][esp32-hal-i2c.c:323] i2cDumpI2c(): num=0
18:02:40.466 -> [I][esp32-hal-i2c.c:324] i2cDumpI2c(): mode=1
18:02:40.466 -> [I][esp32-hal-i2c.c:325] i2cDumpI2c(): stage=3
18:02:40.466 -> [I][esp32-hal-i2c.c:326] i2cDumpI2c(): error=5
18:02:40.466 -> [I][esp32-hal-i2c.c:327] i2cDumpI2c(): event=0x3ffb84c0 bits=312
18:02:40.466 -> [I][esp32-hal-i2c.c:328] i2cDumpI2c(): intr_handle=0x3ffb84f0
18:02:40.466 -> [I][esp32-hal-i2c.c:329] i2cDumpI2c(): dq=0x3ffb8584
18:02:40.500 -> [I][esp32-hal-i2c.c:330] i2cDumpI2c(): queueCount=1
18:02:40.500 -> [I][esp32-hal-i2c.c:331] i2cDumpI2c(): queuePos=0
18:02:40.500 -> [I][esp32-hal-i2c.c:332] i2cDumpI2c(): errorByteCnt=-1
18:02:40.500 -> [I][esp32-hal-i2c.c:333] i2cDumpI2c(): errorQueue=0
18:02:40.500 -> [I][esp32-hal-i2c.c:334] i2cDumpI2c(): debugFlags=0x00000000
18:02:40.500 -> [I][esp32-hal-i2c.c:311] i2cDumpDqData(): Debug Buffer not Enabled
18:02:40.500 -> [I][esp32-hal-i2c.c:354] i2cDumpInts(): Debug Buffer not Enabled
18:02:40.500 -> [I][esp32-hal-i2c.c:1130] i2cProcQueue(): Bus busy, reinit
18:02:40.500 -> -8	-1	-31
18:02:40.535 -> -7	-2	-25
18:02:40.585 -> -1	-3	-26
18:02:40.585 -> -1	-5	-23
18:02:40.632 -> -1	-5	-17
18:02:40.679 -> 1	-5	-19
18:02:40.726 -> 2	-1	-20

And finally, it shows me this and stops working:

18:05:33.998 -> 1	-1	-10
18:05:34.032 -> -1	-1	-11
18:05:34.098 -> [E][esp32-hal-i2c.c:318] i2cDumpI2c(): i2c=0x3ffbebec
18:05:34.098 -> [I][esp32-hal-i2c.c:319] i2cDumpI2c(): dev=0x60013000 date=0x16042000
18:05:34.098 -> [I][esp32-hal-i2c.c:321] i2cDumpI2c(): lock=0x3ffb843c
18:05:34.098 -> [I][esp32-hal-i2c.c:323] i2cDumpI2c(): num=0
18:05:34.098 -> [I][esp32-hal-i2c.c:324] i2cDumpI2c(): mode=1
18:05:34.098 -> [I][esp32-hal-i2c.c:325] i2cDumpI2c(): stage=3
18:05:34.098 -> [I][esp32-hal-i2c.c:326] i2cDumpI2c(): error=5
18:05:34.098 -> [I][esp32-hal-i2c.c:327] i2cDumpI2c(): event=0x3ffb84c0 bits=312
18:05:34.098 -> [I][esp32-hal-i2c.c:328] i2cDumpI2c(): intr_handle=0x3ffb84f0
18:05:34.098 -> [I][esp32-hal-i2c.c:329] i2cDumpI2c(): dq=0x3ffb8584
18:05:34.134 -> [I][esp32-hal-i2c.c:330] i2cDumpI2c(): queueCount=1
18:05:34.134 -> [I][esp32-hal-i2c.c:331] i2cDumpI2c(): queuePos=0
18:05:34.134 -> [I][esp32-hal-i2c.c:332] i2cDumpI2c(): errorByteCnt=-1
18:05:34.134 -> [I][esp32-hal-i2c.c:333] i2cDumpI2c(): errorQueue=0
18:05:34.134 -> [I][esp32-hal-i2c.c:334] i2cDumpI2c(): debugFlags=0x00000000
18:05:34.134 -> [I][esp32-hal-i2c.c:311] i2cDumpDqData(): Debug Buffer not Enabled
18:05:34.134 -> [I][esp32-hal-i2c.c:354] i2cDumpInts(): Debug Buffer not Enabled
18:05:34.134 -> [I][esp32-hal-i2c.c:1130] i2cProcQueue(): Bus busy, reinit

After crashing, it just stays like that forever, and it shows me this after reset button has been pressed:

18:09:04.859 -> ets Jun  8 2016 00:22:57
18:09:04.859 -> 
18:09:04.859 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
18:09:04.859 -> configsip: 0, SPIWP:0xee
18:09:04.859 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
18:09:04.859 -> mode:DIO, clock div:1
18:09:04.859 -> load:0x3fff0018,len:4
18:09:04.859 -> load:0x3fff001c,len:1044
18:09:04.859 -> load:0x40078000,len:8896
18:09:04.859 -> load:0x40080400,len:5816
18:09:04.859 -> entry 0x400806ac
18:09:05.046 -> Checking hardware revision...
18:09:05.046 -> Revision @ user[16][6] = 0
18:09:05.046 -> Resetting memory bank selection to 0...
18:09:05.046 -> DMP Initialization failed (code 1)

meaning it can't get DMP to work again, no matter how many times I reset it. Code 1 is, as stated above in the sketch, something about "initial memory load failed" . To get over that, I simply plug the Vcc pin out of the MPU and put it back on.

Thank you in advance.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.