Hello
I have been trying to use SPI1 for SdFat but have not been successful. The default SPI (on A2-A5) works just fine. Since I will be doing analogRead on analog pins I need to to use Digital pins to write data to SD card.
Here my code
#include <SdFat.h>
#include <SPI.h>
SdFat SD(&SPI1);
File myFile;
const int chipSelect = D5;
void setup()
{
SPI1.begin();
Serial.begin(9600);
SD.begin(chipSelect, SD_SCK_MHZ(50));
SD.wipe(&Serial);
// Initialize at the highest speed supported by the board
SD.begin(chipSelect, SD_SCK_MHZ(50));
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
// Write to file
myFile.println("This is a text file");
myFile.println("From photon");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
}
void loop() {
// empty
}
The code basically wipes the SD card then writes a new file to it for now. I’m using Photon for this purpose.
What should I add to have it working with SPI1? Please let me know,
It’s also not clear why you call SD.begin() twice and SPI1.begin() too?
A signle SD.begin() call should suffice.
And finally you can try a different chipSelect (e.g. A2 which has proven to work with SPI). After establishing whether or not it works with A2 you can find a digital pin to use instead.
BTW, did you know that DAC and WKP can also be used as A6 & A7?
As you stated SPI1 works with 30MHz or less.
I happened to realize for some reason the chipSelect only works with A2.
I didn’t need to call SPI1.begin(), however the second SD.begin() was needed after SD wipe() performed. Otherwise, the setup just deletes things that were on the SD and never writes anything to it.
#include <SdFat.h>
#include <SPI.h>
SdFat SD(&SPI1);
File myFile;
const int chipSelect = A2;
void setup()
{
//SPI1.begin();
Serial.begin(9600);
SD.begin(chipSelect, SD_SCK_MHZ(25));
SD.wipe(&Serial);
//re initialize after SD wipe.
SD.begin(chipSelect, SD_SCK_MHZ(25));
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile)
{
// Write to file
myFile.println("This is a text file");
myFile.println("From photon");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
}
void loop() {
// empty
}
It might be that "only" D5 is the problem due to it being part of the JTAG pin set but not that A2 is required.
Try some other pins (e.g. D0, D1, RX, TX or any other unused pin)