Hello,
I am trying to to write to a microSD breakout board connected through my Sparkfun SAMD21 Mini. From what I can tell, SPI is initialized, but the SD card never is initialized. The datasheet for the SAMD21 is conflicting stating that the MOSI, MISO and SCK pins are in different places. Regardless of all of that, the CS/SS pin is not being set correctly.
If there is anything you can tell that I’m doing wrong please let me know and advise otherwise please.
Please see attached the following code:
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Choose D10 as the SS pin
void setup() {
Serial.begin(9600);
// Set SS pin as output
pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH); // Set SS high initially
// Initialize SPI
SPI.begin();
// Initialize SD card with the designated chip select
if (!SD.begin(chipSelect)) {
Serial.println(“SD card initialization failed!”);
return;
}
Serial.println(“SD card is ready for use.”);
}
void loop() {
// Start SD card operation
digitalWrite(chipSelect, LOW); // Pull SS low to start communication
// Example: write data to a file
File dataFile = SD.open(“log.txt”, FILE_WRITE);
if (dataFile) {
dataFile.println(“Logging data…”);
dataFile.close();
Serial.println(“Data written to file.”);
} else {
Serial.println(“Error opening file.”);
}
digitalWrite(chipSelect, HIGH); // Pull SS high to end communication
delay(1000); // Wait a bit before the next write operation
}
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Define your SS pin
void setup() {
Serial.begin(9600);
// Initialize SS pin as an output and set it high initially
pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH);
// Begin SPI communication
SPI.begin();
// Start SD card initialization
if (!SD.begin(chipSelect)) {
Serial.println(“SD card initialization failed. Check connections and card format.”);
while (1); // Halt if initialization fails
}
Serial.println(“SD card is ready to use.”);
}
void loop() {
// Additional code to interact with the SD card
}