Ethernet Shield + Arduino MEGA 2560


I recently purchased a SainSmart Arduino Mega2560 + SainSmart Ethernet Sheild w/SD card.

I have gotten the SD card to function correctly, but I'm having difficulty with the Ethernet. I can have the Arduino setup an IP Address using DHCP, but whenever I try connecting to a server, for example using the WebClient example (v1.01), it always fails to connect.

I have seen in a variety of forums that you have to modify the pins and spi.h file to get the Mega to play nicely with the Ethernet chip, but the board is labeled "Mega Compatible". I even saw where you still have to set Hardware SS (Pin 53) as an output to make the SPI library function.

Am I missing anything obvious?

I use the ethernet shield with a Mega2560. You need to initialize both devices (w5100 and SD) correctly if you want to use them together. The setup code in this link works for me.,111352.msg837189.html#msg837189

edit: Insure you are using IDE v1.0.1. I had problems with the ethernet library prior to that.

Thanks for the prompt reply!

I ran the code you linked to and both the ethernet and SD card initialized fine. Does this mean I just have issues on the network end, not the Arduino?

Like I said before, I can get an IP Address using DHCP, but I'm not able to connect to anywhere.

If it initialized ok, then the network should be fine. That required dhcp to start.

Once you have the two set up, now you must get the code for each to work together. I use this code for a test. It downloads Google home page every 10 seconds. Change the server ip to yours, and see if you can download the home page from there.,99629.msg754629.html#msg754629 It disables the SD card while testing the download.

Alrighty. So I ran that other program as well, but it always failed on the client.connect() call.

This is where it's always been failing for me.

Did you use the correct network settings? The download test sketch uses a static ip.

If you want to use dhcp, replace the setup routine in the download test with the setup routine I posted earlier, then see if it works.

Alas, even using DHCP I am not able to connect to anything.

Here is the code I’m using. Note that I changed the values of pinMode(10,OUTPUT) to pinMode(53,OUTPUT), since I’m on a Mega.

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

IPAddress server(209,85,148,147); // Google

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {

  // disable w5100 while setting up SD

  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) Serial.println("failed");
  else Serial.println("OK");
  // the SD.begin(4) function returns with its SPI disabled, so nothing needs to be done

  // set up w5100 with dhcp
  Serial.print("Initializing ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println("ok");

  // the Ethernet.begin(mac) function returns with its SPI enabled, so disable it

void loop()
  if(loopCount < 10)
    loopCount = 0;

    if(!getPage(server,"/")) Serial.print("Fail ");
    else Serial.print("Pass ");


byte getPage(IPAddress ipBuf,char *page)
  int inChar;
  char outBuf[64];



    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    return 0;

      inChar =;



  return 1;

And the response

Initializing SD card...OK
Initializing ethernet...ok
Fail 1
Fail 2

I did not say anything about pin 53. I’m on a Mega. I used the code I posted on my Mega 2560 with your Google server ip, and it downloaded that Google webpage ok.

I reverted back to using pin 10, but no change. Is there anything in the indicator lights that might tell me something? I see the Link and RX lights flashing but never the TX

So I did some more digging and this is what I found:

I traced the failure back through the different .cpp files. It starts here in EthernetClient.cpp, even though I modified it to give me some more debug info about how far it got in the process.

int elsewhere = 0;
  uint8_t stat;
  while (status() != SnSR::ESTABLISHED) {
    if ((stat = status()) == SnSR::CLOSED) {
      _sock = MAX_SOCK_NUM;
      if(!elsewhere) return -3;
      else return elsewhere;
    elsewhere = stat;

For some reason, it gets as far as sending the SYN packet, but it never gets a SYN back. After waiting for one, it times out and dies.

And I have no idea why that would be happening...