Reference for using SD Card on Teensy 3.5

Status
Not open for further replies.

Ausplex

Active member
I'm about to use the onboard SD card on a Teensy 3.5 for the first time.

Is there a definitive reference for this anywhere for both sample code and which libraries to use?

I have found multiple posts on which library to use, such as this one from PaulS suggesting using the SD-master library.
https://forum.pjrc.com/threads/37652-microSD-slot-on-teensy-3-6

Bill Giemen suggests using SDFat:
https://github.com/greiman/SdFat-beta

I have 3 libraries already installed under this folder:
C:\Users\MyComputer\Documents\Arduino\libraries
SD-master (installed after reading the above post - from https://github.com/PaulStoffregen/SD)
SdFat-master (installed by Arduino) (probably an older version than the current one on GitHub)
SdFat (installed by Arduino)

The PJRC web site only provides information on using an external SD card adapter.

Advice appreciated.
thanks
 
Last edited:
Both SD/Sdfat library from teensyduino is already usable with T3.5/3.6's onboard SD with SDIO features,

You can just use any example with the CS pin set to BUILTIN_SDCARD
as "SD.begin(BUILTIN_SDCARD)"
by far my experience all function code work with a arduino-uno/T3.2 on microSD will also work with the builtin-SD, but extremely more faster

hope this helps

edit:
BUILTIN_SDCARD above is for SD.h library,

for SdFat you can look into "TeensySdioDemo"
https://github.com/greiman/SdFat/blob/master/examples/TeensySdioDemo/TeensySdioDemo.ino
 
Last edited:
Both SD/Sdfat library from teensyduino is already usable with T3.5/3.6's onboard SD with SDIO features,
Suggest to use SdFS from Bill Greiman, as it also handles exFAT formatted SD cards and replaces SdFAT
 
I've posted an example below using SdFat and the SdFatSdioEX mode which I understand is the fastest mode on the Teensy using the 4 bit parallel interface.

I've assume I can ignore the need to set the chips elect to BUILTIN_SDCARD when using SdFatSdioEX.

The code creates a file then checks to see if it exists. Then it creates another file and logs entries from the console.

Unfortunately the code doesn't generate any file data.

Code:
#include <SdFat.h>

SdFatSdioEX sd;

// Log file.
SdFile myFile;

// serial output steam
ArduinoOutStream cout(Serial);

// store error strings in flash
#define sdErrorMsg(msg) sd.errorPrint(F(msg));

void setup() {
  delay(2000);
  Serial.begin(9600);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }

  if (!sd.cardBegin()) {
    sdErrorMsg("\ncardBegin failed");
    Serial.println("\ncardBegin failed");
  }

    // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile.open("example.txt", FILE_WRITE);
  myFile.println("test data");
  myFile.close();
  
  // Check to see if the file exists:
  if (sd.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  myFile.open("example2.txt", FILE_WRITE);
  myFile.println("test data");

 }


void loop() {

  Serial.println("Enter some data");
  while (!Serial.available()) {
  }
  String sin = Serial.readString();
  Serial.println(sin);
  myFile.println(sin);

  if (sin == "exit") {
    Serial.println("file closed");
    myFile.close();
  } else {
    Serial.println("continue");
  }
 
}
 
Ah! Of course sin is a reserved word (sin/cos/tan). So I changed that string name to InputString and declared it outside of the main loop.

Unfortunately that didn't fix the problem. No data or files are being written to the SD card.
Code:
#include <SdFat.h>

SdFatSdioEX sd;

// Log file.
SdFile myFile;

// serial output steam
ArduinoOutStream cout(Serial);

String InputString = "";

//------------------------------------------------------------------------------
// store error strings in flash
#define sdErrorMsg(msg) sd.errorPrint(F(msg));

void setup() {
  delay(2000);
  Serial.begin(9600);
  
  // Wait for USB Serial 
  while (!Serial) {
    SysCall::yield();
  }

  if (!sd.cardBegin()) {
    sdErrorMsg("\ncardBegin failed");
    Serial.println("\ncardBegin failed");
  }

    // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  myFile.open("example.txt", FILE_WRITE);
  myFile.println("test data");
  myFile.close();
  
  // Check to see if the file exists:
  if (sd.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");
  }

  myFile.open("example2.txt", FILE_WRITE);
  myFile.println("test data");

 }
//------------------------------------------------------------------------------
void loop() {

  Serial.println("Enter some data");
  while (!Serial.available()) {
  }
  InputString = Serial.readString();
  Serial.println(InputString);
  myFile.println(InputString);

  if (InputString == "exit") {
    Serial.println("file closed");
    myFile.close();
  } else {
    Serial.println("continue");
  }
 
}
 
Status
Not open for further replies.
Back
Top