How to use enc28j60 ethernet shield with Arduino Mega 2560

Today it is time to talk again about one of my favourite gadgets – my Arduinos. In particular, for the Arduino Mega 2560 and how these can be used in combination with the enc28j60 ethernet adapters.

No products found.

The enc28j60 Ethernet adapter should be known to one or the other of you, who has ever tinkered with an Arduino. These are the low-cost ethernet shields, which are available for example at eBay for less then 5$. Sure, the wiring and the library (the original ethernet library isn’t compatible), are not quite as comfortable as those of the original ethernet shields, but you can also save 30$  and more compared to the official ethernet shield.

For the Arduino Uno, there is already a handful of useful articles, which refers to the enc28j60 and Ethercard library. However, for the Arduino Mega 2560, I could not find anything useful information, so I would like to address briefly below on the intricacies of this combination.

How to wire Arduino Mega 2560 and enc28j60 ethernet shield

When cabling you must be a bit careful because of some other port assignment of the Arduino Megas. A 1-to-1 replica of the Arduino Uno tutorials unfortunately does not work.

Below I’ve graphed the the wiring for you. But you can also read the exact connections from to the table below the graphic.

Arduino Mega 2560 mit enc28j60 Ethernetadapter verkabeln

Enc28j60 Arduino Mega 2560
GND GND
3.3 3.3V
SO Pin50
SI Pin51
SCK Pin52
CS Pin53

Actually for wiring only 6 pins on the ethernet board has to be used. The remaining 4 pins are not necessary. (At least as long as you can live without things like wake-on-lan.)

The trick with the Ethercard library

When the Ethercard library is used with Arduino Mega a little piece of code needs to be adjusted. Thus, the begin()-method has to be called with a reference to the Pin53.

...
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
  {
    Serial.println(P("Failed to access Ethernet controller"));
  }
  else
  {
     Serial.println(P("Ethernet is fine and up"));
  }
...

That’s all. If the pin has been adjusted in the begin()-method, the library can be used normally (as well as with the Arduino Uno).

Some more tips…

If there are questions or anything is unclear, simply write a comment.

p.s.: Anyone who is still looking for a cheap Arduino Mega, should move on to eBay. There, I’ve already bought several Arduino Megas for ~18$ per unit (including shipping costs). Nowadays Amazon offers some Arduino Megas for the same cheap price as eBay but with better shipping conditions. So I added the following list with daily updated prices for Arduino Mega 2560s on Amazon:

No products found.

14 Comments

  1. Arjensays:

    Library works for my. But how do you send a Post message to a Webserver with this library ?

    something like this?
    client.Post(“192.168.1.1/value=10”)

  2. Ricardo Castrosays:

    Hi,
    Who is interested, I have connected the CS to pin 10 arduino mega 2560, and it works perfectly, I have also connected W5500 to 5V.

  3. Ricardo Castrosays:

    To whom it may concern, I have connected the CS to pin 10 arduino mega 2560, and it works perfectly for me.

  4. pradeepkumar ksays:

    Hi ,
    I am connected the devices as mentioned above but i am getting the DHCP error like below kindly help me to resolve the same …

    [webClient]
    DHCP failed
    IP: 0.0.0.0
    GW: 0.0.0.0
    DNS: 0.0.0.0
    DNS failed
    SRV: 0.0.0.0

    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ
    <<< REQ

  5. zainabsays:

    i want interface en28j60 on lan. a/c to my lan configration ihave set

    ip =172.16.64.202
    gateway:192.168.1.4
    subnetmask: 255.255.248.0
    i have setup this by static setup method and its configured correctly. but when ping from other computers on same network its unreacheable from them :( destination unreacheable.

    any help would be appreciated

    thanks.

  6. Im new to arduino and programming, can anyone help me with the Ethercard installation?

    I downloaded https://github.com/jcw/ethercard/archive/master.zip (renaming from ethercard-master.zip to just ethercardmaster.zip)

    From the Arduino arduino 1.5.6-r2 IDE: Sketch -> Import Library… -> Add Library and pointed it to the zip

    But when I restart the IDE, I don’t see any “Ethercard” in the menu.. In the Examples->Ethernet, the same examples prior to importing are there, but nothing that says Ethercard. What am I doing wrong?

  7. Grahamsays:

    Raffi

    Thanks for this. I’ve been struggling for 2 days trying to figure out how to get the Ethernet module to work with my Mega 2560.

    I appreciate you sharing this.

    Graham

  8. Mairtinsays:

    Hi Raffi,
    I bought an ethernet shield off ebay ( http://www.ebay.co.uk/itm/261086363306?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649 ) it is built to fit directly into the arduino uno, but it fits perfectly into the atmega2560, with the only difference of CS which i cut off the shield so it wouldnt connect to pin 10(unos CS) and then brought a wire from pin 53 (megas CS) to ethernet shield.
    Ive also notices the number 53 in the snippet of code u have and had it changed before i tested it, but it still doesnt work is th pin number referenced more than 1 place in the ethernet library?
    Also I am using the ICSP pins to talk to my shield, could that be an issue im having?
    And final question is it 100% necessary to have my arduino and pc connected to a modem because im not using one at the moment?
    Thanks in advance,
    Mairtin

    • Hey Mairtin,

      it’s some time ago, that I wrote this article. But I seem to remember, that the Pin was referenced at more that one point. I think it was referenced in the header files (*.h) also. Just open the documents in your favourite editor and search for the pin number with help of Strg+F.

      No, they don’t have to be connected to a modem. You can give your Arduino a fixed IP. So you can connect it directly to your pc and send traffic/messages/packets between your pc and your Arduino. Or you take two PowerLAN (Ethernet over Powerline) adapters and connect your Arduino to your wifi router for example.

      I hope I could answer your questions. If there’s something not clear, just write me another comment.

      Greets,
      Raffi

  9. Man , you solve my problem !!

    Thx , from Argentina!

  10. Jacksonsays:

    Please advise on the cable to use ie cross over or straight through between Enc2860 aand PC. I have no router, can I still put arduino on web with out router nor switch.

    • I use a simple patch cable. No crossover. Bringing up the Arduino to net without a router or switch? Sound hard! What about buying a cheap used switch for ~5 bucks at eBay?

  11. Gregsays:

    Can the CS pin be changed to any pin? I would like to use pin 10 as I have a UTFT which uses pin 53.

    I’ve tried (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
    but I can’t seem to get it working.

    • I think it should be possible to change the pin. But you have to have a look inside the Ethercard library. Sadly there seems to be more than one point, where Pin 53 is hardcoded into the library. But if you change all references, then it should work.

Leave a Reply to pradeepkumar k Cancel reply

Please be polite. We appreciate that. Your email address will not be published and required fields are marked