Mega 2560 : SD Card + Ethernet not working together

Hi !
I have an official Arduino Mega 2560 with an official Arduino Ethernet shield.
I am not able to have both network and sd card working together.

I’ve read tons of thread about this problem and i’ve tried everything :

  1. pin is not 10 but 53
  2. multiple pins from Adafruit library: sd.begin(10,11,12,13)
  3. some people speaks about pin 4
    etc etc…

Here is my actual code : it’s says card init ok, but cannot read/write test.txt file.
Note : i notice that i loose ping packets when trying to setup or read the sd card.

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

//Network settings
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0xD0, 0xB5 };
byte gateway[] = { 
  192, 168, 0, 254 };
byte subnet[] = { 
  255, 255, 255, 0 };
IPAddress ip(192,168,0,177);
EthernetServer server(8080);

File myFile;

void setup()
{
  Serial.begin(9600);
 
  Serial.print("Initializing SD card...");
  pinMode(53, OUTPUT);
  digitalWrite(53, HIGH);  
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
   
  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");
    
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    	Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
	// nothing happens after setup
}

When i disable Ethernet like this i have no problems to read the sd card :

//Ethernet.begin(mac, ip);
//server.begin();

I will appreciate any great idea :slight_smile:

Thanks

This worked for me (finally, pin 4) : Arduino SD Card Web Server using Ethernet Shield