Dear Board
Im making a reliable logger using Arduino.
when i search for examples i find few examples with the following (with SD card library)
- open file
- write few bytes (somewhere around 3 to 10)bytes
- followed by "file close"
Above will cause multiple write cycles to SD card reducing write cycles life of SD card?
my question is ->
Do i need to wait until 512 bytes (for block writing) before i use flush() close() command to SD card manually.
or will SD card library automatically flush() whenever buffer (in SD card library) reaches 512?
if i use flush() close() every 3 or 10 bytes will my SD card write cycles life reduces?
Please suggest
You do not need to wai until you have 512 bytes, if you cloe the file it will automatically write any unwritten data to disk. Opening and closing the file for every 3 - 10 bytes will cause alot of writes to an sd card as it is not just the one file that gets updated, but other files such as the directory files. It is usually better to keep the file open. If the file is open and there is unwritten data in the buffer that will be lost (max 512 bytes).
If you keep opening and closing the file it will have very slow performance.
Have a look a the examples in the SDfat library.
Yes i know that, file close function will write data into SD card.
Where i need clarity is
suppose if i have 5bytes of info
I can follow two ways
- open file
- write 5 bytes
- close file
or
- open file
- write 5 bytes, write another new 5 bytes, write another new 5 bytes until 512 bytes
- once 512 bytes reached
- close file
if you see from above "SD card actual disk write cycles" from above method 1 and method 2
method 1 -> ~102 write cycles
method 2 -> 1 write cycle
so, if i follow method 2, i will get 100 times more write cycle life than method 1...
is my understanding correct?
That is my basic understanding, but each of your write cycles will be mutiple write cycles on the SD card as more than one files requires updating each time you close.
If you wait until say 5K btes has been written to the file before closing it there will be even less write cycles on the SD card as for most of the writes only that file will be updated.
Your library will keep track of the 512 bytes, so you don't need to do that. Just continue writing until you are ready to terminate the session, and close the file at that time. Actually, depending on how important your data is, you may want to close the file every now and then to catch everything up, including the file length in the directory and the clusters used per the file allocation table, to protect against a crash or power loss. But that would be more a question of elapsed time, not so much the number of bytes.
Thank you Sherman 8)
you have clearly answered my question.
i will implement power down interrupt and execute file close before power loss.
Have a good day