Go Down

Topic: Arduino Due and Sd shield (Read 1 time) previous topic - next topic

leotriador

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:

Pin 8 = CS
3.3V = 3.3V Shield
GND = GND Shield
Miso = A25
Mosi = A26
SCK = A27

And I`m running the following sketch but the sd does not work

Code: [Select]

#include <SPI.h>

#include <SD.h>
const int CS = 8;
File logfile;

void error(char *str)
{
  Serial.print("error");
  Serial.println(str);
}

void setup() {
  // open a serial connection
  Serial.begin(115200);
  SPI.begin(8);
  pinMode(CS, OUTPUT);
 
  if(!SD.begin(CS))
  {
    Serial.println("Card Failure");
    return;
  }
  Serial.println("card initialized.");
 
  // create a new file
  char filename[] = "LOGGER00.TSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
    }
  }
 
  if (! logfile) {
    error("couldnt create file");
  }
 
  Serial.print("Logging to: ");
  Serial.println(filename);
}

void loop() {

  // change the resolution to 16 bits and read A0
  analogReadResolution(12);
  Serial.print("x :");
  Serial.print(analogRead(A0)); 
  Serial.print("y :");
  Serial.print(analogRead(A1));
  Serial.print("z :");
  Serial.println(analogRead(A2));
 
 
  logfile.print(A0);
  logfile.print(A1);
  logfile.println(A2);
 

  // a little delay to not hog serial monitor
  delay(10);
}


Can anyone help me with this?

bastukee

#1
May 31, 2013, 04:42 am Last Edit: Jun 03, 2013, 03:29 pm by bastukee Reason: 1
Very important!!!

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!!!

Code: [Select]
#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
   }
 }
}
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

leotriador

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?

SimonCino

Dead Bastukee,

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

bastukee

#4
Jun 03, 2013, 03:25 pm Last Edit: Jun 03, 2013, 03:31 pm by bastukee Reason: 1




You want:

DUE        SD Adapter

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.
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

SimonCino

Bastukee,

Thank you so much for your help, I'll try soon to connect my Due to a Sd accordingly to your suggestions.
Regards


leotriador

Hi, no luck with that connection and the sketch, doesn anyone have any other idea?

SimonCino

@ 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: http://forum.arduino.cc/index.php?topic=135439.0  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.

Kind Regards

Go Up