IP address returns 0.0.0.0 using Ethernet shield

Hi all! I've been bending my mind backwards trying to figure out what's going on. I'm VERY new to coding (started with a crash course ~2 weeks ago), so please bear with me and ask for additional information where necessary.

I'm trying to use my Arduino uno R3 + Ethernet shield R2 (with Wiznet w5500 chip) as a server to toggle a motor's direction and speed remotely.

When I tested virtually the same code with a few changes using a simple LED project it worked perfectly fine and I was able to have the network assign my Arduino an IP address without issue. I altered the lower if statements to be relevant for a DC motor project while leaving the server code unchanged, and suddenly my Arduino will only ever be assigned an IP of 0.0.0.0... What's worse is the previously working LED code now also has the same issue. I tried manually assigning the IP address using ethernet.begin(mac,ip,myDns,subnet) and manually entering each of these with no success.

Could anyone please take a look and let me know if there's something silly I'm overlooking? (Code provided below)

---------------------------------Start code------------------------------------

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

int mSpeedOut=3;
int d1=4;
int d2=5;
int mSpeed=0;
int dt=150;

String readString;

byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x75, 0xA1 };  //The mac-address of the arduino Ethernet shield Rev. 2

EthernetServer server(80);  //80 is a frequently used server no. for LAN connections.

void setup() {
  Serial.begin(9600);

  pinMode(mSpeedOut,OUTPUT);
  pinMode(d1,OUTPUT);
  pinMode(d2,OUTPUT);

  Ethernet.begin(mac);

  server.begin();
  Serial.print("Please connect to the following IP address to continue: ");
  Serial.println(Ethernet.localIP());  //This will fetch the designated IP of the ethernet shield
}

void loop() {


  EthernetClient client = server.available();

  if (client) {
    while (client.connected()) {
      if (client.available()) {

        char c = client.read();
        Serial.print(c);
        

        if (readString.length() < 100) {
          readString += c;
        }
        //This package cointains the HTML code used to create the server page
        if (c == '\n'){
          Serial.print(readString);
          client.println("<HTTP/1.1 200 OK>");
          client.println("<Connection-Type: text/html>");
          client.println("<Connection: close>");
          client.println("");

          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Arduino Webserver</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<a href=\"/?DecSpeed\"\"><button>Speed -5%</button></a>");
          client.println("<a href=\"/?Neutral\"\"><button>Reset</button></a>");
          client.println("<a href=\"/?IncSpeed\"\"><button>Speed +5%</button></a>");
          client.println("<body style=background-color:powderblue>");

          delay(1);
          client.stop();

          if (readString.indexOf("?Neutral")>0) {
            mSpeed=0;
          }
          if (readString.indexOf("?DecSpeed")>0) {
            mSpeed=mSpeed-13;
          }
          if (readString.indexOf("?IncSpeed")>0) {
            mSpeed=mSpeed+13;
          }
          if(mSpeed<0){
            digitalWrite(d1,HIGH);
            digitalWrite(d2,LOW);
            analogWrite(mSpeedOut,-mSpeed);
          }

          if(mSpeed==0){
            analogWrite(mSpeedOut,0);
          }

          if(mSpeed>0){
            digitalWrite(d2,HIGH);
           digitalWrite(d1,LOW);
           analogWrite(mSpeedOut,mSpeed);
          }

          readString = "";

        }
      }
    }
  }
}

-----------------------------------End code------------------------------------

try the WebClient example. it has enhanced diagnostics in setup()

Found that the issue was a simple as manually assigning only the ip address and making sure pin 4 was disabled (although I still don't fully understand this). For anyone having the same issue, I used the following code to solve it:

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

EthernetServer server(80);

void setup() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  server.begin();
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());
}

void loop() {
}

you have a SD card inserted?
it is on the same SPI bus, so it must be 'unselected'

Hi Juraj, I don't! Didn't even realise that it would go through the same SPI bus as (The ethernet module?).. Initially one of my leads going to a direction pin on my motor controller chip was set on pin 4 so I imagine this likely has something to do with it.

there is a SD card holder on the Ethernet shield. it is just a holder, but if you insert a SD card, then the card is a device on SPI bus. the CS pin is connected to pin 4 of the shield

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.