Hi everyone I`m trying to make a simple datalogger but with no successs, I have the arduino due and a sd module I have made the proper conections the following way:
The Due's SPI is on the 6 pin ICSP header, not shared with the I/O pins as on the Uno etc. From that pinout you should be able to connect wires to the correct places on whatever shield you use, just not plugged directly. Personally I build my own shield once I noticed the Due and my SD shields were not compatible, soldered some wires to a micro SD to SD adapter and was rolling.
ALSO VERY IMPORTANT!!!
While the Arduino Due is 3.3v, the VCC pin on the ICSP is 5V!!! The Sd card is 3.3v so do not connect to that pin...
Here is a simple data logger, with some write error reporting...let us know how it goes!!!
#include <SdFat.h>
const int chipSelect = 8;
SdFat sd;
SdFile myFile;
void setup(){
Serial2.begin(115200);
pinMode(8, OUTPUT);
sd.begin(chipSelect, SPI_FULL_SPEED);
if(!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) //To print an error message when boot up and it can't write to the SD file
Serial.println("AHHHHHHHHHH File open ERRORRRR!");
else{
String x = "Loggin Started";
Serial.print(x);
for(int i=0;i<x.length();i++) //Writes to text file
myFile.write(x[i]);
myFile.sync(); //Actual write command from write buffer
if(!myFile.getWriteError()){ //Check for writes error
digitalWrite(13, LOW); //Goes Low when no error
}
else if(myFile.getWriteError()){ //If write error, turn LED on and Write Error message
myFile.clearWriteError(); //clear error flag
digitalWrite(13, HIGH); //Goes high if error LED onboard
}
}
}
void loop(){
while(Serial2.available() > 0){ //The source of Data to be logged
char tempChar = Serial2.read(); //The source of Data to be logged
String inputString = "";
inputString += tempChar;
if(tempChar == 10){
for(int i=0;i<inputString.length();i++) //Writes bytes of Serial String to buffer
myFile.write(inputString[i]);
myFile.sync(); //Writes to File
if(!myFile.getWriteError()){ //Check for writes error
digitalWrite(13, LOW); //Use onboard LED for Error clear
}
else if(myFile.getWriteError()){ //If write error, turn LED
myFile.clearWriteError(); //Clear error flag
digitalWrite(13, HIGH); //Use onboard LED for Error Flag
sd.begin(chipSelect, SPI_FULL_SPEED); //Re-initiallize SD after an error
myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END); //Reopen file after a write error
}
inputString = ""; //Clear the buffer string
}
}
}
First of all thanks, and you are saying that I should connect the spi pins directly to the pins in my sd shield avoiding the original connections in the shield?
Thank you very much for the help and the code you posted. Could you post the connections between the due and the SD adapter?
Many thank in advance,
Regards
MISO <-> MISO
MOSI <-> MOSI
GND <-> GND (Both)
SCK <-> SCK (CLOCK)
3.3V <-> Vcc 3.3V
I/O 8 <-> SS (Chip Select) (can be any Due pin you like, my example code uses 8)
You don't need to use Reset and definitely do not connect to 5V! Use 3.3v output on Due
If you are using a shield then simply trace the pins to a spot where you can connect to, do not connect the shield to the Due as this may damage your shield or Due.
@ bastukee: I just tried the connections that you suggested and they works. Thank you so much again!
@ leotriador: are you sure that you're respecting the right connections? and which library you're using? I think that you should have a look to the following topic: SdFat for Due posted - Arduino Due - Arduino Forum and download the sdfat library where you can find a useful example named "quickstart" that may help you (and us) to understand where the problem could be. Please let me know.