Sometimes sd card does not work

Status
Not open for further replies.

xload

Active member
Hello, I have a teensy 2.0 and a teensy 2.0++ and sometimes the sd card doesnot work, here it's the source code for init the sd card:

Code:
if (!SD.begin(chipSelect)) {
      //Error initializing SD card
      Serial.println("ERR:init SD");
}
 
We will need a lot more information in order to help with this;
-> Rest of your code
-> Other devices
-> Hardware setup

Pictures of your hardware would be most ideal. =)
 
A lot of thanks Cosford, the complete code is very very long (1436 lines),I also have conected to teensy 2.0++ a USB Host Shield, wired like this:
USB Host Shield
INT - pin8
SS - pin9
MOSI - pin22
MISO - pin23
SCK - pin21
RESET - 3.3v
3.3v - 3.3v
GND - GND

The microSD card is wired like this:
MOSI - pin22
MISO - pin23
SCK - pin21
SS - pin0
5v - 3.3v

Teensy and sd card module are ready to work with 3.3v

The complete code that init the sd card and usb host is:
Code:
void setup()
{
    Serial.begin(9600);

    //Initialize SD card
    pinMode(chipSelect, OUTPUT);
    delay(100);
    digitalWrite(chipSelect, HIGH); // Add this line
    delay(100);
    
    /*
    if (!SD.begin(chipSelect)) {
      //Error initializing SD card
      Serial.println("ERR:init SD");
    }
    
    
    //Initialize USB
    if (Usb.Init() == -1){
        //Error initializing USB
        Serial.println(F("ERR: init USB"));
        usb_error=1;
    }

    delay(200);
}

The problem is that sometimes works fine and sometimes does not initialize the sd card.
 
Try adding this before your SD initialisation code:

Code:
 pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
 
Whilst it's only an educated guess, the reasoning is due to the way SPI works.

All your SPI devices share the same 3 wire bus (SCK, MISO and MOSI). So if all your devices are listening to the SPI bus, how does each know when you're talking to it? Well that's where your CS/SS pin comes in. If that line is pulled low, the device will think you're talking to it.

So the reasoning behind writing the ether net modules CS (SS) pin high, is to ensure its CS pin is high and so it doesn't try to respond to your commands, essentially talking over the SD card (who you actually want to talk to).

Well designed arduino modules usually have pull ups on their CS lines to ensure that they're held high by default, but just in case, this code ensures that the CS pin is high.
 
A lot of thanks Cosford, but the problem is the same, the new code is:

Code:
void setup()
{
    DEBUG_SERIAL.begin(9600);
    
    //Initialize SPI pins to avoid errors
    pinMode(9, OUTPUT);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    
    //Initialize SD card
    while (!SD.begin(chipSelect) && err_count<3){
      //Error initializing SD card
      #ifdef DEBUG_ON
      DEBUG_SERIAL.println(F("ERR:init SD"));
      #endif
      err_count++;
    }
    
    //Write start tag at log.txt file
    if(!sd_error){
      sd_log("[Start]",1);
    }
    
    //Initialize USB
    if (Usb.Init() == -1){
        //Error initializing USB
        DEBUG_SERIAL.println(F("ERR: init USB"));
    }

    delay(200);
}
 
I added this code, and the problem still happen.
Code:
    //Initialize SPI pins to avoid errors
    //USB host SS pin
    pinMode(9, OUTPUT);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    //MicroSD SS pin
    pinMode(chipSelect, OUTPUT);
    delay(200);
    digitalWrite(chipSelect, HIGH);
    delay(200);
 
Status
Not open for further replies.
Back
Top