Disabling the Ethernet Chip and SD Card on an Arduino Ethernet Shield

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
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

Disabling the Ethernet Chip

To disable the Ethernet chip before using the SD card / SD card libraries, use this code:

// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

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.


Ten Things to Do After Buying a Raspberry PI

This article on ten things to do after buying a Raspberry PI shows how to install the Raspbian operating system, change settings, play a video, clone your SD card and more.

The ten things to do are the following:

  1. Install the Raspbian Operating System — you need and operating system to use your Raspberry PI and Raspbian is the “standard” OS for the PI board.
  2. Set Language and Keyboard — the default settings for the Raspberry PI are for the UK, if you are located anywhere else, you will probably need to change these settings.
  3. Setup Timezone — if you are not located in the UK, you will need to change the timezone. The Raspberry PI gets the current time from the Internet.
  4. Start the Graphical Desktop — start and explore the graphical desktop that is installed with Raspbian.
  5. Change Screen Viewing Area — there is usually a black border around the HDMI screen after installing Raspbian – remove the border to make full use of the screen area.
  6. Update System Software — make sure that the Raspbian operating system and installed applications are updated to the latest versions.
  7. Login Automatically — if you want to automatically run an embedded application on the Raspberry PI at switch on, you will first need Raspbian to boot up without the need for you to log in.
  8. Play a Video — the Raspberry PI is capable of playing video up to full 1080p HD resolution.
  9. Automatically Run an Application — if you want to use your Raspberry PI for a dedicated purpose, you will need to run your application software at switch on.
  10. Clone Your SD Card — after installing an operating system, configuring it and then developing your own custom application, it is a good idea to back up your work by cloning your Raspberry PI SD card. This will also enable you to make multiple copies of your project.

Go to the ten things to do after buying a Raspberry PI article now.

Testing the Arduino Ethernet Shield SD Card Socket

After buying an Arduino Ethernet shield and testing the Ethernet connection, you may want to test that the micro SD card socket is working and is compatible with the micro SD card that you are using.

The article on Arduino SD card testing shows how to use two of the example sketches that come with the Arduino software to test that the SD card is accessible and that it can be written to and read from.