Arduino +MPU 6050 How can I improve the output data rate?

I use the example script MPU6050_raw. And I set DLPF_CFG=0, SMPLRT_DIV=0.Therefore the sample rate of mpu 6050 should be 8 kHz. However, when I found out that the output process takes about 11 ms each time. That means I can only get the acceleration values every 11 ms instead of every 1/8 us because it takes about 11 ms to go through loop() one time. So some data are lost. And the rate of output data is not enough for me. I need more samples. So how can I let arduino output same amount of data that mpu 6050 produces. The loop() codes are as follows.

void loop() {
// read raw accel/gyro measurements from device
t1=millis();

accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);

#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g:\t");
Serial.print(ax/2048.0+0.09); Serial.print("\t");
Serial.print(ay/2048.0+0.02); Serial.print("\t");
Serial.print(az/2048.0-0.01); Serial.print("\t");
Serial.print(gx/131.0); Serial.print("\t");
Serial.print(gy/131.0); Serial.print("\t");
Serial.println(gz/131.0);
#endif
t2=millis();

e=t2-t1;
Serial.print("e=" );Serial.println(e);

}

Post ALL your code.
Set the serial baud rate higher.

Yes. Once the serial output buffer is full you can't send any more until characters leave the buffer. How many characters are you sending? Multiply that by 10 and divide by the bit rate and that is the minimum time it will take to do the output. You have to get that down from 11 milliseconds to 125 microseconds to update at 8 kHz.

I would switch to SPI mode if not using that already, it supports 1 MHz transfer rate, and only 8 bits vs 9. Then you can sent it out at 1MHZ serial rate, and not fall too far behind, as serial needs 10 bits to transmit (start bit, 8 data bits, 1 stop bit).
Course, you might not have much time other processing.

digitalWrite (csPin, LOW);
SPI.transfer(command_address); // read-write bit and 7-bit address register
registerData[address] = SPI.transfer(dummyByte); // read in data from MPU6050 address
registerData[address+1] = SPI.transfer(dummyByte); // Burst mode!
registerData[address+2] = SPI.transfer(dummyByte); // 
registerData[address+3] = SPI.transfer(dummyByte); // 
registerData[address+4] = SPI.transfer(dummyByte); // 
registerData[address+5] = SPI.transfer(dummyByte); // 
digitalWrite (csPin, HIGH);

Serial.write (registerData[address]);
Serial.write (registerData[address+1]);
Serial.write (registerData[address+2]);
Serial.write (registerData[address+3]);
Serial.write (registerData[address+4]);
Serial.write (registerData[address+5]);

At SPI speed of 1MHz, 7 transfers will take about 7 * 56uS plus the digitalWrite (which can be sped up with direct port manipulation).
The Serial.writes at 1 MHz will take about 70uS.
Total: 126+ uS, so 7,936.5 Hz.
Then you're doing some float processing:
ax/2048.0+0.09 and gx/131.0
and adding extra characters "\t"
so that will bog it all down some more.

Seems to me "You can't get there from here."

If the purpose of the Arduino is just to collect data for a faster computer to process, then the Arduino is really not necessary. As Crossroads suggests, it may not even be up to the task. Just get a fast interface to connect the MPU6050 directly with the data processing computer.

Hi;

Can you please post a copy of your entire sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom..... :slight_smile:

CrossRoads:
I would switch to SPI mode if not using that already, it supports 1 MHz transfer rate, and only 8 bits vs 9. Then you can sent it out at 1MHZ serial rate, and not fall too far behind, as serial needs 10 bits to transmit (start bit, 8 data bits, 1 stop bit).
Course, you might not have much time other processing.

digitalWrite (csPin, LOW);

SPI.transfer(command_address); // read-write bit and 7-bit address register
registerData[address] = SPI.transfer(dummyByte); // read in data from MPU6050 address
registerData[address+1] = SPI.transfer(dummyByte); // Burst mode!
registerData[address+2] = SPI.transfer(dummyByte); //
registerData[address+3] = SPI.transfer(dummyByte); //
registerData[address+4] = SPI.transfer(dummyByte); //
registerData[address+5] = SPI.transfer(dummyByte); //
digitalWrite (csPin, HIGH);

Serial.write (registerData[address]);
Serial.write (registerData[address+1]);
Serial.write (registerData[address+2]);
Serial.write (registerData[address+3]);
Serial.write (registerData[address+4]);
Serial.write (registerData[address+5]);



At SPI speed of 1MHz, 7 transfers will take about 7 * 56uS plus the digitalWrite (which can be sped up with direct port manipulation).
The Serial.writes at 1 MHz will take about 70uS.
Total: 126+ uS, so 7,936.5 Hz.
Then you're doing some float processing:
ax/2048.0+0.09 and gx/131.0
and adding extra characters "\t"
so that will bog it all down some more.

Seems to me "You can't get there from here."

Hi, I am very new to this but can MPU6050 be used in SPI interface??

Isnt it only MPU6000 can be used in SPI and MPU6050 only restricted to I2C mode??

Because if MPU6050 can be used in SPI interface then I will be so happy because I need my MPU6050 to work with faster sampling rate.

Please advise. Thank you.

"Isnt it only MPU6000 can be used in SPI and MPU6050 only restricted to I2C mode??"

Quite right! Sorry CrossRoads, 6050 don't do SPI, only the 6000. Quick ref the data sheet pg 21, sect 7.1 Pin outs.