3

mpu6050 has sampling frequency of 1kHz (mentioned in datasheet). But when i interface it with arduino uno via I2C communication (400Khz bus speed), i only get 50 raw data (each of x,y,z). There is no delay in the program. why is this happening. Why am I not getting 1000 or at least 800 samples. What is the concept I am missing here. Please explain in detail. I have also attached the code and output for 1 second.

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
//#include <SoftwareSerial.h>
//SoftwareSerial BTserial(10,11);
MPU6050 accelgyro;
char ch;
int incomingbyte = 0;
int16_t ax, ay, az;
int16_t gx, gy, gz;

void setup() {

  Wire.begin();
  Serial.begin(115200);

  accelgyro.initialize();
  accelgyro.setXAccelOffset(542);
  accelgyro.setYAccelOffset(-835);
  accelgyro.setZAccelOffset(1651);
  accelgyro.setXGyroOffset(6);
  accelgyro.setYGyroOffset(-18);
  accelgyro.setZGyroOffset(23);
}
void loop()
{
  accelgyro.getAcceleration(&ax, &ay, &az);
  accelgyro.getRotation(&gx, &gy, &gz);

  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.println(az);
}

enter image description here

5
  • the datasheet indicates that the sample rate is programmable ... looks like register 25 is involved ... this document describes the register functions ... invensense.tdk.com/wp-content/uploads/2015/02/…
    – jsotola
    May 3, 2020 at 17:18
  • HI @jsotola , thanks for the reply. I found out sampling rate divider that is at reg 0x25 , 255 was set. So the sampling rate should be either 31.25Hz DLPF is disabled or 3.9 hz when DLPF enabled according to the datasheet (pg no 12 in the link you have provided). Still i am getting more than these values around 50 samples. How come? Also sampling rate is different from data transmission rate from mpu6050 to arduino uno right? Can you explain in detail like how many bytes would transmitted through i2c and all those calculation stuffs. I am not getting that May 3, 2020 at 18:46
  • Set the I2C to 400kHz using Wire.setClock(), I think by default it's set to 100kHz. That would bump up the sampling rate from 50 Hz to 200 Hz. If that's not enough I think you have to disable the digital low pass filter. Mar 23, 2021 at 19:28
  • Your problem is the Serial.print which slows down the loop. Add a counter in your loop and do a serial.print just once in a while.
    – NickB
    Oct 28, 2021 at 19:43
  • i want to know what is the unit of this signal? for example what is the 5608 ?? in first row Feb 28, 2023 at 12:59

1 Answer 1

1

The reason is that Serial.print() is mush slower compared to reading a sample from the sensor.

If you comment the print(), then read sensor data for a certain time repeatedly and calculate the output data rate accordingly, you will find that the sampling rate is indeed 1000 Hz.

If what you wanted is merely to store the data and use it afterwards (instead of real-time processing), you can store the sensor readings in a SD card, which is always much faster than serial printing. A useful trick is that do not write data to the sd card whenever you get a new sample, but use a software buffer (i.e., array) to store the samples (the buffer size depends on the RAM of microcontroller) and then write the whole buffer to the sd card when it's full.

However, as long as you want to store the data (either print in serial or write in a file), you'll always have to 'sacrifice' some data rate and cannot get 1kHz as mentioned in datasheet. Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.