How to properly use Ethernet

Almost all Arduino projects I have made use Ethernet.

I have been trying to track down an error, where the Arduino connects to the network on the bench, but not at the project location.

This raised the question: How do I properly use the Ethernet library?

I have been doing the following in setup():

    pinMode (PIN_ETHERNET_SEL, OUTPUT);
    digitalWrite (PIN_ETHERNET_SEL, HIGH);
    Ethernet.begin (MAC_ADDRESS);

... which has been working without any issues.
So I wonder why I set the CS to high, thus disabling Ethernet.
(Yes, I have seen this in my early days somewhere, always did it, without asking why... hence, my query today) :slight_smile:

I wonder, what does Ethernet.init(); do?
I see lots of examples using the Ethernet library, but not using Ethernet.begin();... even on the Arduino pages.

Also, Ethernet relies on SPI. Do I have use SPI.begin(); as well.

What is the best practice to properly start up and use Ethernet?

If it works on the bench, it's unlikely to be a problem with the begin() or init() methods. More likely some network configuration issue.

Likely, you don't have to worry about SPI initialization as it is handled by the Ethernet library.

do you use DHCP or static IP addresses?
what printout do you display on the Serial Monitor, e.g. use Ethernet.localIP() to check the IP address?
what is displayed if you run on Windows ipconfig/all (ip on Linux)?
can you logon to the local router to check what devices are attached?
what Ethernet board are you using and what library to build the project? have you tried other Ethernet boards and/or libraries?
use wireshark to look at network traffic

When that happens to me it was because I was using an Arduino Leonardo clone and, because there was no USB connection, it got stuck at:

  Serial.begin(115200);
  while (!Serial) {}

Could that be your problem?

I agree. Is there any kind of security on the network that is not supported by Arduino? Even something as "simple" as the router/DHCP controller needs to "whitelist" all the allowed MAC addresses?

Right. Router/AP's aren't simple automatons like they used to be.

Oops, I should have never mentioned that I was tracking down a problem.

My question is: How to proper initialise and start up Ethernet.

The SPI.begin(); has been clarified.

Now back to Ethernet...

Should I do Ethernet.init();?
... in addition to Ethernet.begin()?
Setting CS to HIGH? (digitalWrite (PIN_ETHERNET_SEL, HIGH);) or omit this?

Use an example provided with the Ethernet library you are using.

For further information:
What Arduino board are you using?
What Ethernet hardware are you using?
What Ethernet library are you using?

Some examples use init, others don't. begin() used by most. Some use neither.

Uno
Wiznet 5100
Ethernet lib: Copyright 2018 Paul Stoffregen

Well, I have been troubleshooting this to death, and ended up buying a KeyEstudio UNO with Wiznet5500 on the same board. Uploaded the program and ... it worked instantly. It must be something the Wiznet5100 did not like. It did not ask for a DHCP lease; hence, never progressed beyond that.

Other than that, I am still curious, what commands to use to start up Ethernet properly.

I have problems that my EthernetSheildV1 will not connect to my new router
it will however connect to an old router OK
I have no problem connecting Raspberry PIs and other equipment to my new router ethernet ports

If you look at the documentation:
https://www.arduino.cc/en/Reference/EthernetInit
You will see that Ethernet.init(pin) is used when you are using a non-default (not pin 10) CS pin.

Ethernet.begin(MAC) is where you specify the MAC address, (and optionally the IP address, Gateway Router address, DNS Server address, and Subnet Mask if you are not using DHCP)
https://www.arduino.cc/en/Reference/EthernetBegin