Teensy 3.6 sd card data logging not working

Status
Not open for further replies.

RomanP

Member
Hi,

I have several sensors connected to my teensy 3.6. I need to log the data on an sd card. I am using the built in micro sd holder with a SanDisk 64 GB sd card.
To test the data logger, I simply tried to save data from two analog pins that are measuring voltage and current from a battery.

I adapted example codes from two libraries. The SD library and the SdFat library. However, it seems that it cannot initialize the sd card, and therefore cannot create a file and write in it.
Here are my two codes. Do I need to somehow pre-format the sd card or is my code not correct?

Thank you for your help!


Example code 1 from SD library:


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

// change this to match your SD shield or module
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD

const int chipSelect = BUILTIN_SDCARD;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage

void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}

Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
delay(3000);
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);

dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);

// open the file.
File dataFile = SD.open("datalog.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(1000);
}

Example code 2 from SdFat library:

#include <SdFat.h>
#define USE_SDIO 1
const uint8_t SD_CHIP_SELECT = SS;

SdFatSdioEX sd;
SdFile myFile;
char fileName[] = "datalog.txt";
char KeyCode;
int FileData;
float amps; //define variable for current
float volt_bat; //define variable for battery voltage

void setup()
{
pinMode(A21, INPUT_DISABLE); //define pin A21 as input for battery voltage
pinMode(A22, INPUT_DISABLE); //define pin A22 as input for current measurement

Serial.begin(9600);
while(!Serial);
}

void loop()
{
String dataString = "";
amps = map(float(analogRead(A22)), 386, 1024, 0, 37.2);
volt_bat = map(float(analogRead(A21)), 1, 1024, 0, 26.2);

dataString += String(amps);
dataString += ",";
dataString += String(volt_bat);
sd.begin();

if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
Serial.println("Opening text file for write failed.");
}
else {
myFile.println(dataString);
myFile.close();
Serial.println(dataString);
}
delay(1000);

}
 
Did You try the examples provided with the libraries?
Anway chipSelect must be BUILTIN_SDCARD for T3.6 (not SS)
 
Yes, I've tried several examples from the libraries and I still have the same result. And yes I changed chipSelect to BUILTIN_SDCARD.

Could it have something to do with the SD card I'm using? It's a 64GB SDHD card from SanDisk and has the exFAT format. I read somewhere that it had to be the FAT32 format and could therefore not be a bigger card than 32GB. I'd need to try that...
 
SD library from TD does not support exFAT.
For ExFAT you need SdFAT-beta from Bill (AFAIK)
In case you install SdFAT-beta, best you delete the SD library due to conflicting include filers.
 
Thanks!

I actually tried with a smaller sd card that had FAT32 format and both my scripts worked well.

Problem solved.

Cheers!
 
Status
Not open for further replies.
Back
Top