Can pins on stacked USB Host Shield on Arduino be used like it's Arduino's pins?

I have a project that needs to use USB host shield so I can plug in a USB microphone for sound recording. And I learnt that it can be used and stacked on Arduino Uno/Mega. Which I used Mega for it. However, I needed to use an SD card to save the data. So, the USB shield was stacked on most of the pins on Arduino Mega, leaving some extra pins. So where should I connect my microSD card adapter? Can I just use the pins on USB shield? Does it connect to the Arduino Mega board too? Thanks!!!

The communication between the Arduino and the SD card module is using SPI which is available on pins 50, 51 and 52 (MISO, MOSI, SCK) for the Arduino mega 2560 + the hardware SS pin (pin 53 on Arduino mega 2560). You can have multiple devices on a SPI bus

You need to look at the pinout of your specific USB host shield and see if those pins are used and for what

1 Like

Thanks! I did this, but when I run the SD cardinfo, nothing comes up in the serial monitor. I changed the pin from pin 4 to pin 53 in the code.

// 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
// MKRZero SD: SDCARD_SS_PIN

the original is
const int chipSelect = 4;

then I change it to:
const int chipSelect = 53; Because for MEGA the SPI is at 53 for CS

but I still did not get anything.

is this correct?

Did you try to see if it works without the shield first ?

yes, I removed the shield. And it still did not work.

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// 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
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 53; //I CHANGED THIS FROM 4 TO 53, ON MEGA

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


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.println();
  Serial.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }

  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
  Serial.print("Volume size (Kb):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (Mb):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Gb):  ");
  Serial.println((float)volumesize / 1024.0);

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop(void) {
}

This is the output:

it seems your white cable is 5V and the black cable is GND but you wired them to the wrong pins

you might have destroyed your module if it does not have reverse polarity protection…

yes, I fixed it because my arduino aren’t turned on because of the mistake and it still happens the same. And before the mistake was made, I did several trials with correct pin connections but still same output. What do you think is the problem here, might it be the SD card itself?

you might have damaged the module if you wired it in reverse…

did you format your SD card ? (I usually use SD Card Formatter or SDFat library’s formatting exemple )

I have built the same setup()

and uploaded this code

#include <SD.h>
Sd2Card card;
SdVolume volume;

const byte chipSelect = 53;

void setup() {

  Serial.begin(115200);

  Serial.println("\nInitializing");
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed");
    while (1);
  }

  Serial.println(F("Wiring is correct and a card is present."));
  Serial.print(F("Card type:"));

  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }
}

void loop(void) {}

Serial Monitor (@ 115200 bauds) shows

Initializing
Wiring is correct and a card is present.
Card type:SD2

so technically it should work.

1 Like

Thanks, I formatted my SD card using the ones you suggested just fine. But when I tried your code this is what I get:

And I tried running the SD examples CardInfo and then this is what I get:

I am guessing, there is something wrong with the microSD card adapter or no? Nothing seems to helps…

So the SD card works in a computer and if you are 100% sure that you connected MISO, MOSI, SCK in the right order and that your wires are of good quality (they seem so but could have been broken) then you might have a damaged SD card reader unfortunately…

(You could try with some of the SDFat library examples too just to be sure)

ahh okay, but let me try switching jumper wires just to be sure its all right.

Yes! I did it, it worked. I guess the problem initially is that I didnt format the SD card. And next, I damaged the module. I bought a new one for like 2 dollars, and it worked fine now. Thanks man!

great news… $2 well spent :slight_smile: