Hi everyone,
I recently got a SainSmart Ethernet Shield (W5100+uSD), which gives me grief when I try to access the SD card (or event get the SD card initialised). Documentation on that particular shield is sparse, however from checking the schematics and the little documents I did find, it is pretty similar to the original Arduino Ethernet shield (I hope I didn't overlook the major thing here
). So I thought I'd give the "CardInfo" example from the Arduino IDE a go and see what happens .... guess what?! 
I read through quite a few forum posts which basically all state to start with disabling the SS pin to avoid messing up the SPI bus. So here is the code I use.
#include <Ethernet.h>
#include <SD.h>
#include <SPI.h>
const int sdpin = 4;Â Â
const int ethpin = 10;
byte mac[] = {Â
 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
void setup()
{
 Serial.begin(9600);
 Serial.print("\nInitially switching off SD and ETH SPI ... ");
 pinMode(ethpin, OUTPUT);
 digitalWrite(ethpin, HIGH);
 pinMode(sdpin, OUTPUT);
 digitalWrite(sdpin, HIGH);
 Serial.println("done.");
 Serial.print("Trying SD card ... ");
 if (!SD.begin(sdpin)) {
  Serial.println("bad luck.");
 } else {
 Serial.println("job done.");
 }
Â
 Serial.print("Trying ethernet ... ");
 if (!Ethernet.begin(mac)) {
  Serial.println("bad luck.");
 } else {
 Serial.println("job done.");
 }
}
void loop() {
Â
}
The result in the serial console is:
Initially switching off SD and ETH SPI ... done.
Trying SD card ... bad luck.
Trying ethernet ... job done.
I also tried both parts separately. Ethernet works just fine, SD not.
- I am using the shield on an Arduino UNO R3.
- I did use the SDFormatter on the SD cards
- I did use different SD cards (2x 256MB, 1x 2GB)
I'd really appreciate if someone could point me into the right direction.
Cheers,
Christoph
Pins 4 and 10 are chip select pins. Only the one you want to talk to should be HIGH.
Hi PaulS,
Thanks, for your feedback. But I think I still have not gotten it.
I want to talk to the SD card, hence pin 4 should be HIGH? I changed the sketch accordingly:
#include <SD.h>
#include <SPI.h>
const int sdpin = 4;Â Â
const int ethpin = 10;
void setup()
{
 Serial.begin(9600);
 pinMode(ethpin, OUTPUT);
 digitalWrite(sdpin, HIGH);
 Serial.print("Trying SD card ... ");
 if (!SD.begin(sdpin)) {
  Serial.println("bad luck.");
 } else {
 Serial.println("job done.");
 }
}
void loop() {
}
Still, It did not work. I also tried
digitalWrite(ethpin, HIGH) instead of digitalWrite(sdpin, HIGH)
Both came back with the same result.
Christoph
Where did the pinMode() statement for pin 4 go?
It went ... and came back.
#include <SD.h>
#include <SPI.h>
const int sdpin = 4;Â Â
const int ethpin = 10;
void setup()
{
 Serial.begin(9600);
 pinMode(ethpin, OUTPUT);
 pinMode(sdpin, OUTPUT);
 digitalWrite(sdpin, HIGH);
 Serial.print("Trying SD card ... ");
 if (!SD.begin(sdpin)) {
  Serial.println("bad luck.");
 } else {
 Serial.println("job done.");
 }
}
void loop() {
}
However, still not working. :~
You must disable the w5100 SPI before starting the SD card. Like this:
#include <SD.h>
#include <SPI.h>
const int sdpin = 4;Â Â
const int ethpin = 10;
void setup()
{
 Serial.begin(9600);
 // disable w5100 SPI before starting SD card
 pinMode(ethpin, OUTPUT);
 digitalWrite(ethpin, HIGH);
 Serial.print("Trying SD card ... ");
 if (!SD.begin(sdpin)) {
  Serial.println("bad luck.");
 } else {
 Serial.println("job done.");
 }
}
void loop() {
}
Hi SurferTim,
Thank you. I tried that, but it didn't work. 
It is hard to tell from here what is wrong. Did it ever start ok? Or was it bad from the start?
I have a Sandisk 4GB card, and it worked fine for a while. Then one day I was testing a forum user's code (it didn't disable the SD SPI) and I forgot to remove the card from the slot. The w5100 SPI communication destroyed the format on the SD card. I had to reformat it before the SD library would recognize it again.
I know fat16lib will disagree, but I used a Windows format utility to reformat my card and it did fine. However, fat16lib recommends a specific formatting utility. You may want to search the forum for that utility or maybe he will post here with that info.
Hi SurferTim,
The SD card thing never (well, I have the shield only a couple of days now) worked. But I will start fresh tomorrow using SD Formatter to reformat the card and your code. And if its no good, I might even go for another ethernet shield to buy myself the answer whether I am too stupid or just had a hardware problem. 
Thanks so far, I'll write an update if there are new developments ....
Christoph
And if its no good, I might even go for another ethernet shield to buy myself the answer whether I am too stupid or just had a hardware problem.
A new SD card would be a lot cheaper.
Please run this program to get more information.
#include <SD.h>
#include <SPI.h>
Sd2Card card;
SdVolume vol;
SdFile root;
const int sdpin = 4;
const int ethpin = 10;
void setup() {
Serial.begin(9600);
pinMode(ethpin, OUTPUT);
digitalWrite(ethpin, HIGH);
if (card.init(SPI_HALF_SPEED, sdpin)) {
Serial.println("card init OK");
} else {
Serial.print("errorCode: ");
Serial.println(card.errorCode(), HEX);
Serial.print("errorData: ");
Serial.println(card.errorData(), HEX);
return;
}
if (vol.init(&card)) {
Serial.println("vol init OK");
} else {
Serial.println("vol init failed");
return;
}
if (root.openRoot(&vol)) {
Serial.println("open root OK");
} else {
Serial.println("open root failed");
}
}
void loop() {}
If all is well you should see this:
card init OK
vol init OK
open root OK
If there is a hardware problem with the shield or card you will see something like this:
errorCode: 1
errorData: FF
If the card format is bad you will see something like this:
card init OK
vol init failed
Or this:
card init OK
vol init OK
open root failed
Thanks for the code. I ran it with four different SD cards. The result was always
errorCode: 1
errorData: FF
I guess it doesn't look good. 
I noticed the Sainsmart ethernet shields don't use a logic level converter on the SD card, but appears to use a voltage divider with resistors. I have heard that causes problems.
Does the w5100 part of the shield work ok? That is a good way to determine if the SPI part of your shield is ok.
ErrorCode 1 means that there was no response from the first SD initialization command. This is the response you get when you remove the card and run the program.
Since you get this from four cards, there is a fundamental hardware problem.
Either SD chip select is not getting to the card or there is a SPI problem.
Thanks for all the help!
The W5100 part worked flawless (static IP, DHCP, webserver with sensordata, ... all ok). So I guess SPI works.
I have decided to return the shield and have ordered an "original" replacement (+ a very cheap one for testing). I'll give feedback once I have received the new shields.
Watch out for "very cheap" ones. If it does not have the logic level converter ICs, you could end up in the same situation.
Thanks for the hint. I'll keep that in mind. 