2

I have connected this Adafruit SD Breakout to a Teensy 3.2 over SPI. Because Teensy 3.2 is 3.3 V, I've connected the 3.3 V pin of the Teensy to the 3V pin of the SD breakout.

See sketch below. I'm using Arduino IDE 2.0.4. The SD card is a micro SD HC 32 GB, see photos. FAT32. The SD setup usually goes fine, but every now and then it fails. Whenever the setup goes fine, writing data to the SD card thereafter also goes fine. Does anyone know what this failing could be caused by? Perhaps the SD card is a bit flaky? Or something in software (library)?

My initial guess would be with the wiring, but I've seen similar problems on a PCB with a SD module on it. Hence I think there's more at play than just wiring.

N.B. on the photo you can see there is also an ADXL breakout board connected. However that is over I2C and does not participate in this setup. I've left it in there because I don't want to have to take it out and reconnect it later.

/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library. Pin numbers reflect the default
  SPI pins for Uno and Nano models

  The circuit:
   analog sensors on analog pins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** SDO - pin 11
 ** SDI - pin 12
 ** CLK - pin 13
 ** CS - depends on your SD card shield or module.
        Pin 10 used here for consistency with other Arduino examples
    (for MKR Zero SD: SDCARD_SS_PIN)

  created  24 Nov 2010
  modified  24 July 2020
  by Tom Igoe

  This example code is in the public domain.

*/

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

const int chipSelect = 10;
#define LED_PIN 7

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // wait for Serial Monitor to connect. Needed for native USB port boards only:

  pinMode(LED_PIN, OUTPUT); 
  Serial.print("Initializing SD card...");

  while(!SD.begin(chipSelect)) {
    Serial.println("SD initialization failed.");
    delay(100); 
    digitalWrite(LED_PIN, HIGH);
    delay(50); 
    digitalWrite(LED_PIN, LOW);
    delay(50); 
    digitalWrite(LED_PIN, HIGH);
  }

  Serial.println("initialization done.");
  digitalWrite(LED_PIN, HIGH); 
}

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

Output:

09:09:03.970 -> Initializing SD card...initialization done.
09:09:19.976 -> Initializing SD card...initialization done.
09:09:28.293 -> Initializing SD card...initialization done.
09:09:38.538 -> Initializing SD card...SD initialization failed.
09:09:42.748 -> SD initialization failed.
09:09:44.924 -> SD initialization failed.
09:09:47.140 -> SD initialization failed.
09:09:49.346 -> SD initialization failed.
09:09:51.556 -> SD initialization failed.
09:09:53.770 -> SD initialization failed.
09:09:55.939 -> SD initialization failed.
09:09:58.147 -> SD initialization failed.
09:10:00.352 -> SD initialization failed.
09:10:02.518 -> SD initialization failed.
09:10:04.725 -> SD initialization failed.
09:10:06.965 -> SD initialization failed.
09:10:09.160 -> SD initialization failed.
09:10:11.353 -> SD initialization failed.
09:10:13.566 -> SD initialization failed.
09:10:15.775 -> SD initialization failed.
09:10:17.954 -> SD initialization failed.
09:10:20.169 -> SD initialization failed.
09:10:22.368 -> SD initialization failed.
09:10:24.574 -> SD initialization failed.
09:10:26.777 -> SD initialization failed.
09:10:28.979 -> SD initialization failed.
09:10:31.159 -> SD initialization failed.
09:10:33.376 -> SD initialization failed.
09:10:35.565 -> SD initialization failed.
09:10:37.783 -> SD initialization failed.
09:10:39.978 -> SD initialization failed.
09:10:42.166 -> SD initialization failed.
09:10:44.384 -> SD initialization failed.
09:10:46.573 -> SD initialization failed.
09:10:48.793 -> SD initialization failed.
09:10:50.974 -> SD initialization failed.
09:11:51.284 -> Initializing SD card...initialization done.
09:12:18.656 -> Initializing SD card...initialization done.
09:12:28.948 -> Initializing SD card...initialization done.
09:12:32.867 -> Initializing SD card...initialization done.
09:12:38.416 -> Initializing SD card...initialization done.
09:12:44.657 -> Initializing SD card...initialization done.
09:12:52.141 -> Initializing SD card...initialization done.
09:13:08.003 -> Initializing SD card...initialization done.
09:13:10.421 -> Initializing SD card...initialization done.
09:13:12.893 -> Initializing SD card...initialization done.
09:13:15.070 -> Initializing SD card...initialization done.
09:13:17.771 -> Initializing SD card...initialization done.
09:13:19.922 -> Initializing SD card...initialization done.
09:13:25.418 -> Initializing SD card...initialization done.
09:13:28.104 -> Initializing SD card...initialization done.
09:13:30.835 -> Initializing SD card...initialization done.
09:13:33.217 -> Initializing SD card...initialization done.
09:13:36.146 -> Initializing SD card...initialization done.
09:13:40.727 -> Initializing SD card...initialization done.
09:13:44.985 -> Initializing SD card...initialization done.
09:13:49.089 -> Initializing SD card...initialization done.
09:13:51.687 -> Initializing SD card...initialization done.
09:13:55.698 -> Initializing SD card...initialization done.
09:14:00.868 -> Initializing SD card...initialization done.
09:14:03.965 -> Initializing SD card...initialization done.
09:14:06.697 -> Initializing SD card...initialization done.
09:14:10.518 -> Initializing SD card...initialization done.
09:14:13.549 -> Initializing SD card...initialization done.
09:14:18.414 -> Initializing SD card...initialization done.
09:14:22.145 -> Initializing SD card...Initializing SD card...initialization done.
09:14:31.614 -> Initializing SD card...initialization done.
09:14:34.230 -> Initializing SD card...initialization done.
09:14:37.077 -> Initializing SD card...SD initialization failed.
09:14:41.273 -> SD initialization failed.
09:14:43.482 -> SD initialization failed.
09:14:45.675 -> SD initialization failed.
09:14:47.858 -> SD initialization failed.
09:14:50.080 -> SD initialization failed.
09:14:52.286 -> SD initialization failed.
09:14:54.464 -> SD initialization failed.
09:14:56.665 -> SD initialization failed.
09:14:58.931 -> SD initialization failed.
09:15:01.088 -> SD initialization failed.
09:15:03.253 -> SD initialization failed.
09:15:05.482 -> SD initialization failed.
09:15:07.696 -> SD initialization failed.
09:15:09.902 -> SD initialization failed.
09:15:12.096 -> SD initialization failed.
09:15:20.904 -> SD initialization failed.
09:15:23.112 -> SD initialization failed.
09:15:25.287 -> SD initialization failed.
09:15:27.494 -> SD initialization failed.
09:15:29.720 -> SD initialization failed.
09:15:30.936 -> Initializing SD card...SD initialization failed.
09:15:35.095 -> SD initialization failed.
09:15:37.304 -> SD initialization failed.
09:15:39.515 -> SD initialization failed.
09:15:41.713 -> SD initialization failed.
09:15:43.899 -> SD initialization failed.
09:15:46.108 -> SD initialization failed.
09:15:48.320 -> SD initialization failed.
09:15:50.502 -> SD initialization failed.
09:15:52.685 -> SD initialization failed.
09:15:54.920 -> SD initialization failed.
09:15:57.887 -> Initializing SD card...SD initialization failed.
09:16:01.902 -> SD initialization failed.
09:16:04.097 -> SD initialization failed.
09:16:06.312 -> SD initialization failed.
09:16:08.518 -> SD initialization failed.
09:16:10.720 -> SD initialization failed.
09:16:12.901 -> SD initialization failed.
09:16:15.125 -> SD initialization failed.
09:16:17.328 -> SD initialization failed.
09:16:19.521 -> SD initialization failed.
09:16:21.734 -> SD initialization failed.
09:16:23.925 -> SD initialization failed.
09:16:26.097 -> SD initialization failed.
09:16:28.332 -> SD initialization failed.
09:16:30.533 -> SD initialization failed.
09:16:32.732 -> SD initialization failed.
09:16:34.917 -> SD initialization failed.

EDIT: Now I've also added logic analyser output of failed SD setup.

EDIT 2: The Teensy SD library is used:

Multiple libraries were found for "SD.h"
  Used: C:\Users\xxxxx\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.57.2\libraries\SD
  Not used: C:\Users\xxxx\AppData\Local\Arduino15\libraries\SD

enter image description here enter image description here enter image description here enter image description here enter image description here

4
  • What happens if you eject, then re-insert the SD card while it is stuck in the "SD initialization failed." while loop?
    – VE7JRO
    Mar 11 at 21:30
  • It depends. Once when I did it, the setup completed fine and the SD card was functional after that. Another time it just continued in the "SD initialization failed" loop. And then another time still, the setup completed fine but then the error popped up "error opening datalog.txt". I have been noticing a pattern where whenever I get the "error opening datalog.txt" error, on subsequent resets the "SD initialization failed" error is more prevalent.
    – daniel
    Mar 13 at 12:45
  • Try ejecting the SD card, then re-inserting it, then reboot the Arduino. Let me know what happens when you do those 3 steps in that order.
    – VE7JRO
    Mar 13 at 17:52
  • Then it immediately goes back into the "SD initialization failed" loop :L
    – daniel
    Mar 15 at 12:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.