Arduino Forum

Using Arduino => Programming Questions => Topic started by: ckissi on Mar 07, 2018, 01:49 pm

Title: Led does not blink when ethernet is enabled
Post by: ckissi on Mar 07, 2018, 01:49 pm
I'm trying to blink led with ethernet enabled unfortunately cannot get it work.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE,0xAC,0xBF,0xEF,0xFE,0xAA};

IPAddress ip(192,168,0,11);
EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);
 
  pinMode(LED_BUILTIN, OUTPUT);
 
}

void loop()
{   
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}



If I comment out the "EthernetClient client;" line the LED starts to blink. Whats wrong with this?
Title: Re: Led does not blink when ethernet is enabled
Post by: PaulS on Mar 07, 2018, 01:56 pm
Quote
If I comment out the "EthernetClient client;" line the LED starts to blink. Whats wrong with this?
It doesn't make sense that commenting out that line, and only that line, causes the LED to start blinking.

Move the Serial.begin() statement to the top of setup(). Add a Serial.print() statement before and after Ethernet.begin(). It is for more likely that that call is being made but never returns.

Forgot to mention that the Ethernet shield uses pins 10, 11, 12, and 13 on the 328-based Arduinos. If you are using one of them, you'll need to move your LED to another pin.
Title: Re: Led does not blink when ethernet is enabled
Post by: wildbill on Mar 07, 2018, 01:58 pm
From the Ethernet shield documentation:
Quote
Arduino communicates with both the W5500 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno
From memory, the onboard LED is pin 13.
Title: Re: Led does not blink when ethernet is enabled
Post by: ckissi on Mar 07, 2018, 02:26 pm
It doesn't make sense that commenting out that line, and only that line, causes the LED to start blinking.

Move the Serial.begin() statement to the top of setup(). Add a Serial.print() statement before and after Ethernet.begin(). It is for more likely that that call is being made but never returns.

Forgot to mention that the Ethernet shield uses pins 10, 11, 12, and 13 on the 328-based Arduinos. If you are using one of them, you'll need to move your LED to another pin.
I use UNO with Ethernet shield. I've tried to move LED to PIN 9 : pinMode(9, OUTPUT) with no success.

Now the LED doesn't blink with or without the  Ethernet.begin(mac, ip); line.

I've tried also PIN 3. No success. Where to move the LED?  
Title: Re: Led does not blink when ethernet is enabled
Post by: ckissi on Mar 07, 2018, 02:32 pm
Got it working on port 9

I've added this at the top:

#define LED_BUILTIN 9
Title: Re: Led does not blink when ethernet is enabled
Post by: PaulS on Mar 07, 2018, 02:43 pm
Quote
#define LED_BUILTIN 9
Pin 9 doesn't have a built in LED.
Title: Re: Led does not blink when ethernet is enabled
Post by: sterretje on Mar 07, 2018, 04:45 pm
Got it working on port 9

I've added this at the top:

#define LED_BUILTIN 9
And this is the warning that you get if you set the compiler warnings to ALL in preferences.
Code: [Select]
C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_594459\sketch_mar07a.ino:1:0: warning: "LED_BUILTIN" redefined

 #define LED_BUILTIN 9

 ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:257:0,

                 from C:\Users\sterretje\AppData\Local\Temp\arduino_build_303495\sketch\sketch_mar07a.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard/pins_arduino.h:54:0: note: this is the location of the previous definition

 #define LED_BUILTIN 13

 ^


Understand what you are doing. You should not redefine a predefined pin. How would you have added a second led (on pin 9)?

E.g. with
Code: [Select]
#define OTHERLED 9

And in setup() you would have used something like
Code: [Select]
pinMode(OTHERLED, OUTPUT);

And to control the led, you would have used e.g.
Code: [Select]
digitalWrite(OTHERLED, HIGH);

Now find a decent name for OTHERLED and you're good to go.

With regards to your original problem, your shield uses the SPI bus and pin 13 is the SPI clock. Pin 13 is also the built-in led and hence you have a conflict.