Mega + Ethernet Shield, Best Library ?

Which is the best and more accurate library to use with an Mega2560 board with EthernetShield (the last one with sdhc card slot) ?

Must I use SD.h library or SdFat.h ?

Thanks for your comments.


I want also use Ethernet libraries, so from the choice of using to good SD lib is important (switching from Ethernet to SD et go Back from SD to Ethernet).

There will be no real difference. SD is just a thin wrapper for SdFat to provide a different API. You don’t even need to install SdFat to use its API. Here is an example that will print a line to a file.

#include <SD.h>
Sd2Card card;
SdVolume vol;
SdFile root;
SdFile file;

void setup() {
  if (!card.init() || !vol.init(&card) || !root.openRoot(&vol)) {
    Serial.println("init failed");
  file.println("Hello world");
void loop() {}

The new version of SdFat, which is in beta, has improved SPI access and plays better than many other libraries that use SPI. It also has better error handling and C++ I/O operators. It is simpler to use and has most of the features of the SD.h wrapper.

Here is the url for the beta It will soon be the production version of SdFat.

Thanks for the fast answer from you. Another trouble.

When the sd is inserted into the Ethernet Shield, Server is not responding. If I remove the card, server works again. I d'ont use for the moment SD code in my app. What could be the trouble of this fact ?


Could be SD chip select is not held high.

Stick the micro SD card in a full size holder and format it on another computer. First things first -- test the card.

In fact, now it works.

I just add two function (that could be inlined to save speed) :

const int SDchipSelectPin = 4; const int ETHERNETchipSelectPin = 53;

void SelectEthernet() { digitalWrite(ETHERNETchipSelectPin, LOW); // select ethernet mode -> LOW selects, HIGH deselects module digitalWrite(SDchipSelectPin, HIGH); delay(10); }

void SelectSd() { digitalWrite(ETHERNETchipSelectPin, HIGH); // deselect ethernet mode-> LOW selects, HIGH deselects module digitalWrite(SDchipSelectPin, LOW); delay(10); }

It allows to set correctly the pins before using one or the other harware : Ethernet or SD. Ités a little tricky to program but it works. (I'm no yet sure if 'delay(10)' lines are important.

Another thing to test : Is it possible to let an file 'open' during et switch from sd to ethernet, doing some stuff and back to the sd to continue to write datas without opening again the file.

Bye, and thanks to this forum that got a so lot of informations !