trying to interface with an SD card device with bizarre results

Hi

I have purchased sd card reader/writer and I'm currently trying to interface with it using the recommended library. I have an SD card installed. At the moment, I'm just trying to connect to the device to ensure that everything is working ok.

When I try the cardinfo example with chipSelect = 10 (which the documentation says is the correct chip select pin to use), the interface returns a failure:

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

Sd2Card card;
SdVolume volume;
SdFile root;

int chipSelect = 10;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  delay (2000);

      Serial.print("\nInitializing SD card. Trying Chip Select ");
      Serial.println(chipSelect);
    
      if (!card.init(SPI_HALF_SPEED, chipSelect)) 
      {
        Serial.println("initialization failed.");
      } 
      else 
      {
        Serial.println("Wiring is correct and a card is present.");
      }  

}

This produces the following output:

Initializing SD card. Trying Chip Select 10
initialization failed.

Just to make sure that I wasn't using the incorrect chipSelect pin I thought I would loop through each pin:

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

Sd2Card card;
SdVolume volume;
SdFile root;

int chipSelect = 0;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  delay (2000);

  while (true)
  {
      Serial.print("\nInitializing SD card. Trying Chip Select ");
      Serial.println(chipSelect);
    
      if (!card.init(SPI_HALF_SPEED, chipSelect)) 
      {
        Serial.println("initialization failed.");
      } 
      else 
      {
        Serial.println("Wiring is correct and a card is present.");
      }
      chipSelect++;
  }

}

and to my suprise the output was as follows:

Initializing SD card. Trying Chip Select 0
initialization failed.

Initializing SD card. Trying Chip Select 1
initialization failed.

Initializing SD card. Trying Chip Select 2
initialization failed.

Initializing SD card. Trying Chip Select 3
initialization failed.

Initializing SD card. Trying Chip Select 4
initialization failed.

Initializing SD card. Trying Chip Select 5
initialization failed.

Initializing SD card. Trying Chip Select 6
initialization failed.

Initializing SD card. Trying Chip Select 7
initialization failed.

Initializing SD card. Trying Chip Select 8
initialization failed.

Initializing SD card. Trying Chip Select 9
initialization failed.

Initializing SD card. Trying Chip Select 10
Wiring is correct and a card is present.

Clearly chipSelect = 10 is succeeding when looping through...but not when I hardcode the chipSelect = 10. What am I missing here!?

EDIT: I have also added 'pinMode(chipSelect, OUTPUT)' and 'digitalWrite(chipSelect, HIGH)' which others have recommended but unfortunately it is still not working.

Thanks

That is indeed bizarre. I don't know anything about that module, but would suggest you try a loop trying pin 10 repeatedly until it's successful, and see how many tries it takes. The other thing would be adding a longer delay ahead of trying it the first time to see if that alone will make it work. You would think 2 seconds is already plenty, but it might be worth a try.

You may have to ask in the Adafruit forums.

ShermanP:
That is indeed bizarre. I don't know anything about that module, but would suggest you try a loop trying pin 10 repeatedly until it's successful, and see how many tries it takes. The other thing would be adding a longer delay ahead of trying it the first time to see if that alone will make it work. You would think 2 seconds is already plenty, but it might be worth a try.

You may have to ask in the Adafruit forums.

Thanks, they got it solved.
For anyone who comes across this issue, if you are using the M0 RFM95 Feather Module with the Featherwing Adalogger, make sure you set pin 8 to high before you interface with the SD card.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.