1

I have an Arduino Zero processor (atsamd21g18a) with a microSD shield connected (check images of schematics)micro

sd

The point is that I cant make it to work, not with sd.h sketch and not with sdfat.h sketch.

Chip-select for the SD card is A3 or digital pin 17.

sd.h sketch

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  SerialUSB.begin(9600);
  delay(1000);
  
 while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  SerialUSB.print("Initializing SD card...");

  if (!SD.begin(A3)) {
    SerialUSB.println("initialization failed!");
    return;
  }
  SerialUSB.println("initialization done.");

  if (SD.exists("example.txt")) {
    SerialUSB.println("example.txt exists.");
  } else {
    SerialUSB.println("example.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  SerialUSB.println("Creating example.txt...");
  myFile = SD.open("example.txt", FILE_WRITE);
  myFile.close();

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    SerialUSB.println("example.txt exists.");
  } else {
    SerialUSB.println("example.txt doesn't exist.");
  }

  // delete the file:
  SerialUSB.println("Removing example.txt...");
  //SD.remove("example.txt");

  if (SD.exists("example.txt")) {
    SerialUSB.println("example.txt exists.");
  } else {
    SerialUSB.println("example.txt doesn't exist.");
  }
}

void loop() {
  // nothing happens after setup finishes.
}

sdfat.h code

const int chipSelect = A3;
/*
 SD card read/write
  
 This example shows how to read and write data to and from an SD card file   
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 modified by Bill Greiman 11 Apr 2011
 This example code is in the public domain.
   
 */
#include <SdFat.h>
SdFat sd;
SdFile myFile;

pinMode(chipSelect, OUTPUT);

void setup() {
  delay(5000);
  SerialUSB.begin(9600);
  SerialUSB.println("Serial start");
  //while (!SerialUSB) {}  // wait for Leonardo
  //SerialUSB.println("Type any character to start");
  //while (SerialUSB.read() <= 0) {}
  delay(1000);  // catch Due reset problem
  
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  SerialUSB.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");

  // close the file:
  myFile.close();
  SerialUSB.println("done.");

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  SerialUSB.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) SerialUSB.write(data);
  // close the file:
  myFile.close();
}

void loop() {
  // nothing happens after setup
  SerialUSB.println("loop done.");
}

I am not sure if it is a hardware or software issue, but soldering and tracks are good.

Its like the hardware gets frozen, so I cant really see the serial messages. I know its not a serial issue because I can make a simple print serial script and it shows up. I think it might be related with libraries, or maybe some hardware limitation?

Can I use any pin as Chipselect? is digitalpin17 / A3 right?

5
  • 1
    what do you mean? PA04 is analogpin3 or digitalpin17 on samd21g18a . If you mean the comments on the sketches I use it is because they are examples of arduino and should work on any board as long as you change cs pin, thats why the second says pin4 as cs
    – Espada86
    Jun 23 at 14:59
  • 1
    Oop, yep, mis-remembered--pinout confirms. Jun 23 at 15:09
  • 1
    Z1 to Z5 are varistors for overvoltage protection? Why does VDD of the SD card connect to 3.3V via a 1.69kOhm resistor and not directly? Can you check with a logic analyzer whether there is correct activity on the SD card's SPI bus? Also in the sdfat.h example you probably want to replace sd.initErrorHalt(); with { sd.initErrorPrint(); SerialUSB.flush(); } to get the error message. Jun 23 at 17:43
  • Yes, z1 to z5 are varistors of 3,3V. I could not make the sd card to work so I ended up checking schematics on google and I copied what I thought could help. thats why I added the varistors and the series resistor on the vdd. I made a previous question about a proper schematic for sd card because everything I see online is actually different, but didnt really get a proper answer. I dont have a logic analyzer, but I have a scope, I will try to see what I get. I will try the change on the sdfat code. I put a tft screen on the spi, and it does work but it has a different CS.
    – Espada86
    Jun 23 at 18:50
  • Update here. I managed to get the sd card working. Adding a resitor to the 3,3V pin of the sd card was an absolute mistake beacuse it was making the volttage to drop. So I put a jumper there for now. Another thing was adding a pull up resistor on miso. Also one varistor seemed to be damaged so I removed it. Now the SD card is working fine, but the timestamps funcions of sdfat library make the board freeze. I will try to fix that but apparently it involves edditing the library. Does anyone know how to edit the library for this?
    – Espada86
    Jun 24 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.