SD.open() Works in Example but Not in My Sketch

Status
Not open for further replies.

gtrmstr53

Member
Hi there,

I'm starting a project with a Teensy 3.6 that requires me to write data to an SD card, and was having some issues opening a new file onto the SD card. After some troubleshooting, I'm at a point where I know I must be missing something obvious but for the life of me I can't figure it out.

To make sure the SD card reader on my 3.6 was working ok, I loaded the ReadWrite example file at File/Examples/Examples for Teensy 3.6/SD/ReadWrite. Once I change the chipSelect declaration to BUILTIN_SDCARD, this example works exactly like it's supposed to. So that leads me to think there's no hardware issues, and I have all the software installed that I need.

However, I've tried writing my own slightly simplified code using the example as a guide, but I can't get it to work. I've copied the code I currently have below, and no matter what I try the file TestSDRec.txt doesn't get created and 'file' must not return true because the 'if (file)' statement doesn't get triggered. I'm new to Arduino IDE, and thought there might be some sketch-specific settings that might be causing some problems, so I commented out my code and copied the ReadWrite example into my sketch. The ReadWrite example code works perfectly in my sketch, even though my own code doesn't, so that leads me to think there's nothing wrong with my sketch. I'm left assuming there's something wrong with my code itself, but for the life of me I can't see what I'm doing wrong.

Any pointers you can spare would be a huge help. Best!


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

File file;
const int chipSelect = BUILTIN_SDCARD;


void setup() {
  Serial.begin(9600);
  while(!Serial) {
    ;  
  }
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Unable to access the SD card");
    return;
  }

  Serial.println("void setup() complete");

  file = SD.open("TestSDRec.txt", FILE_WRITE);
  if (file) {
    Serial.println("TestSDRec.txt opened");
  }
  
  while(true) {
    Serial.println("...in while loop...");
    delay(5000);
  }
}


void loop() {
  
}
 
Maybe depends on how disk formatted. Try file name of 8.3 format. Like drop the second letter e from name and see if that makes a difference
 
Yep, changing the name to 8.3 format did the trick. I knew it was something simple!!!

Even still, I wouldn't have come up with it on my own. Thanks so much for the pointer.
 
Status
Not open for further replies.
Back
Top