Setting Sampling Rate ESP32 MPU6050


I’m trying to set the sampling rate of a MPU6050 accelerometer to a constant 100Hz or 200Hz and log the recorded data to either a microsd-card or send it per wifi to influxdb.

My current state:
I set the baudrate to 500000, only for testing purpose, how often a new value will be displayed in the serial monitor. I used millis() as a timestamp. Seems that maximal actualisation rate is around every 2ms to 3ms, which is more than I need.
→ setting the sampling rate of the MPU6050 to 100Hz by using the ‘setSampleRateDivision(9)’.
The output of the serial monitor still renews every 2ms to 3ms. However, the acceleration and gyro values are staying the same ± 3 to 4 times, which (I think) means that the MPU6050 is actually set to 100Hz, but these are requested more often. And with the limits of the serial monitor, I expect that the values are even requested even more often.
I also tried alternating the Wire.setClock(100000) and Wire.setClock(400000), but this didn’t change anything.

My Question:
How can I fix that the sampling rate of the ESP32 also stays at 100Hz or 200Hz?

My code:

//ESP32 MPU6050

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  //setClock has no impact here
  while (!Serial)

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
  Serial.println("MPU6050 Found!");

  // set sampling rate --> 0 stays the same... 1000 / 9+1 = 100Hz 


void loop() {
  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);
  /* Print out the values */
  Serial.print(" ");
  Serial.print("Acceleration X: ");
  Serial.print(", Y: ");
  Serial.print(", Z: ");
  Serial.print(" m/s^2 ");

  Serial.print("Rotation X: ");
  Serial.print(", Y: ");
  Serial.print(", Z: ");
  Serial.println(" rad/s");
  Serial.print("Temperature: ");
  Serial.println(" degC");
Serial.println(" degC");

With delay() the execution of the code before would add up to the delay, so that at one point one value would be skipped, which would be ignorable regarding only the accelerometer code.
But: I also want to include more code, like either saving the values to an sd-card or send them to an influxdb. The execution of that part is also rather time-consuming I think, so delay() would only add up to the time needed to run through the loop?

An ESP32. I’d use freeRTOS and set the mpu task to read at the proper time and run others tasks at their own rates.

I’ve looked FreeRTOS up now, it will be challenging for my programming skills, but offers a lot of possibilities. Thank you! I will try it and come back if I got further questions.

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