Calling Ethernet.begin sets pins high

I have a problem with my Mega 2560 and the Ethernet shield. When I call Ethernet.begin, pins 53 and 51 are set to high. Why?

All I have in the code is the following:

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

int relay1_out     = 53; 
int relay2_out     = 51;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 123);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

EthernetUDP UDP;
int port = 9254;

void setup() {
  pinMode(relay1_out, OUTPUT);
  pinMode(relay2_out, OUTPUT);
  
  digitalWrite(relay1_out, LOW);
  digitalWrite(relay2_out, LOW);
  
  Ethernet.begin(mac, ip, gateway, subnet);
  //UDP.begin(port);
}

void loop() {
  
}

If I comment out the Ethernet.begin line those pins stay low.

That is an easy one. :slight_smile:

The w5100 uses the SPI bus to communicate with the Mega. The Mega uses D50-D53 for the SPI lines.

D53 is the default slave select, so it must be set to OUTPUT, and normally the libraries that use the SPI set that line HIGH also, even tho it is not mentioned as required to set the SPI bus to master mode.

D51 is the MOSI line. It is the Mega SPI (MOSI) output pin for the SPI bus.

Thanks for your answer!

Well, it seems I have to change my soldering : ) Do you know if there is any other reserved ports I can't use?

Kinda. D4 is used by the SD card slot on the shield as the slave select. If you plan on using an SD card (or even having one in the slot), don't use that pin. I understand that D2 is used by the w5100 as an interrupt line, so I would avoid it also.

Ok!

I had made a circuit board with header pins connecting to Arduino. I have to change the connection from pins 53 and 51 to 29 and 27.

Thanks a lot!

The datasheet for ATmega1280/2560 has the full info on all the uses of all the pins if you are interrested - just
look at the pin names on the package outlines to see how many pins have extra uses! PWM and analog capabilities,
SPI and I2C bus, address/data bus and so forth... Mostly the Arduino platform hides the detail to promote
portability and simplicity.

Note that the SPI pins are also brought out in duplicate on the 6-pin ICSP header, which is good because otherwise they would
be in different places on Uno and Mega. If you want to connect to SPI use that header is possible. If you don't
want to clash with SPI then you have to avoid pins 10..13 (for Uno) and 50..53 (for Mega).