Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Works in Example but Not in My Sketch

  1. #1
    Junior Member
    Join Date
    Feb 2019
    15 Works in Example but Not in My Sketch

    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!

    #include <SD.h>
    #include <SPI.h>
    File file;
    const int chipSelect = BUILTIN_SDCARD;
    void setup() {
      while(!Serial) {
      if (!SD.begin(chipSelect)) {
        Serial.println("Unable to access the SD card");
      Serial.println("void setup() complete");
      file ="TestSDRec.txt", FILE_WRITE);
      if (file) {
        Serial.println("TestSDRec.txt opened");
      while(true) {
        Serial.println(" while loop...");
    void loop() {

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    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

  3. #3
    Junior Member
    Join Date
    Feb 2019
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts