# When ESP32S3 is writing data to SD card, how to remove the card safely?

**URL:** <https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679>\
**Category:** Storage\
**Created:** [February 2, 2026, 6:34pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679 "2026-02-02T18:34:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbqq](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bbqq/32/1129734_2.png) [@bbqq](https://forum.arduino.cc/u/bbqq)\
**Post date:** [February 2, 2026, 6:34pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/1 "2026-02-02T18:34:37Z")

</div>

Hello, I am writing a program to find out how fast data is being written to a SD card. If I remove all unnecessary statements (such as a if-statment to check if certain number of lines has reached), how do I savely close the file and end the program before I remove the card?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 2, 2026, 7:40pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/2 "2026-02-02T19:40:45Z")

</div>

> [@bbqq](#):
>
> close the file and end the program before I remove the card

```arduino
  file.flush(); // empty buffer
  delay(1000);
  file.close();

```

---

<div class="post-metadata">

**Author:** ![bbqq](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bbqq/32/1129734_2.png) [@bbqq](https://forum.arduino.cc/u/bbqq)\
**Post date:** [February 2, 2026, 7:43pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/3 "2026-02-02T19:43:06Z")

</div>

Thanks. How do I integrate these to within the loop() without throttling the sampling in any way?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [February 2, 2026, 7:52pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/4 "2026-02-02T19:52:04Z")

</div>

"file." is the name you give the SD card instance, ~~usually~~ when you open the file (so, maybe, use another descriptive name to avoid confusion... like "bqFile".

1. open bqFile in `setup()`
2. read/write to bqFile in `loop()`

When finished read/write-ing (in `loop()`)

1. (flush bqFile)
2. close bqFile

---

<div class="post-metadata">

**Author:** ![bbqq](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bbqq/32/1129734_2.png) [@bbqq](https://forum.arduino.cc/u/bbqq)\
**Post date:** [February 2, 2026, 7:58pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/5 "2026-02-02T19:58:21Z")

</div>

Thanks. I know this procedure but the problem is the "When finished read/write-ing" part. I think I was told to do the sampling as fast as possible so I guess I need to avoid the if-statement within the loop() function to test when, say a certain number of lines have been written or when a button/key is pressed. If I just unplug the card, I might corrup the file/file system or damage the sd card. Without using flush() to clear the buffer and then closing the file, the data file may not be written successfully.

Please refer to Post#31 and Post#47

[How to obtain and save sensor data at specific samplying rate using Adalogger with RTC + SD? - Other Hardware / 3rd Party Boards - Arduino Forum](https://forum.arduino.cc/t/how-to-obtain-and-save-sensor-data-at-specific-samplying-rate-using-adalogger-with-rtc-sd/1427468?_gl=1*74yj5e*_up*MQ..*_ga*NjgxNTI1NzM1LjE3NzAwNjAzNDc.*_ga_NEXN8H46L5*czE3NzAwNjAzNDYkbzEkZzAkdDE3NzAwNjAzNDYkajYwJGwwJGg2NTI2NzQ1NTg.)

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 2, 2026, 11:18pm UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/6 "2026-02-02T23:18:40Z")

</div>

> [@bbqq](#):
>
> Thanks. How do I integrate these to within the loop() without throttling the sampling in any way?

Before getting to excited about throttling the sampling, consider the data for the SD card is written in 512 byte blocks. You can sample as fast as you can until you hit the 512 byte limit and then the rest of your code, including the sampling is blocked from executing until the 512 byte block is written and the file header is updated and written to the SD card. Only then will your code continue. Facts of using the SD card. It all began in the dark ages with the floppy disk in the PC.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [February 3, 2026, 12:58am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/7 "2026-02-03T00:58:49Z")

</div>

Question is: Why the SD card.  
The ESP32S3 has built-in 8MB or 16MB flash and BT+WiFi capabilities.  
You can log a lot of data on that flash before offloading with WiFi or BT.  
If you are not going to use WiFi or that flash, then why did you buy an ESP32S3.  
A RTC is also not needed with WiFi near.  
Leo..

---

<div class="post-metadata">

**Author:** ![bbqq](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bbqq/32/1129734_2.png) [@bbqq](https://forum.arduino.cc/u/bbqq)\
**Post date:** [February 3, 2026, 4:47am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/8 "2026-02-03T04:47:43Z")

</div>

So to avoid this problem, just created a large arrary to hold the data and then write them to the SD card or offloading with WiFi or BT all at once at the end?

---

<div class="post-metadata">

**Author:** ![bbqq](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bbqq/32/1129734_2.png) [@bbqq](https://forum.arduino.cc/u/bbqq)\
**Post date:** [February 3, 2026, 4:55am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/9 "2026-02-03T04:55:20Z")

</div>

There is no specific reason for using SD card only except that I know how to do it. By using the "biilt-in 8MB or 16MB flash", do you mean creating a big array to store to data? Or there is a special way to store the data in the flash perhaps for faster access than using an array?

By "RTC is also not needed with WiFi near". Do you mean just use NTP regularly to get the clock from the internet?

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 3, 2026, 5:14am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/10 "2026-02-03T05:14:38Z")

</div>

Yes, but design the array entries so as to use the fewest number of bytes to store your data. At some point you can learn to compute the maximum number of entries that will fit in the remaining memory because as you develop and modify the program that size will probably shrink.

---

<div class="post-metadata">

**Author:** ![Wawa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wawa/32/1211303_2.png) [@Wawa](https://forum.arduino.cc/u/Wawa)\
**Post date:** [February 3, 2026, 5:35am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/11 "2026-02-03T05:35:27Z")

</div>

> [@bbqq](#):
>
> Do you mean just use NTP regularly

Yes, default NTP updates are 3-hourly, which is more than enough to keep time within one or two seconds. NTP is built into the ESP core and only requires one line in setup.

There are plenty of LittleFS data logging examples online to study.  
Leo..

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [February 3, 2026, 10:02am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/12 "2026-02-03T10:02:33Z")

</div>

> [@bbqq](#):
>
> I think I was told to do the sampling as fast as possible so I guess I need to avoid the if-statement within the loop() function to test when, say a certain number of lines have been written

This statement will only require a matter of microseconds to execute. So it is not something you need to worry about at this point. In addition, it is possible this type of check could actually be utilized to improve the sampling rate by the technique of staging your samples in an array in RAM, then only undertaking the significantly time consuming process of writing that data to the SD card after the sampling session is complete, as you described in post #8.

> [@bbqq](#):
>
> or when a button/key is pressed.

A `digitalRead` call does take more time compared to a simple value check. For the purpose of the simple test to get an idea about the maximum possible sampling rate, I would recommend just taking a fixed number of samples, as is done by your existing code:

> [@How to obtain and save sensor data at specific samplying rate using Adalogger with RTC + SD?](https://forum.arduino.cc/t/how-to-obtain-and-save-sensor-data-at-specific-samplying-rate-using-adalogger-with-rtc-sd/1427468/32):
>
> ```arduino
> if (count_seconds > RECORD_DURATION)
> 
> ```

(the variable names are misleading as this is actually the sample count as implemented in the code)

---

<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:** [August 2, 2026, 10:02am UTC](https://forum.arduino.cc/t/when-esp32s3-is-writing-data-to-sd-card-how-to-remove-the-card-safely/1428679/13 "2026-08-02T10:02:36Z")

</div>

Closed on August 2, 2026, 10:02am UTC

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