SD.begin() returns false

Hi,

I would like to test an SD module. I downloaded a library and ran the example sketch.
The CardInfo example works perfectly. It displays the type and size of the SD card.

All other examples dont work. The SD.begin(10) command always returns false. However this command is not used in the CardInfo sketch but in all others.

The SD.begin(10) command always returns false.

Is 10 the correct pin?

If your SD slot is on an ethernet shield, you will need to go thru a procedure to get both to work together. The below test code has a method to get both started. I've noticed that if I start the ethernet card the usual way, I have to power down the arduino before I can get the SD cared to work again.

//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString; 

//////////////////////

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");
          
          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");
          if (myFile) {
            //Serial.println("test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              client.write(myFile.read());
            }
            // close the file:
            myFile.close();

          }
            delay(1);
            //stopping client
            client.stop();
            readString="";
          //}
        }
      }
    }
  } 
}

Yes, 10 is the correct pin. As I said, one sketch works so I assume the wiring is correct and the SD card is formated correctly etc.

And Im working with a "standalone" SD module, not an Ethernet shield.

Yes, 10 is the correct pin. As I said, one sketch works so I assume the wiring is correct and the SD card is formated correctly etc.

One sketch that doesn't use the chip select pin works. None that do use it do. I wouldn't assume, then, that 10 is the correct pin.

I'd suggest trying other pins. How are you connecting the SD reader to the Arduino?

What board type are you trying to run this on? I ask because the default SS signal name is on different pin number for mega boards Vs 328 based boards.

Lefty

PaulS:
I wouldn't assume, then, that 10 is the correct pin.

Im sorry. I thought that the chipselect pin is necesary for all communication via SPI.

I have an Arduino Uno R3 with an ATMEGA 328.

My wiring is this:
10 - CS
11 - MOSI
12 - MISO
13 - SCLK

I tried all other pins (2 - 9) for the CS pin as well.

Hey marian, I don't know if you got this to work already or not but I ran into the same problem and figured out (through the help of others on this board) what's going on and more importantly, how to fix it. The short of the story is that it's communicating too fast at different stages within the SD library and therefore failing. The best solution is to edit the SD.cpp file. Look for a line that reads:if (!card.init(SPI_HALF_SPEED, chipSelect)) { and change that to sayif (!card.init(SPI_QUARTER_SPEED, chipSelect)) {

That will fix the error you're seeing.

Also, remember that you need to drive the card reader at 3.3V. If you're connecting it to an Arduino running at 5V, you need a signal level converter for all four lines, MOSI, MISO, SCK, and SS. Failing to do that will result in burned up SD cards.

The line in my SD.cpp looks like this:

  return card.init(SPI_HALF_SPEED, csPin, mosi, miso, sck) &&
         volume.init(card) &&
         root.openRoot(volume);

SPI_HALF_SPEED is never used at any other point in SD.cpp.
I changed it to QUARTER but the error still occurs.

My SD module has a +5V pin as well as a 3.3V pin. Do I still need to provide a 3.3V signal if I supply the 5V pin with 5V?

I can't tell what the 5V or 3V3 pins are for. The eBay page claims it can use one or the other. It looks as if there's a regulator on board so I'm assuming that the 5V is being regulated down to 3V3. What I don't see however are the signal lines being properly converted. If the SD module is indeed working at 3V3, then you must convert the signal lines as well. Without any kind of datasheet or schematic for that module, you are on your own to figure it out.

I have SFE's microSD breakout, which runs at 3V3. So in order to communicate with a 5V Uno, I used an LLC to translate the signals from 5V to 3V3 and back. 3V3 from the Uno to the breakout board. The LLC gets powered on one side with 5V and the other with 3V3. It works as expected.

Changing of the SD.cpp as you indicated, is correct by the way. It should read:

return card.init(SPI_QUARTER_SPEED, csPin, mosi, miso, sck) &&
         volume.init(card) &&
         root.openRoot(volume);

I'm getting some proper buffers in the next few days at which point I will try and see if things will run at HALF speed, or maybe even FULL speed.