# Alternatives for SD.h library

**URL:** <https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617>\
**Category:** Programming\
**Created:** [April 15, 2023, 6:15pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617 "2023-04-15T18:15:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhir\_33](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dhir_33/32/720935_2.png) [@dhir\_33](https://forum.arduino.cc/u/dhir_33)\
**Post date:** [April 15, 2023, 6:15pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/1 "2023-04-15T18:15:31Z")

</div>

Are there any alternatives for SD.h library for **Nano 33 BLE Sense Rev2**? SdFat.h lib doesn't support this board. I want to achieve fast data logging (28 letter string every 250ms) but the SD.h lib isn't efficient in doing this and at times freezes the board.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:25pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/2 "2023-04-15T18:25:46Z")

</div>

Most likely, there are one or more problems with the code you forgot to post.

> the SD.h lib isn't efficient

It is _very_ efficient, if used correctly.

---

<div class="post-metadata">

**Author:** ![dhir\_33](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dhir_33/32/720935_2.png) [@dhir\_33](https://forum.arduino.cc/u/dhir_33)\
**Post date:** [April 15, 2023, 6:28pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/3 "2023-04-15T18:28:25Z")

</div>

The code is 600 lines long, that's why I didn't post it.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:29pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/4 "2023-04-15T18:29:19Z")

</div>

You can't possibly frighten us with a mere 600 lines of code. Post it using code tags.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:30pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/5 "2023-04-15T18:30:57Z")

</div>

> [@dhir\_33](#):
>
> I did use flush commands properly.

Rarely needed, and waste considerable time if used when not needed.

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [April 15, 2023, 6:31pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/6 "2023-04-15T18:31:30Z")

</div>

SD cards ain't fast to begin with. The overhead is the DOS emulation. That also makes it a blocking device (nothing else gets done until SD finishes.)  
Your data rate isn't bad. Are you closing & reopening or running with the file open?  
Close/reopen kills a lot of time but is safer in the event of random power off.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:39pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/8 "2023-04-15T18:39:17Z")

</div>

> [@dhir\_33](#):
>
> ```arduino
> dataFile.println(tele);
> backupFile.println(backup);
> backupFile.flush();
> dataFile.flush();
> 
> ```

As I suspected, that is where a lot of time is going. flush() is NOT required here.

Pointless usage of Strings, which are never required, and lead to memory problems and program crashes.

Unnecessary SD open() and close() commands. Open a file when you start logging, and close it when you are done logging.

Why a "backup" file if you want fast logging?

Good luck with your project!

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:46pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/10 "2023-04-15T18:46:46Z")

</div>

Each file requires a 512 byte buffer, which is allocated each time you open the file, and is written out each time you close it. So every time you open and close a file, you have to read or write an entire 512 byte block from or to the SD card.

That is extremely time consuming! Likewise, flush() has to write to the SD card to update the data block and file pointers. Otherwise, the memory buffer is only written out when full, then erased and refilled.

The only time I use flush() when logging is to update file pointers every 15 minutes to an hour, in case the power fails. That way, if power fails, you lose only the last data block.

Lastly, [The Evils of Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/).

---

<div class="post-metadata">

**Author:** ![madmark2150](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@madmark2150](https://forum.arduino.cc/u/madmark2150)\
**Post date:** [April 15, 2023, 6:47pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/11 "2023-04-15T18:47:49Z")

</div>

You're building a very long string there, that eats buffer. Finish a line before you get overflow issues.  
An ounce of software optimization beats a pound of hardware ...

---

<div class="post-metadata">

**Author:** ![dhir\_33](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dhir_33/32/720935_2.png) [@dhir\_33](https://forum.arduino.cc/u/dhir_33)\
**Post date:** [April 15, 2023, 6:52pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/12 "2023-04-15T18:52:47Z")

</div>

> [@anon94801460](#):
>
> The only time I use flush() when logging is to update file pointers every 15 minutes to an hour

The issue is, if in any case a power failure occurs, I will loose 15 mins of data, but the entire mission time is less than 10 min once started. However, the system will sit on launch pad for 2 hrs and has to start data logging when commanded remotely.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [April 15, 2023, 6:56pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/13 "2023-04-15T18:56:16Z")

</div>

In that case, collect data as fast as you can, without Strings, backup files, or any unnecessary flush, open or close commands. **Small, efficient code** has far less chance of failing.

You have a lot of code rewriting to do, so have fun.

> if in any case a power failure occurs, I will loose 15 mins of data

Hint: please take a couple of minutes to think about what forum members write, and maybe do a bit of research before posting a silly objection like the above.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 12, 2023, 6:56pm UTC](https://forum.arduino.cc/t/alternatives-for-sd-h-library/1115617/14 "2023-10-12T18:56:36Z")

</div>

Closed on October 12, 2023, 6:56pm UTC

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