Problems such as intermittent hanging of an Arduino sketch can occur when using the Arduino Ethernet shield with an uninitialized SD card plugged into the SD card socket of the shield. It is always best to disable the SD card before initializing the Ethernet chip, or disable the Ethernet chip before initializing the SD card.
The SPI Bus
On an Arduino Ethernet shield, the WIZnet W5100 Ethernet chip and micro SD card socket share the same SPI bus — that is the MISO, MOSI and SCK SPI bus pins. Data sent on the SPI bus will be received by all devices on the bus that are enabled. Only one device should be enabled at a time to prevent this problem.
In the case of the Arduino Ethernet shield, when initializing one device (Ethernet chip or SD card) and then only initializing the other device later, data may unintentionally be sent to the other device if it is not disabled. For example, if initializing the Ethernet chip, but having an SD card on the Ethernet shield, if the SD card is not disabled, data may be sent to the SD card and the SD card may try to respond which will corrupt data on the SPI bus.
Disabling SPI Bus Devices
The solution to this problem is to disable the device not being used. This is done by switching pin 4 of the Arduino high to disable the SD card and switching pin 10 of the Arduino high to disable the Ethernet chip.
Disabling the SD Card
To disable the SD card, use the following code before initializing the Ethernet chip with the Arduino Ethernet libraries:
// disable SD card
Disabling the Ethernet Chip
To disable the Ethernet chip before using the SD card / SD card libraries, use this code:
// disable Ethernet chip
The above code would normally be put in the setup() part of the sketch. The code should work on all Arduino boards that are compatible with the Arduino Ethernet shield, e.g. Uno, MEGA, Due, Leonardo, etc. It will also work on boards that have built-in Ethernet such as the Arduino Ethernet board.
Arduino Ethernet Shield Documentation
SPI Bus Pins on the Arduino Uno and MEGA
The Arduino Ethernet shield information page has the following to say about the SPI bus pins on the Uno and MEGA:
Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won’t work.
So on the Arduino MEGA, pin 53 is not used as the SPI slave select (SS) pin for either the Ethernet chip or SD card, but must be left as an output for the SPI bus to work.
SD Card and Ethernet Chip Disable Pins
The Ethernet shield information page has the following to say about disabling SPI bus devices on the Ethernet shield:
Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you’re not using one of the peripherals in your program, however, you’ll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.
It is always best to disable the second peripheral before initializing the first on the Ethernet shield SPI bus.