arduinosd-cardteensyvisualmicro

Teensy 4.1 SD.open randomly failing to create files


I'm creating a Teensy 4.1 system that has as part of it a data logger sketch using an SD card. The Teensy receives the log name via external message, checks if that file exists and, if so, increments it with a suffix ("_X"). That part is working fine, the problem lies in the SD file management part.

Whenever I try to actually create the files (not even writing data to them yet) I start getting erratic behavior. Specifically, here's what happened: Sketch was able to create "original_name" file without issue. Then checks, file exists, increments and creates "original_name_1" without issue. Now when it tries to create "original_name_2", that fails. Restart teensy, fails again. Remove SD card from slot, open in reader, check for files (all created ok), delete all, re-insert into teensy. "original_name", ""original_name_1" and "original_name_2" all created successfully. Now fails on "original_name_3". Editing the sketch change to other log name, now fails on creating "original_name" base file. Changing again sometimes fails at base name, sometimes writes some iterations, but never managed to get past the third increment.

I'm stumped.

Code is as simple as can be, following the generic SD library examples. Code:

File log_file = SD.open(log_name, FILE_WRITE);

if (!log_file) {
    Serial.println("File open failed!");
}

if(log_file) log_file.close();

Using

#include <SD.h>
#include <SPI.h>

Other info: I'm using the onboard SD card slot (obviously), an OctoWS2811 Adaptor and a common anode RGB led connected to pins 24, 28, and 37 (free PWM pins). Using Visual Micro on Visual Studio. 32GB SD card formatted using official SD Memory Card Formatter, and shows no errors when checked.


Solution

  • Arduino SD library is based on classic FAT filesystem which uses 8.3 filename, meaning the file name is 8-character long with max 3-character extension.

    Use shorter file name or switch to SDFat library which supports modern filesystem implementation.