element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Due and Sd shield
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 415 subscribers
  • Views 389 views
  • Users 0 members are here
Related

Arduino Due and Sd shield

e14 Contributor
e14 Contributor over 13 years ago

Hi everyone, I`m making a datalogger to measure acceleration but I can`t manage to get my sd cardo working with my arduino due. I have allready tryed soldering the sd directly to the pins in my arduino due but with no succes. I put the pins the following way:

 

Vcc to 3.3v

Gnd to Gnd

Cs to my pin 52

and miso, mosi and sck to the correct pins in the spi header.

 

I run the example sketch that comes with the latest realese of the sdfatlib (bench) but it tells me via the serail monitor that the cs pin is incorrect, can anyone help me with this?

 

the sketch is the following one:

 

#include <SdFat.h>

#include <SdFatUtil.h>

 

 

// SD chip select pin

const uint8_t chipSelect = SS;

 

 

#define FILE_SIZE_MB 5

#define FILE_SIZE (1000000UL*FILE_SIZE_MB)

#define BUF_SIZE 100

 

 

uint8_t buf[BUF_SIZE];

 

 

// file system

SdFat sd;

 

 

// test file

SdFile file;

 

 

// Serial output stream

ArduinoOutStream cout(Serial);

//------------------------------------------------------------------------------

// store error strings in flash to save RAM

#define error(s) sd.errorHalt_P(PSTR(s))

//------------------------------------------------------------------------------

void setup() {

  Serial.begin(9600);

  while (!Serial){}  // wait for Leonardo

  Serial.println();

}

//------------------------------------------------------------------------------

void loop() {

  uint32_t maxLatency;

  uint32_t minLatency;

  uint32_t totalLatency;

 

 

  // discard any input

  while (Serial.read() >= 0) {}

 

 

  // pstr stores strings in flash to save RAM

  cout << pstr("Type any character to start\n");

  while (Serial.read() <= 0) {}

  delay(400);  // catch Due reset problem

 

  cout << pstr("Free RAM: ") << FreeRam() << endl;

 

 

  // initialize the SD card at SPI_FULL_SPEED for best performance.

  // try SPI_HALF_SPEED if bus errors occur.

  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();

 

 

  cout << pstr("Type is FAT") << int(sd.vol()->fatType()) << endl;

 

 

  // open or create file - truncate existing file.

  if (!file.open("BENCH.DAT", O_CREAT | O_TRUNC | O_RDWR)) {

    error("open failed");

  }

 

 

  // fill buf with known data

  for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {

    buf[i] = 'A' + (i % 26);

  }

  buf[BUF_SIZE-2] = '\r';

  buf[BUF_SIZE-1] = '\n';

 

 

  cout << pstr("File size ") << FILE_SIZE_MB << pstr("MB\n");

  cout << pstr("Buffer size ") << BUF_SIZE << pstr(" bytes\n");

  cout << pstr("Starting write test.  Please wait up to a minute\n");

 

 

  // do write test

  uint32_t n = FILE_SIZE/sizeof(buf);

  maxLatency = 0;

  minLatency = 9999999;

  totalLatency = 0;

  uint32_t t = millis();

  for (uint32_t i = 0; i < n; i++) {

    uint32_t m = micros();

    if (file.write(buf, sizeof(buf)) != sizeof(buf)) {

      error("write failed");

    }

    m = micros() - m;

    if (maxLatency < m) maxLatency = m;

    if (minLatency > m) minLatency = m;

    totalLatency += m;

  }

  file.sync();

  t = millis() - t;

  double s = file.fileSize();

  cout << pstr("Write ") << s/t << pstr(" KB/sec\n");

  cout << pstr("Maximum latency: ") << maxLatency;

  cout << pstr(" usec, Minimum Latency: ") << minLatency;

  cout << pstr(" usec, Avg Latency: ") << totalLatency/n << pstr(" usec\n\n");

  cout << pstr("Starting read test.  Please wait up to a minute\n");

  // do read test

  file.rewind();

  maxLatency = 0;

  minLatency = 9999999;

  totalLatency = 0;

  t = millis();

  for (uint32_t i = 0; i < n; i++) {

    buf[BUF_SIZE-1] = 0;

    uint32_t m = micros();

    if (file.read(buf, sizeof(buf)) != sizeof(buf)) {

      error("read failed");

    }

    m = micros() - m;

    if (maxLatency < m) maxLatency = m;

    if (minLatency > m) minLatency = m;

    totalLatency += m;

    if (buf[BUF_SIZE-1] != '\n') {

      error("data check");

    }

  }

  t = millis() - t;

  cout << pstr("Read ") << s/t << pstr(" KB/sec\n");

  cout << pstr("Maximum latency: ") << maxLatency;

  cout << pstr(" usec, Minimum Latency: ") << minLatency;

  cout << pstr(" usec, Avg Latency: ") << totalLatency/n << pstr(" usec\n\n");

  cout << pstr("Done\n\n");

  file.close();

}

  • Sign in to reply
  • Cancel
Parents
  • neilk
    0 neilk over 13 years ago

    Hi Leonardo

     

    I'm not familiar with the Due, or with the example sketch you are using, which seems very complicated.

     

    In the standard example sketch that comes with Arduino 1.0.1, up to and including 1.0.5, it states quite clearly that the pin you choose for CS MUST be set as an output. Here is the relevant section of code:

     

    Serial.print("\nInitializing SD card...");

      // On the Ethernet Shield, CS is pin 4. It's set as an output by default.

      // Note that even if it's not used as the CS pin, the hardware SS pin

      // (10 on most Arduino boards, 53 on the Mega) must be left as an output

      // or the SD library functions will not work.

      pinMode(10, OUTPUT);     // change this to 53 on a mega

     

      // we'll use the initialization code from the utility libraries

      // since we're just testing if the card is working!

      if (!card.init(SPI_HALF_SPEED, chipSelect)) {

        Serial.println("initialization failed. Things to check:");

        Serial.println("* is a card is inserted?");

        Serial.println("* Is your wiring correct?");

        Serial.println("* did you change the chipSelect pin to match your shield or module?");

        return;

      } else {

       Serial.println("Wiring is correct and a card is present.");

      }

     

    I don't see any code in your sketch which sets the pin you have chosen as CS as an output - hope I din't miss it!

     

    I've used that standard sketch reliably with my Uno setup and it only fails if the SD card is not within the spec required by the Arduino.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • neilk
    0 neilk over 13 years ago

    Hi Leonardo

     

    I'm not familiar with the Due, or with the example sketch you are using, which seems very complicated.

     

    In the standard example sketch that comes with Arduino 1.0.1, up to and including 1.0.5, it states quite clearly that the pin you choose for CS MUST be set as an output. Here is the relevant section of code:

     

    Serial.print("\nInitializing SD card...");

      // On the Ethernet Shield, CS is pin 4. It's set as an output by default.

      // Note that even if it's not used as the CS pin, the hardware SS pin

      // (10 on most Arduino boards, 53 on the Mega) must be left as an output

      // or the SD library functions will not work.

      pinMode(10, OUTPUT);     // change this to 53 on a mega

     

      // we'll use the initialization code from the utility libraries

      // since we're just testing if the card is working!

      if (!card.init(SPI_HALF_SPEED, chipSelect)) {

        Serial.println("initialization failed. Things to check:");

        Serial.println("* is a card is inserted?");

        Serial.println("* Is your wiring correct?");

        Serial.println("* did you change the chipSelect pin to match your shield or module?");

        return;

      } else {

       Serial.println("Wiring is correct and a card is present.");

      }

     

    I don't see any code in your sketch which sets the pin you have chosen as CS as an output - hope I din't miss it!

     

    I've used that standard sketch reliably with my Uno setup and it only fails if the SD card is not within the spec required by the Arduino.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube