Arduino to SD logger speed problems (feat MPU6050)

Hi. I’m sorry but it is very urgent and everything I do doesn’t work. I try to write to a SD card data coming from my MPU6050 IMU (data comes through a Bluetooth connection-software serial),and it’s to slow with the SD library.It misses data . What can i do to maximize the speed of write. I heard that i can create some buffers to work with the data…but I am kind of new at this so if anyone can help me with this ,I’ll be very grateful. (working with arduino mega on the SD side and UNO on the MPU side ,bluetooth connection made with two HC05’s ). Posted the code below.

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

const int chipSelect = 53;
String inputString = “”; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete

SoftwareSerial BTSerial(10, 11); // RX | TX
File f;

void setup()
{
Serial.begin(38400);
BTSerial.begin(38400);

inputString.reserve(200);

if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);
}

void loop() {

btserial_to_SD();

}

void btserial_to_SD() {

f = SD.open(“temp.csv” , FILE_WRITE);
while (BTSerial.available()) {
if(f)
{
// get the new byte:
char inChar = (char)BTSerial.read();
// add it to the inputString:
inputString += inChar;

// if the incoming character is a newline, set a flag
// so the main loop can do something about it:

if (inChar == ‘\n’) {
Serial.println(inputString);
f.println(inputString);
f.close();
// clear the string:
inputString = “”;
stringComplete = false;
}
}
else {
Serial.println(“error opening temp.csv”);
}
}
}

and it’s to slow with the SD library.

How do you know that? You are appending data to a String object. For the first few characters, that is fairly quick (though nowhere near as quick as using a NULL terminated array of chars would be). For more characters, the append operation takes longer and longer. Perhaps that is where the problem is.

Opening and closing the file each time through isn’t helping either…

There are some easy things you can try.

  • Move the file opening code to setup() and don't close the file
  • Don't print to the serial console while you are trying to write to the SD card (printing to Serial is very slow in comparison)

You could also collect the string in one operation, by using Serial.readStringUntil():