Arduino mega 2560 how to connect to the ethernet shield.

Hi Every Body! This is my first post, finding help.

I have a arduino mega 2560 and ehternet shield r3. I tried to connect both and try make some examples from the ethernet shield, but always i recive "conection time out".

My router never see the adress of the board, and looking for some help, i found this: http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/
and din't work. I think the problem is the pingmapping is a litle diferent between the shield and the board. But i didnt find a updated tutorial explining how to reconect the pins / fix this issue.

I'll apreciate any help provided

same problem for me , with a mega rev 3 and a ethernet shield rev 3 , still if the official doc say it's compatible. So i'm wondering if my mega board as a h/w issue

With the limited information you provided, it is difficult to determine.

It does not need any pin bending or jumpers. Just plug it in. Insure the ICSP pins align correctly. Those are the pins that have the SPI data lines.

Try an example from the IDE. First, try to ping the shield's ip from your computer. Chances are if you can't ping it, something is not correct.

Maybe posting your code would help.

I'm facing a similar issue trying to make arduino mega 2560 rev3 work with arduino ethernet shield with PoE module rev3.

i'm able to ping the Ethernet shield from my PC, and i can see in wireshark that an acknowledment is also received from Ethernet shield.
Next i started up the Webserver example and tried querying the ethernet shield ip address from browser, however after some time the page request fails. i put some Serial prints in arduino code, but what i could figure out was that somehow if(client) statement is always false.

then i wrote a small C# program to connect to server , and i could see that socket connection is successfully established and i'm able to send data as well (without any errors or exceptions) but on receiving end if(client) statement is always false as if arduino never received a connection request.

interesting thing i noticed in wireshark that i could see packet with my 2 bytes (he Hex 68 65) of data sent from PC and an acknowledge packet as well from the ethernet shield.

packet sent from PC

570	732.708302	192.168.1.40	192.168.1.77	TCP	56	20892 > cddbp-alt [PSH, ACK] Seq=1 Ack=1 Win=65520 Len=2

Ack received from Arduino Ethernet shield

576	732.910612	192.168.1.77	192.168.1.40	TCP	60	cddbp-alt > 20892 [ACK] Seq=1 Ack=3 Win=2046 Len=0

sending 2 bytes after initial connection success from Cthe conn but somehow in code if(client) statement is always false.

there are few topics online available on how to do it but unfortunately they didn;t work for me.

1st approach: as mentioned on arduino help docs and also on sparkfun (where i purchased ethernet shield) it is mentioned that for mega SPI pins are 50, 51, 52 and 53. pin 53 is hardware SS and should be set to OUTPUT otherwise SPI won't work.
i made following connections

Pin-Mega Pin-Ethernet shield
4 4
10 10
11 50
12 51
13 52
Reset Reset
IOREF IOREF
AREF AREF

on mega pin 53 was left open.

network settings and code
IP: 192.168.1.77
i didn't specify any mask or gateway.

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x58, 0x6E };
IPAddress ip(192, 168, 1, 77);
IPAddress gateway(192, 168, 1, 1);
IPAddress mask(255, 255, 255, 0);
// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
int port = 8880;
EthernetServer server = EthernetServer(port);

void setup() {

 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

pinMode(53, OUTPUT);

/// disabeling the SD card
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);//, gateway, mask);
  server.begin();
  Serial.print("server is at ");
  Serial.print(Ethernet.localIP()); Serial.print(":"); Serial.println(port);
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

  if(client)
  {
      Serial.println("client connected");

      Serial.println(client.read());
      Serial.println(client.read());
      
      while(client.connected())
      {
          client.write("hi");
          client.println("hi, i am ethernet shield connected on mega 2560.");
          // give the web browser time to receive the data
          delay(1000);
          Serial.println("data sent to client.");
      }   
      // close the connection:
      client.stop();
      Serial.println("client disonnected");
  }
}

C# program code

  public void SendReceiveData()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(IPAddress.Parse("192.168.1.77"), 8880);

            if (socket.Connected)
            {

                Console.WriteLine("Client Connected.");

                //while (!Stop)
                {
                    Thread.Sleep(2000);
                    var data = Encoding.Default.GetBytes("hello");
                    socket.Send(new byte[] { data[0], data[1] });

                    while (!Stop)
                    {
                        Thread.Sleep(2000);
                        var dataAvailable = socket.Available;

                        if (dataAvailable != 0)
                        {
                            byte[] buffer = new byte[1024];

                            socket.Receive(buffer, 0, dataAvailable, SocketFlags.None);
                            Console.WriteLine("{0} bytes received.", dataAvailable);
                            Console.WriteLine(Encoding.Default.GetString(buffer, 0, dataAvailable));
                        }
                    }
                }

                socket.Disconnect(false);
                socket.Close();
            }
        }

2nd approach as SurferTim says that ethernet shield is compatible with mega and no bending pins are required,
i hooked up following connections and tried the same program as mentioned above.

ping test was successful, but the problem with the program and sending receiving data was not working.

Pin-Mega Pin-Ethernet shield
4 4
10 10
11 11
12 12
13 13
Reset Reset
IOREF IOREF
AREF AREF
ICSP header ICSP header

its a very long post but i wanted to add all the information i had. already wasted 2 days on this thing and i'm starting to hate Ethernet shield now.
any help will be much appreciated.

The Mega and newer ethernet shields need no pin bending. Works right out of the box. If the ethernet shield has a 6 pin connector (2x3) in the center of the board, it is normally Mega compatible.

Here is a web server sketch that might help troubleshoot the shield.

Change the network settings to yours. Use a web browser to see if you get a form page with two form fields on it. If you open the serial monitor, you can see the values returned in the form fields.

Some of the examples in the IDE don't work as expected. Just a warning.

I like my ethernet shield. After a very rocky start to our relationship, we are friends now. :slight_smile:

ran the web server sketch. still no luck and no clue what might be wrong.
In serial monitor it just says "Ready" nothing else ever prints there. disconnected my WiFi and opened the chrome to see if page opens and "Google Chrome could not load the webpage because 192.168.1.2 took too long to respond. "

i have even tried connected a 2nd Ethernet shield (same model, i ordered 2 of these) and same problem with the 2nd one. how else i can trouble shoot this problem?

What OS are you using? Windows?

Are you using the latest version IDE? V1.0.1 is the good one. Earlier versions had a problem with the w5100 interface.

You can ping the ethernet shield with that ip?
ping 192.168.1.2
...and you are using this URL?
http://192.168.1.2
...and if you power down the shield it no longer responds to the ping?

i'm running this on Windows 7 64-bit.
IDE version is 1.0.1

i'm directly connecting to ethernet shield using cat5 patch cable. and ping is working to ethernet shield with 192.168.1.2 (i have uploaded the webserver code you referred to)
yes http://192.168.1.2:80 (although browser automatically uses 80 for http requests, i'm not taking any chances now )

and yes, becuase it is directly connected to laptop, only ethernet shield has this ip and ping fails when i disconnect it.

i was thinking that probably VM ware adapters are messing up and so i disabled all the adapters as well, but nothings working so far. i did notice one small behavior with my C# program. C# program socket connects when i have just restarted the arduino program. and if i leave the arduino code running (do not restart it) and try to restart the C# program the socket connection fails. that tells me that ethernet shield still has old connection with it and is not discarded when C# socket connection is closed. (ofcourse in arduino program code never reaches that stage where i can do client.disconnect).

If this is the only thing connected to that ethernet port, is the ethernet port in the laptop assigned a 192.168.1.x/24 ip? No subnet conflict with your wifi? No web proxy settings?

It could be a routing problem with your main internet connection. Is that wireless? Any chance of using the wireless router with the shield connected to it?

edit: If you think it may be a proxy setting, change the server port for a test. Use port 8088 in the shield, and
http://192.168.1.2:8088

yes i disconnect the Wireless adapter when connecting to Ethernet shield or running the tests (browser tests). no web proxy settings.

laptop network settings
192.168.1.40
255.255.0.0
192.168.1.1

I would change that laptop network subnet setting to match the shield.
255.255.255.0

Did you see the edit to my previous post about the port change test? Does it work on another port?

I usually do not double post, but this may be important to you. Are you certain the wireless interface is not also 192.168.1.x/24 when it is active?

Maybe you should post the network settings for your wireless device when it is connected.

Wireless interface is on 192.168.6.x
192.168.6.91
255.255.255.0
192.168.6.1

i connected both ethernet shield and laptop with cisco switch and did the same test. but problem is still there. ping test works fine, however browser test fails.
tried it with 8088 port as well, same behavior. i'm suspicious of SPI communication between ethernet shield and arduino mega more than network settings.

Pin-Mega Pin-Ethernet shield
4 4
10 10
11 50
12 51
13 52
Reset Reset
IOREF IOREF
AREF AREF
ICSP header ICSP header

i disconnected the pins 11, 12, and 13 and ran the same tests but same behavior.

Is this the way it is connected?

Pin-Mega Pin-Ethernet shield
4 4
10 10
11 50
12 51
13 52
Reset Reset
IOREF IOREF
AREF AREF
ICSP header ICSP header

You do not need any jumpers for this. You do not need any pin bending.
Disconnect any jumpers you have on the shield.

If you were to jumper them, it must be like this:
Mega - Shield
50 - 12
51 - 11
52 - 13

edit: This will not make a difference on newer ethernet shields. Pins 11-13 on the shield are not connected to anything. All data lines goes through the ICSP connector. The w5100 SS pin is digital 10, and the SD SS is pin 4.

OH! And the important part.

Wireless interface is on 192.168.6.x
192.168.6.91
255.255.255.0
192.168.6.1

laptop network settings
192.168.1.40
255.255.0.0
192.168.1.1

The first subnet is also a part of the second if the second subnet has that netmask. Bad routing problem there.

i disconnected the jumpers.

actually the shield version i have can not directly sit on top of mega. i am using custom made small pin wires to make the connections. i have few of these wires so i'm only connecting following pins (10, 11, 12, 13, AREF, IOREF, RESET and ICSP header pins). Do i need to connect all the pins to make it work?

arduino serial monitor doesn't show any client connection when i made request from browser or from C# app.

Why won't it connect? The R3 shield on a pre-R3 Mega will overhang a bit (2 pins each side). You may need to pin bend those clear of the Mega components. Insure the ICSP connector on the shield is lined up with the ICSP pins on the Mega.

i have R3 mega and R3 ethernet shield. see attached image of what i received from sparkfun.

i disconnet the wireless and to be extra sure from network adapters i disable wireless adapter to do tests. i think that should not cause any problem.

Ethernet R3 backside.jpg

Ethernet R3 frontside.jpg

That does not seem to be assembled correctly for an R3 shield.

This is an R3 ethernet shield. Pay close attention to the ICSP connector. It is on the bottom center of the shield, so it will mate up with the ICSP pins on the Arduino.

no matter, i have connected the ICSP pins manually using the male header pin wires.

without the ICSP header the ping tests fails as well. but after connecting the ICSP headers i'm able to ping ethernet device from laptop. but the orignal problem that browser test, and other c# app fail to send any data or should i say arduino program fails to receive any data from external applications.

thanks for helping me and your patience. i really want to make it work with mega. i have completed the program with mega and transmitting data over serial. but now i want to move to ethernet, so i can transfer data over lan and don't have to use serial.

The ICSP header is how the SPI bus gets to the shield. If you can ping it, the ip is being set. That means the SPI bus is working. As to why no other sketch code works...??

I have a non-R3 Mega and ethernet shield, both official Arduino products. They are very similar to the R3 except for the 2 pins on each side. They work great together.

I wouldn't take that as an R3 shield, but then I am a bit picky. If one major component is installed incorrectly, what can you expect from the rest of it?

You might want to check the w5100 IC pin connections to the board. There have been cases of solder bridges on some of the non-Arduino products.

edit: Here is some pics of the solder bridges:

Just my opinion.