Teensy 3.2 SD card problem

Status
Not open for further replies.

MGBGTE

New member
I am working on a project to monitor battery status in my electric car (converted 1968 MGB) ... I am a Mac user, my first attempt was to use an arduino ZERO, it worked OK but I needed more I/0 capability, so I switched to a teensy 3.2 ... the teensy 3.2 is a perfect fit ...the two I2C connections made life much easier (they work perfectly!) ... so far everything (sensors, displays, switches) is working well with one exception: I am using a micro SD card (Adafruit MicroSD card breakout board ID:254) to log usage data ... the SD card worked well on the ZERO but I can't get the Teensy to recognize the SD ... I keep getting the standard "initialization failed" situation ...

the SD card board is already buried in the project with dozens of wires connecting various components, so a picture of the hardware would not be helpful ... I double checked the wiring: CLK to pin 13, DO to pin 12, DI to pin 11 CS to pin 10.

the sketch for the test routine follows:
/*
SD card read
*/

//#include <Wire.h>
#include <i2c_t3.h>
#include <SD.h>
#include <SPI.h>
#include <BMS_simple.h> // this for the full application, and not really used for the test

File LogFile;

byte buffer[100];
int indx;

void setup()
{
delay(9999);
while(!Serial);
Serial.begin(115200);
Serial.print("About to initialize SD ");

pinMode(10, OUTPUT); // 10 is chip select for SD card

if(!SD.begin(10))Serial.println("initialization failed!"); delay(999);
if(!SD.begin(10)){Serial.println("initialization failed!"); return;}

Serial.println("initialization done.");

LogFile = SD.open("Log_File.dat", FILE_READ); // Re-open file for reading

if (LogFile)
{
Serial.println("Read Log record zero.");

while (LogFile.available())
{
Serial.print(LogFile.read());
delay(99);
}

LogFile.close();

}

else Serial.println("Error opening Log record ");

}

void loop()
{
exit(0);
}

.......... any suggestion would be much appreciated.
 
Try with a low clock-setting (Teensy @ 24MHz) to check wether it is a cock-related problem or not.
Does it work ? If yes, the adapter might be the problem.
Teensy is fast and uses a fast SPI-Clock - some "Arduino" Adapters don't work with this unexpected fast speed.Best is to use an adapter without any level-shifters.
Or perhaps too long connections ? (High frequency)
 
Last edited:
I have a new Adafruit micro-sd breakout board too and have only tinkered with it enough to test it. I tested with the SD Datalogger provided in the Examples and changed the chipSelect definition in the sketch to 10. Here's the sketch:

Code:
/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4      <-- change to 10 for Teensy 3.2
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

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

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// Teensy audio board: pin 10
// Wiz820+SD board: pin 4
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
[B]const int chipSelect = 10;[/B]

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


  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.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  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");
  } 
}

Based on other threads, I was also careful to format the microSD card first using SDFormatter 4.0 which is a free download. I don't remember where I downloaded it from, but this looks like a good place to start: https://members.sdcard.org/downloads/formatter_4/eula_windows/

One other thing I learned was to purchase a quality microSD card. Mine is a SanDisk Ultra PLUS (red and gray). I think Paul mentioned that a SanDisk Extreme is also a good choice.

Note that the code above does not do a good job of logging because I end up with many data files instead of one. I have not worked on a logger other than trying the above code but I hope this helps get you started with your SD board.
 
Status
Not open for further replies.
Back
Top