This is my first post, so I kindly ask for a bit of patience if there are any formatting issues.
Context:
I’m currently designing a system that uses an interrupt service with a specific frequency. To determine the minimum frequency for this system, I first need to figure out the time it takes to write data to an SD card, as my system needs to complete a write operation to the SD memory before the next interrupt occurs. I’m using the "SD.h" library provided by Arduino, and I assume that the write time using the "println()" function also depends on the number of characters being written to the file.
println() function: "Print data, followed by a carriage return and newline, to the File, which must have been opened for writing. Prints numbers as a sequence of digits, each an ASCII character (e.g., the number 123 is sent as the three characters ‘1’, ‘2’, ‘3’)." (Information sourced from Arduino reference: SD - println() - Arduino Reference)
My request:
I would greatly appreciate it if someone could tell me how long it takes to write a character to an SD card, considering any factors that might affect this time, such as file opening, path access, data type conversion, etc. I’m posting this as a last resort since, unfortunately, my online research hasn’t been successful.
Sending warm regards, and I hope someone has an answer!
Data is written to the SD card in fixed size blocks, so the time will vary. The code allocates a block sized buffer for data. If the block is not full the data is just added to the buffer (fast). When the buffer is full it is written to the card (slow). If a write is done before the buffer is full (file close or flush) the buffer data had to be merged with the existing data on the card (slower). So the answer is "it depends".
Do not ever design a system that is time dependent and includes a blocking function like SD card. That may even turn off all interrupts at the time a block is being written to the SD card.
Hello everyone, and apologies for the delay in my response. After considering your suggestions, I’ve decided to go with the option of using a buffer to store data and write during non-critical times in the execution.
Thanks in advance to everyone for your input. Sending you all a warm virtual hug.
Buffer your data, write in chunks of 512 bytes at a time. 4K or 8K is a good size.
Make your code robust to write delays of up to 250 ms or so.
Most cards have wear leveling, don't try to do this for the card.
The filesystem adds an additional complication to things, but allocation size should be aligned to sector size and the above performance guidelines are valid.
Check the allocation size after a format, that will give you an ideal buffer size.