I am currently trying to get the Ethernet Shield working on my Mega. I was trying to run the Webserver example but the program seems to stuck at one point, so I tried to start from scratch.

This is my test code:

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

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0F, 0xF6, 0x3D
byte subnet[] = { 255,0,0,0 };
byte gateway[] = { 2,0,0,1 };
IPAddress ip(2, 0, 0, 1);

EthernetServer server(80);

void setup() {
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.println("Ethernet started");
  Serial.println("Server started");

void loop() {
  // put your main code here, to run repeatedly:

The output I get from the serial console is:

Ethernet started

So I think the program gets stuck inside the EthernetServer::begin() function. I am aware that there are earlier versions of ethernet shields which are not compatible to the mega, but the vendor of my shield says it is.

Also I don't understand, why it outputs the first line.

Thanks for your hints!

  • Ok, I had to make some hardware changes to fit the pin layout. Now the code works but the interface does not get the desired IP adress. Instead of putting out the right IP, the serial console only prints or randomly. Any hints on that?
    – julianrabe
    Jun 8, 2015 at 21:06
  • I now know that occurs when pin 53 (SS, actual pin 10 on ethernet shield) is set to LOW and occurs when pin 53 is set to HIGH (disable ethernet). In all cases, pin 4 (SD_CS) is set to HIGH to disable the SD card reader.
    – julianrabe
    Jun 8, 2015 at 23:23

3 Answers 3


You must use these libraries:

#include <Ethernet.h>  -->  #include <Ethernet2.h>
#include <EthernetUdp.h>  -->  #include <EthernetUdp2.h>

More info at this link Arduino Ethernet Shield2

// fill in your Domain Name Server address here:
IPAddress myDns(192,168,1,1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.arduino.cc";

unsigned long lastConnectionTime = 0;
// last time you connected to the server, in milliseconds
boolean lastConnected = false;
// state of the connection last time through the main loop
const unsigned long postingInterval = 14*1000;
// delay between updates, in milliseconds

void setup() {
  pinMode (pin, OUTPUT);
  digitalWrite (pin, LOW);

  // start serial port:
  // give the ethernet module time to boot up:
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    Serial.println ("1"); delay(500);
    char c = client.read();
    } else {Serial.println ("11");delay (500);}
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println(2); delay(500);
  } else {Serial.println ("22");delay (500);}

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    Serial.println(3); delay(500);
    Serial.println ("4");delay (100);
  } else {Serial.println ("33");delay (500);}
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
  Serial.println ("end loop");

// this method makes a HTTP connection to the server:
void httpRequest() {
  // if there's a successful connection:
  Serial.println ("httpRequest"); delay(500);
  if (client.connect(server, 80)) {
     Serial.println ("H1"); delay(500);
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    Serial.println ("H11"); delay(500);
    // if you couldn't make a connection:
    Serial.println("connection failed");

I inserted debug lines. all I get are the faileure "if" comments. after 15 seconds, I get debug #3 and then "HTTP Request" then nothing but crickets....


Just to get this answered (see comments): It was a weird "bug" in my case. After days of research I found people with the same issue and in their cases it helped to cut all of the pin headers but the ICSP header by 2mm so the ICSP header had better contact. I tried it on my shield and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.