skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    As additional information, for anyone stumbling upon this question: Teensy's SD library is actually a wrapper for SDfat, and should work with 8+ filenames. If it doesn't, then the issue probably lies in the IDE falling back to the classic SD library. In my case it was a problem with Visual Micro, which was not using the Teensy implementation, resulting on the described problems. In Arduino IDE that might happen if the Teensy libraries aren't correctly installed/found. Compile project with verbose output and look for the line concerning SD library to check which version is being used.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search