MPU9250: call to delay() freezes the sensor

About the MPU9250, I can run successfully
the basic tutorial taken here

#include "MPU9250.h"
MPU9250 mpu;

void setup() {
Serial.begin(9600);
Wire.begin();
delay(2000);
mpu.setup(0x68);
}

void loop()
{
if (mpu.update()) {
Serial.print(mpu.getYaw()); Serial.print(", ");
Serial.print(mpu.getPitch()); Serial.print(", ");
Serial.println(mpu.getRoll());
}

//This delay makes the sensor freeze?
//delay(100); //persist with 15 ms , disappear with 10 ms
}

Nevertheless, if I insert the above commented delay(), the sensor reading always report the following fixed values (always the same):

-7.51, 0.00, 0.00
-7.51, 0.00, 0.00
-7.51, 0.00, 0.00

The problem persists if the delay is 15 ms or greater, and disappear for lower values.
Any idea why?
System: Linux Ubuntu Mate 20.04
Arduino ide 1.8.13
Library: MPU9250 0.4.4 by Hideakitai

Thanks a lot for help.

When you call the mpu.setup() function without settings, then it uses the default settings which sets the sample rate at 200Hz which is stored in the FIFO. You have to be fast enough to read the data from the FIFO.

I'm not happy with that library.
The "simple.ino" example has this loop() function:

void loop() {
    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
            print_roll_pitch_yaw();
            prev_ms = millis();
        }
    }
}

I don't understand why there is a combination of a millis-timer and the mpu.update() function. The millis-timer is also wrong.

I can confirm that the following lines in the setup() solved the problem:

MPU9250Setting setting;
setting.fifo_sample_rate = FIFO_SAMPLE_RATE::SMPL_125HZ;
mpu.setup(0x68, setting)

Now I can read meaningful results each 100 ms in the loop().
Thanks a lot,
Valerio

We rectify our previous answer.
We managed to solve the problem changing the magnetometer data read parameter in the MPU9250.h file.
In detail, we simply changed the MAG_MODE parameter (line 85), setting it to 0x02 (default is 0x06).
With this change, the mpu.update() function returns reliable data even if called with an interval up to 200 ms, without freezing.

Hope this helps.

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