Variable runtime using ESP32 board and MPU6050

I'm currently trying to run the following code on an ESP32 board from HiLetgo with a MPU6050 gyroscope/accelerometer, and when I print the runtime it's normally around 1-2ms, but occasionally jumps to 124ms.

/* Get tilt angles on X and Y, and rotation angle on Z
 * Angles are given in degrees, displays on SSD1306 OLED
 * 
 * License: MIT
 */
#include <Wire.h>
#include <MPU6050_light.h>

MPU6050 mpu(Wire);
unsigned long timer = 0;

void setup() {
  Serial.begin(9600);  // Ensure serial monitor set to this value also

  Wire.begin();
  mpu.begin();
  mpu.calcGyroOffsets();
}

int past_millis = 0;

void loop() {
  mpu.update();

  Serial.print(mpu.getAngleX());
  Serial.print(",");

  Serial.print(mpu.getAngleZ());

  Serial.print(",");
  Serial.print(millis() - past_millis);

  Serial.println();

  past_millis = millis();
}

(the third comma separated value is the runtime)

This kind of jumping didn't happen when I tried running the code on an R3 Uno, and I'm fairly certain this has something to do w/ the esp32 board I'm using, as when I unplug the SDA or SCL pins & the gyroscope continues running, then runtime drops down to abt 1 ms, but the jumps up to around 123ms in runtime still remain.

I'm wondering if there's a way I can fix this or not. Any workarounds would be appreciated too.

The library you use is not really blocking waiting for a successful update so it would seem to indicate that the wire communication might get stuck? Did you try adding good pull-ups for your I2C? (4.7KΩ pull-up resistors on SCL and SDA, to either 5V or 3.3V depending on what your MPU does for the output - most MPU-6050 hardware I've seen use an I2C voltage I/O level of 3.3V with 5.0V I2C master)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.