Ethernet.begin stops digitalWrite(Led, HIGH); working

Hi

I am new to Arduino coding although I have done a fair bit of C# and Python over the years as well as Html and Javascript so I have an understanding of the code, but this problem has me stumped without the debuging I am used to.

I have an Arduino Mega 2560 Rev3 with an Ethernet Sheild Rev3 compatable and my code is as follows

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte dns[] = {8, 8, 8, 8};
byte ip[] = {192, 168, 1, 3};
byte gateway[] = {192, 168, 1, 254};
byte subnet[] = {255, 255, 255, 0};
EthernetServer server = EthernetServer(80);

#define Led 52

void setup() {

// put your setup code here, to run once:

//Ethernet.begin(mac, ip, dns, gateway, subnet);
//server.begin();

pinMode(Led , OUTPUT);

}

void loop() {

digitalWrite(Led , HIGH);//turn the LED On by making the voltage HIGH
delay(500); // wait half a second
digitalWrite(Led , LOW);// turn the LED Off by making the voltage LOW
delay(500);

}

The above code works as expected and the Led flashes however if I uncomment this line of code

Ethernet.begin(mac, ip, dns, gateway, subnet);

the led will not work it just does nothing, I have also tried

Ethernet.begin(mac, ip, gateway, subnet); same result.

If I add code to access the web page at 192.168.1.3 everything works fine and I can print the result of commands I send to be executed but

digitalWrite(Led , HIGH);

will not switch the Led either HIGH or LOW when I send the command to the web page to switch the led with this line of code,

Ethernet.begin(mac, ip, gateway, subnet);

uncommented which of course needs to be included to be able to comunicate with the Arduino via ethernet.

Thanks in advance any help is much appreciated.
Neil

Why on earth would you put an LED on Pin 52 which is one of the SPI interface pins?!?

The Ethernet adapter is an SPI device so it has to use 50, 51, 52, and 53.

Pick a different pin for your LED.

Lookey here: :slight_smile:
Mega pinout

johnwasser:
Why on earth would you put an LED on Pin 52 which is one of the SPI interface pins?!?

The Ethernet adapter is an SPI device so it has to use 50, 51, 52, and 53.

Pick a different pin for your LED.

Thank you John

That has fixed the problem, I feel such an idiot, I just assumed that the pins the Ethernet Shield were plugged into were the pins used by it, never entered my head that it would conflict with any of the others and I just chose 52 at random.

Many thanks for you help
Neil

Neil128:
I just chose 52 at random.

Bad luck that of all the pins on an Arduino Mega you happened to pick one of the six used by the Ethernet shield. :frowning: