Pages: [1]   Go Down
Author Topic: Arduino Due and Sd shield  (Read 1646 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
    }
  }
}
« Last Edit: June 03, 2013, 08:29:11 am by bastukee » Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Italy
Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset





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 smiley-cool

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.
« Last Edit: June 03, 2013, 08:31:06 am by bastukee » Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Italy
Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bastukee,

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

Offline Offline
Jr. Member
**
Karma: 3
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

May be this can help you:
http://forum.snootlab.com/viewtopic.php?f=32&t=804&sid=e8a4f643129fe9b018d5577679d35038


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Italy
Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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
Logged

Pages: [1]   Go Up
Jump to: