<Answered> SD Query

Guys & gals

I am just running some tests on a couple of SD modules, to check their operation.

I have 2 modules, 1 x SD, and 1 x uSD, and am using the native sd.h.

The uSD works with ReadWrite.ino from both the native example and from SD_Master, downloaded from Git Hub. Note, in both cases I changed CS to pin 10, the SS pin, but it also worked with pin 4.

This is the native ReadWrite.ino

/*
  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               Changed to Pin 10, SS pin

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

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

File myFile;

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


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

  if (!SD.begin(10))                              // Changed pin number from 4
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

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

The SD_Master version

/*
  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:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header

  created   Nov 2010  by David A. Mellis
  modified 9 Apr 2012  by Tom Igoe

  This example code is in the public domain.
*/

/*

  Mods done to the code:

  const int chipSelect = 10;       This was 4

*/

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

File myFile;

// change this to match your SD shield or module;
//     Arduino Ethernet shield: pin 4
//     Adafruit SD shields and modules: pin 10
//     Sparkfun SD shield: pin 8
const int chipSelect = 4;

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


  Serial.print("Initializing 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(SS, OUTPUT);

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

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

The SD doesn't work with either. With both sd.h versions, if I make CS pin 10, it fails to initialise. If I use pin 4, it Initialises and writes to the card, but fails to open the file when it attempts to reopen it to read the contents back. Same with both versions.

The output to the Serial Monitor

Initializing SD card...initialization done.
Writing to test.txt...done.
error opening test.txt

Using Serial.print I determined that it when it first tries to open the file, digitalRead(myfile) returns 1, but when it goes to open the file to read it back, digitalRead(myfile) returns 0 - WRONG!!

Apart from the different SD carriers, the major difference is the uSD module being fitted with a 74LVC125, quad non-inverting buffers, with CS, SCK & MOSI configured as I/P and MISO as O/P, and all CE pulled Low.

Is this a level shifting problem? and what's with the pins 4 and 10 working differently?

TIA

Fof

The uSD works with ReadWrite.ino from both the native example and from SD_Master, downloaded from Git Hub. Note, in both cases I changed CS to pin 10, the SS pin, but it also worked with pin 4.

the major difference is the uSD module being fitted with a 74LVC125, quad non-inverting buffers

The SD doesn't work with either. Is this a level shifting problem?

Very likely. Many people have had trouble with the modules which use the resistor network instead of the level shifting chip.

I would throw the full sized SD module away, and if you need a full sized card unit, buy one with the level shifters.

@cattledog

Exactly what I suspected. C'est la vie!

As a follow on, the 74LVC125 if only a 3v3 NI quad buffer, with 5v tolerent I/Ps, so would level shift for SCK, CS, and MOSI, but not for MISO which would be at 3v3 when high. So presumably the Arduino must see 3v3 as a high. Must check the AVR data sheet.

Thanks for the feedback.

EDIT: ATMega328 I/P is seen as High when VIH >0.6v. Plenty of head room.

Fof