I am using a MEGA with hardware SPI to an SD card.
The code I've found uses SD.h from the IDE.
I'm using pin 65 (A11) as the CS pin.
I've tried to look in the library to make sure if it supports hardware SPI and I need to define the CS pin but I cant see anything other than the one line in the code. I am already using 53 as a CS for another device so am I redefining the CS correctly in the if (!SD.begin(65)) line?
At the moment it wont initialise.
#include <SPI.h>
#include <SD.h>
File myFile;
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("Initializing SD card...");
if (!SD.begin(65)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
I'm just not sure the CS pin is being defined correctly.
I have a OLED hardware SPI and the SD card hardware SPI connected.
The OLEd works fine but the Sd fails its initialisation. My CS is connected to pin 65 on the MEGA but unless its the Sd.begin line that defines the pin I cant see how it is defined.
I've looked through the SD libraries and cant see anything.
Has anyone used these modules and assigned the CS pin?
I also have a software SPI RFID module connected which ha snow stopped working and it is the MISO pin that is shared with the SD card that seems to make the RFID crash.
i'm not sure how this relates to the CS issue? but even if I remove the RFID completely the SD still doesn't initialise.
I tested the code in the OP and wiring from post #@ with a Mega and 2 different SD modules. Both SD cards initialized, saved and recovered the file successfully.
Which SD module are you using? Did you format the card? Is the display still connected?
Many SD card modules have the MISO line running through a level shifter chip. Because of that the MISO line is not properly released and thus the SD modules tie up the SPI bus. Those SD modules do not play well with others. The SD modules like the Adafruit ones have the MISO line bypass the level shifter so do work properly on the bus. I used one of each type when I did the test. both OK by themselves, but only the Adafruit type will work with your RFID module.