DEV Community

張旭豐
張旭豐

Posted on

Why Your SD Card Fails During Long-Term Data Logging

Why Your SD Card Fails During Long-Term Data Logging

You build a data logger. It writes temperature, humidity, and pressure to an SD card every 10 minutes. You test it for a day. Everything works. You deploy it for a month. When you retrieve the card, half the file is missing. Or the file exists but is corrupted. Or the card is completely unreadable.

Arduino SD card module connected for data logging, showing SPI wiring and capacitor placement
SD card data logger — the card requires dedicated power decoupling with capacitors at the card pins to handle write current spikes.

You assume the card failed. You buy a new card. The same thing happens after three months. You buy a new card. The same thing happens after three months.

SD card data logging failures are almost never the card. They are the power, the write timing, or the file system behavior.


The Write Timing Problem

When you call SD.open() and file.write(), the data goes into a buffer. It does not necessarily go to the card immediately. The SD card has its own internal controller that manages when to actually program the data to flash memory. This operation takes 10-50ms per write cycle.

If you open a file, write data, and immediately close the file, the data might be safe. But if you open a file, write data, and then open another file before closing the first, or if you power off before calling file.close(), the buffer data is lost.

The rule: every write operation must be followed by file.close() or file.flush(). The flush() call forces the buffer to be written to the card without closing the file. But even flush() is not instantaneous — it waits for the card to complete the write cycle.

If you are logging data every 10 minutes, you are safe. If you are trying to log data every 100ms, the SD card write cycle time becomes the bottleneck.


The Power Problem Is Bigger Than You Think

SD cards draw 20-100mA during write operations. During the flash program cycle, the current can spike. If your Arduino is powering the SD card module from its 5V rail, the Arduino's regulator might not be able to supply the peak current.

Voltage droop during a write cycle causes the card to reset. The card firmware detects the voltage drop, enters a safe mode, and aborts the write. The file system on the card can become corrupted.

The fix is not to use a bigger Arduino. It is to give the SD card its own dedicated power path. Use a separate 3.3V LDO regulator just for the SD card. Add a 10µF capacitor directly across the card's power pins. This absorbs the write current spikes.

If your logger is battery-powered and the SD card causes voltage dips when writing, the battery cannot supply peak current. Add a large capacitor (100µF to 470µF) close to the SD card power pins. This capacitor acts as a peak current reservoir.


The File System Problem

Most Arduino SD libraries format cards as FAT16 or FAT32. Both file systems have a property called the FAT (File Allocation Table), which tracks which clusters belong to which files. When you write data, the FAT must be updated.

On a FAT file system, writing a single byte to a file requires:

  1. Finding a free cluster
  2. Writing the data to the cluster
  3. Updating the FAT entry for the file
  4. Updating the FAT entry for the directory
  5. Updating the directory entry

If power is lost during step 3 or 4, the FAT is inconsistent with the actual data. The file system detects this during mount and declares the file system corrupted, or marks the file as orphaned.

The solution for long-term logging: log to a raw file without a file system. This is complex. The practical solution is to use file.flush() after every write, and design the system to survive power loss by keeping a transaction log.

Or: use an SD card with wear leveling and power-loss protection features. Industrial SD cards and some high-end consumer cards have capacitor-based power-loss protection that completes the FAT update even if power is lost mid-write.


The Card Speed Problem

Not all SD cards are equal. A Class 4 card has a minimum sequential write speed of 4MB/s. A Class 10 card has a minimum of 10MB/s. In practice, the sustained write speed matters more than the class rating.

Cheap SD cards from unknown manufacturers often claim Class 10 but cannot sustain the write speed. When the Arduino writes faster than the card can accept data, the buffer fills up, writes stall, and data is lost.

For Arduino data logging at 10-minute intervals, any card works. For logging at 1-second intervals, use a known-brand card. For logging at millisecond intervals, you need a high-quality card and a microcontroller with a dedicated SDIO interface (like the Teensy), not SPI.

SPI mode on SD cards is limited to about 25-50kbytes/second maximum throughput, regardless of card quality. If you need faster logging, use SDIO.


The Corruption Patterns and What They Mean

Missing data at the end of the file: The card lost power or reset during a write. The data was in the buffer but never written.

Entirely empty file: The file was created but never written. Check your code for file.close() calls.

Garbage characters or wrong data: Two processes wrote to the same file simultaneously, or the card was removed during a write.

Card not readable by computer: The file system is corrupted. The card experienced a power loss during a FAT update. Try SD.format() to recover. If the card cannot be formatted, it is physically damaged.

Card becomes read-only: The card's internal controller detected excessive write errors and locked the card. This is permanent. Replace the card.


The Safe Logging Pattern

For reliable long-term data logging:

  1. Open the file once at setup. Keep it open for the entire logging session. Never close and reopen.
  2. Call file.flush() after every write.
  3. Add a heartbeat timestamp to every line. If the timestamps are not sequential, you know where data was lost.
  4. Use a ring buffer: when the file reaches a maximum size, close it, rename it with a timestamp, and open a new file.
  5. Power the SD card from a dedicated regulator with a large capacitor.
  6. Use a known-brand card. Samsung, SanDisk, or Kingston. Not the cheapest cards on Amazon.

The SD card is not the problem. The problem is that SD cards require specific power and timing conditions that Arduino projects often do not provide. Design for the card's requirements, not the code's convenience.

For reliable SD card data logging:

SanDisk Ultra 32GB microSD Card — Class 10, good sustained write performance. Reliable brand for long-term logging. (Amazon)

SparkFun microSD Shield — Includes level shifting and proper power decoupling. Saves debugging time over raw module. (Amazon)

Low ESR Capacitor 470µF 16V — Place directly across SD card power pins to absorb write current spikes. Essential for battery-powered loggers. (Amazon)

I earn from qualifying purchases.


Article #016, 2026-04-18. Content Farm pipeline, Run #016.

Top comments (0)