Ethernet and SD card

Hi. I am working on a sketch for a Leonardo which for every loop connects to a server and posts some data using a GET request. The sketch was working, but after running for a short time it would freeze, usually when calling client.print().

After many hours debugging I found out that the problem was caused by a 4Gb SD card which I earlier placed in the SD slot and forgot about. After removing the card, the sketch would run without problems.

After some googling I found out that SD card reader and Ethernet uses same SPI bus and that only one can be active at the same time by setting either pin 4 (SD) or pin 10 (Ethernet) low.

However, the strange thing was that I was not even trying to use the SD card. I did not try to read/write from the card and I didn't even include the SD lib.

Anyway, I tried to set pin 4 high and pin 10 low, in order to make sure the SD card reader was not selected, but it does not make any difference. The sketch would still freeze if the SD card is inserted.

I would really like it to work, even though the SD card is inserted, because I am planning to expand my sketch later to use both the SD card and the Ethenet at the same time.

Have anyone experienced similar behaviour or have any ideas what could be wrong?

"High", it is idle high.

The SPI bus can have many devices, each with its own slave select signal. All those signals are active low. Only one device can be active, so there may only be one of the slave select signals low at a time. They should be high before using the SPI bus, otherwise some device might get active.
For example something like this:

The I2C bus is also idle high. Both SDA and SCL can be pulled down by a Master or a Slave. There should be pullup resistors to keep the signals high.
The Serial (UART) is also idle high. Both RX and TX are (should) be high when it is idle. Therefor, if a RX input needs to be set to inactive and avoid noise, a pullup resistor is needed, to keep it high.

I have tried what you suggest in example link also. My setup routine now looks like this:

void setup() 




  // start the Ethernet connection:

  if (Ethernet.begin(mac) == 0) 

    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);


However, the result is still the same: If the SD card is present the sketch will eventually hang when making a call to client.print in the loop. If I take out the card, it will run just fine.

In the main loop I am not changing the state of pin SS, 4 or 10.

The code is okay.

Could it be hardware ?
Which Arduino board do you use, and which Ethernet shield ? I hope an Arduino Uno R3 or Mega R3 with Ethernet Shield R3.
Or are you using a cheap SD card module without level shifter for the signals ?

Do you use the official libraries, or did you install a library ?
Which Arduino IDE version do you use ? Use the latest version 1.6.7.
What about power usage ? Do you use USB power for the whole project ? Try a powered hub or a better USB cable. Or use a power supply of 7.5V to the power barrel jack.

When you have a problem and you say that you don't change the pin SS, 4 or 10 in the sketch. That raises my eyebrow. When someone on this forum doesn't show the complete sketch and is sure that it is okay, then it is probably not.

What else is connected to the Arduino board, perhaps a display or sensor that is using pin 4 or 10 and you forgot about it ?