Using SD card with Giga R1 WiFi

Continuing the discussion from How to access SP1 on Arduino Giga:

I am confused with the documentation I need spi5. I have a pmodsd card reader attached to cs 10 then mosi pin 11 miso pin 12 and sclk pin 13 which I believed means i need to use spi1 which is spi5 but the picture says use spi1 and the text the please note says spi is spi1 and spi1 is spi5 and i dont know how to write data to my sdcard. i’ve connected it to my unor4 and that works fine but i need the power of the giga. I saw and tried to amend the code given in this thread. Help would be much appreciated.

#include <SPI.h>

const uint8_t SPI_CS_PIN = 10; // Chip Select pin

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Initialize SPI5 (default SPI object)
  SPI1.begin();  // Uses D11 (MOSI), D12 (MISO), D13 (SCK)

  // Configure chip select pin
  pinMode(SPI_CS_PIN, OUTPUT);
  digitalWrite(SPI_CS_PIN, HIGH); // Deselect device

  Serial.println("SPI5 Initialized.");
}

void loop() {
  // Begin SPI transaction
  SPI1.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

  digitalWrite(SPI_CS_PIN, LOW); // Select device

  // Send a dummy byte and receive response
  uint8_t sentData = 0x00;
  uint8_t receivedData = SPI1.transfer(sentData);

  digitalWrite(SPI_CS_PIN, HIGH); // Deselect device

  SPI1.endTransaction();

  // Print results
  Serial.print("Sent: 0x");
  Serial.print(sentData, HEX);
  Serial.print(" | Received: 0x");
  Serial.println(receivedData, HEX);

  // Debug CS pin state
  Serial.print("CS Pin State: ");
  Serial.println(digitalRead(SPI_CS_PIN));

  delay(1000);
}

Hi @pb21.

To be more clear, it means you must use the SPI1 object defined by the Arduino "SPI" library. But yes, that is correct.

I think it will be easiest to use the "SdFat" library. You can install it using the Arduino IDE Library Manager. After installing it, you will find an array of example sketches that demonstrate the usage of the library under the File > Examples > SdFat menu in Arduino IDE.

Do I need to make an amendment to the code apart from adding sdfat?

Yes. Your code does communication via the SPI bus at a low level. The "SdFat" library provides a higher level interface that abstracts the complex SPI communication that is required to work with an SD card.

Here is a simple sketch you can use to verify that the communication is working between the GIGA R1 WiFi and the SD card:

#include <SdFat.h>

SdFat sd;

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // Wait for the serial port to be opened.

  if (!sd.begin(SdSpiConfig(10, SHARED_SPI, SD_SCK_MHZ(4), &SPI1))) {
    Serial.println("SD initialization failed");
    while(true) {}  // Do not continue.
  }

  Serial.println("Files found (date time size name):");
  sd.ls(LS_R | LS_DATE | LS_SIZE);
}

void loop() {}

Thank you, so so much.

You are welcome. I'm glad if I was able to be of assistance.

Regards, Per