How to improve sampling rate of mpu6050 accelerometer and Gyrometer data

Following is the code which we are using...

include <MPU6050_tockn.h>
include <SPI.h>
include <SD.h>
include <DS3231.h>

DS3231 rtc(SDA, SCL);
const int chipSelect = 4;
MPU6050 mpu6050(Wire);
long timer = 0;

void setup()
{
Serial.begin(9600);
rtc.begin();
rtc.setDate(27, 11, 2021);
rtc.setTime(10, 30, 0);
mpu6050.begin();

Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
}
void loop()
{
mpu6050.update();

Serial.print(rtc.getDateStr()); Serial.print(","); Serial.write(rtc.getTimeStr()); Serial.print(",");

Serial.print(mpu6050.getAccX());
Serial.print(",");
Serial.print(mpu6050.getAccY());
Serial.print(",");
Serial.println(mpu6050.getAccZ());

File dataFile = SD.open("dataset1.txt", FILE_WRITE); if (dataFile)
{
dataFile.print(rtc.getDateStr());
dataFile.print(",");
dataFile.print(rtc.getTimeStr());
dataFile.print(",");

dataFile.print(mpu6050.getAccX()); dataFile.print(","); dataFile.print(mpu6050.getAccY()); dataFile.print(","); dataFile.println(mpu6050.getAccZ()); dataFile.close();

}

else
{
Serial.println("error opening imulog.txt");
}
}

what sampleing rate are you getting at the moment?
what board are you using?
you are probably limited by the speed writing to the SD card
what do you want to do with the data?

You make two classic mistakes:

  1. using the very low serial baud rate of 1 character printed per millisecond:
    Serial.begin(9600);

  2. Opening the SD file, writing a bit of data, and closing it again. This is EXTREMELY slow, so open the file once in setup() and close it when you are all done collecting data.

Have you checked the default sample rate used by the sensor library?

I am using Arduino uno and currently i am getting 25 to 27 samples per second..... From the acceleration data of the three axes i am applying double integration formula to get displacement thats all.....

Okay i will try to make the changes u have suggested to me... Thanks

That won't work either, but that is a different problem. Don't forget to use code tags when posting code.

a recent project sampled a LSM9DS1 accelerometer every 20mSeconds for transmission over BLE to an Android phone.
18 bytes/sample using double buffering filling one 1000byte buffer when transmiiting the other over BLE.
In this case the BLE transmission was the limiting factor.

I am really weak in coding...Can u please explain me clearly if i write the file open statement in setup section then where do i need to close it (In setup section or in loop section)...

follow the suggestions by @jremington

  1. increase the serial baudrate, e.g. to 115200 baud
  2. open the file once in setup() and write to it in loop()

e.g.

#include <MPU6050_tockn.h>
#include <SPI.h>
#include <SD.h>
#include <DS3231.h>

DS3231 rtc(SDA, SCL);
const int chipSelect = 4;
MPU6050 mpu6050(Wire);
long timer = 0;

void setup()
{
Serial.begin(115200);                // ***** changed!
rtc.begin();
rtc.setDate(27, 11, 2021);
rtc.setTime(10, 30, 0);
mpu6050.begin();

Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect))
{
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
// ************** open file here
File dataFile = SD.open("dataset1.txt", FILE_WRITE); 
if (dataFile)
Serial.println("opened imulog.txt OK");
else
Serial.println("error opening imulog.txt");
}
void loop()
{
mpu6050.update();

Serial.print(rtc.getDateStr()); Serial.print(","); Serial.write(rtc.getTimeStr()); Serial.print(",");

Serial.print(mpu6050.getAccX());
Serial.print(",");
Serial.print(mpu6050.getAccY());
Serial.print(",");
Serial.println(mpu6050.getAccZ());
//  ***************** write to file
if (dataFile)
{
dataFile.print(rtc.getDateStr());
dataFile.print(",");
dataFile.print(rtc.getTimeStr());
dataFile.print(",");
dataFile.print(mpu6050.getAccX()); dataFile.print(","); dataFile.print(mpu6050.getAccY()); dataFile.print(","); dataFile.println(mpu6050.getAccZ()); dataFile.close();
}

}

how do you stop the data acquisition and close the file?

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