W5500 board only works with USB connector from pc

Hello,
I've a tricky problem. I've an "arudino nano esp32 without headers" and a W5500 ethernet modul .

The problem is, that it only works, if I connect my pc via USB-C to the USB-C port on the arduino.

With this, it worked fine, the arduino answers on requests.

If I put an USB charger with a cable on the arduino USB-C port or if I use VIN with the USB charger or with my pc usb-c cable, the programm did not longer work correctly. I've got no http anser back from the arduino.

My arduino code looks like this:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Wire.h>
#include <Adafruit_SGP30.h>
#include <Adafruit_HTU21DF.h>

#define audioDigs (30)


const int ledPin = A1;

//PIN definition
//Audio PIN
const int audioPin = A3;

//Hallsensor
const int hallSensorPinD = D9;  // Digital pin connected to the KY-024 Hall sensors
const int hallSensorPinA = A2;  // Analog pin connected to the KY-024 Hall sensors

//handle LED
unsigned long ledPreviousMillis = 0;  // speichert den letzten Zeitpunkt, an dem die LED umgeschaltet wurde
unsigned long ledCurrentMillis = 0;
unsigned long ledOnMillis = 0;        // speichert den Zeitpunkt, an dem die LED eingeschaltet wurde
const long ledBlinkInterval = 10000;  // Intervallzeit in Millisekunden (5000 ms = 5 Sekunden)
const long ledOnTime = 100;           // Zeit, die die LED eingeschaltet bleibt (100 ms)
bool ledState = LOW;                  // aktueller Zustand der LED


// Ethernet settings
byte mac[] = { 0xAA, 0xAB, 0xAC, 0x01, 0x02, 0x10 };
IPAddress ip(192, 168, 1, 249);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 16);
IPAddress targetIp(147, 54, 28, 65);

unsigned int targetPort = 80;
unsigned long ethAktivMillis = 0;
bool ethTimedout = false;
// Create instances for the sensors
Adafruit_SGP30 sgp;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();


// Create a web server on port 80
EthernetServer server(8080);
EthernetClient client;

//SGP30-Counter for baseline
int sgp30Counter = 0;

bool isHumidity = false;
bool isAudio = true;
bool isAirquality = false;
bool isHall = true;
bool isFirstTime = true;  //nur beim ersten Mal



void setup() {
  
  //neu
  pinMode(hallSensorPinD, INPUT);
  pinMode(hallSensorPinA, INPUT);
  pinMode(audioPin, INPUT);
  pinMode(ledPin, OUTPUT);

  Wire.begin();

  delay(2000);
  
  delay(2000);
  //Setup pins

  Ethernet.init(10);
  
  //  Ethernet.begin(mac);
  delay(2000);
  Ethernet.begin(mac, ip,dns,gateway,subnet);
  // Ethernet.setSubnetMask(subnet);
  // delay(500);

  // Ethernet.setGatewayIP(gateway);
  delay(2000);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    
    while (true) {
      delay(1);
    }
  }
  
  delay(2000);
  isFirstTime = true;

  //Setup pins
  digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(300);                  // wait for a second
  digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(300);                  // wait for a second


  if (!sgp.begin()) {
    
    isAirquality = false;
  } else {
    isAirquality = true;
  }
  
  if (!htu.begin()) {
    
    isHumidity = false;
  } else {
    isHumidity = true;
  }
  

  // Senden eines ARP-Requests
  //sendArpRequest();

  // // Start the server
  
  delay(1000);
  digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                 // wait for a second
  digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
  delay(1000);
  digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                 // wait for a second
  digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
}

void loop() {

  ledCurrentMillis = millis();  // aktuelle Zeit in Millisekunden

  // prüft, ob das Intervall vergangen ist und die LED ausgeschaltet ist
  if (ledCurrentMillis - ledPreviousMillis >= ledBlinkInterval && ledState == LOW) {
    ledPreviousMillis = ledCurrentMillis;  // speichert den aktuellen Zeitpunkt
    ledOnMillis = ledCurrentMillis;        // speichert den Zeitpunkt, an dem die LED eingeschaltet wurde
    ledState = HIGH;                       // setzt den Zustand der LED auf HIGH
    digitalWrite(ledPin, ledState);        // schaltet die LED ein
  }

  // prüft, ob die LED lange genug an war
  if (ledState == HIGH && ledCurrentMillis - ledOnMillis >= ledOnTime) {
    ledState = LOW;                  // setzt den Zustand der LED auf LOW
    digitalWrite(ledPin, ledState);  // schaltet die LED aus
  }
  // ethAktivMillis = millis();



  if (isFirstTime) {
    isFirstTime = false;
    digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(500);                  // wait for a second
    digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
    delay(500);
    digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(500);                  // wait for a second
    digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
    delay(500);
    digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(500);                  // wait for a second
    digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
    delay(500);
    digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(500);                  // wait for a second
    digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level)
    delay(500);
    // Make a GET request to the server
    if (client.connect(targetIp, targetPort)) {
      client.println("GET /api/ping HTTP/1.1");
      client.println("Host: 1.1.1.1");
      client.println("Connection: close");
      client.println();
    }

    // Wait for the response
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
      }
    }

    // Ensure the connection is fully closed
    
    client.stop();
    delay(1000);  // Add a delay to ensure the connection is fully closed
    
    delay(2000);
    server.begin();
    
    delay(2000);
  }

  EthernetClient serverClient = server.available();
  

  if (serverClient) {
    serverClient.setConnectionTimeout(400);
    // Wait for a client to connect
    
    String currentLine = "";
    String request = "";
    ethTimedout = false;
    while (serverClient.connected()) {
      // while (client.connected() && !ethTimedout) {
      digitalWrite(ledPin, HIGH);  // turn the LED on (HIGH is the voltage level)
      if (serverClient.available()) {
        String request = serverClient.readStringUntil('\r');
        
        serverClient.flush();
        if (request.indexOf("GET /api/v1/temperature") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleTemperature());
        } else if (request.indexOf("GET /api/v1/humidity") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleHumidity());
        } else if (request.indexOf("GET /api/v1/tvoc") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleTVOCECO2());
        } else if (request.indexOf("GET /api/v1/eco2") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleTVOCECO2());
        } else if (request.indexOf("GET /api/v1/airquality") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleTVOCECO2());
        } else if (request.indexOf("GET /api/v1/audio") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleAudio());
        } else if (request.indexOf("GET /api/v1/hall") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleHallSensor());
        } else if (request.indexOf("GET /api/v1/status") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handleStatus());
        } else if (request.indexOf("GET /api/v1/ping") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handlePing());
        } else {
          // Handle other requests
          serverClient.println("HTTP/1.1 404 Not Found");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println("Not Found");
        }
        serverClient.println();
        break;


      }
      // if (millis() - ethAktivMillis >= 10000) {
      //   ethTimedout = true;
      // }
    }
    serverClient.stop();
    delay(100);                 // wait for a second
    digitalWrite(ledPin, LOW);  // turn the LED off by making the voltage LOW
    delay(10);
    
  } else {
  }
}




String handleStatus() {
  String response = "{\"Airquality\":" + String(isAirquality) + ",\"radiation\":" + String(isHall) + ",\"audio\":" + String(isAudio) + ",\"temperature_humidity\":" + String(isHumidity) + "}";
  
  return response;
}

String handleTemperature() {
  String response = "";
  if (isHumidity) {
    float temperature = htu.readTemperature();
    response = "{\"temperature\": " + String(temperature) + "}";
    
  }
  return response;
}

String handleHumidity() {
  String response = "";
  if (isHumidity) {
    float humidity = htu.readHumidity();
    response = "{\"humidity\": " + String(humidity) + "}";
    
  }
  return response;
}
String handleTVOCECO2() {
  String response = "";
  if (isAirquality) {
    
    if (!sgp.IAQmeasure()) {
      
      return "";
    }
    response = "{\"tvoc\": " + String(sgp.TVOC) + "\"ec02\": " + String(sgp.eCO2);
    
    if (!sgp.IAQmeasureRaw()) {
      
      return response + "}";
    }
    response = response + "\"RawH2\": " + String(sgp.rawH2) + ",\"rawEthanol\": " + String(sgp.rawEthanol);

    uint16_t TVOC_base, eCO2_base;
    if (!sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      
      return response + "}";
    }

    
    response = response + "\"\eCO2_base\": " + String(eCO2_base) + "\"TVOC_base\": " + String(TVOC_base);
    return response + "}";
  } else {
    return "";
  }
}


String handleAudio() {
  if (isAudio) {
    int audioAverage = 0, audioMax = 0, audioMin = 1024;
    int audioInRectified;
    int audioInRaw;
    String response;
    for (int i = 0; i < audioDigs; i++) {
      audioInRaw = analogRead(audioPin);
      audioInRectified = abs(audioInRaw - 337);  // level shift for 3,3V
      audioMin = min(audioMin, audioInRaw);
      audioMax = max(audioMax, audioInRaw);
      audioAverage += audioInRaw;
    }
    audioAverage /= audioDigs;

    response = "{\"audio\": " + String(audioAverage) + ",\"audioInRectified\": " + String(audioInRectified) + ",\"audioMin\": " + String(audioMin) + ",\"audioMax\": " + String(audioMax) + ",\"audioAverage\": " + String(audioAverage) + ",\"audioPeakToPeak\": " + String(audioMax - audioMin) + "}";
    return response;
  } else {
    return "";
  }
}

String handleHallSensor() {
  if (isHall) {
    int sensorValue = analogRead(hallSensorPinA);       // Read the hall sensor value
    int sensorTriggered = digitalRead(hallSensorPinD);  // Read the hall sensor value
    int trueFalse = 0;
    if (sensorTriggered == 1) {
      trueFalse = 1;
    }


    String response = "{\"value\": " + String(sensorValue) + ",\"trigger\": " + String(trueFalse) + "}";  // Create a JSON response
    
    return response;
  } else {
    return "";
  }
}
String handlePing() {
  String response = "{\"ping\": " + String("Hello World") + "}";
  
  return response;
}

I've also written a simple code where only the W5500 shield is used and no other components (like sgp30, ...) to work as a rest server. But the same behaviour. With pc connected, it worked fine, without not.


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

// Ethernet settings
byte mac[] = { 0xAA, 0xAB, 0xAC, 0x01, 0x02, 0x10 };
IPAddress ip(192, 168, 1, 249);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 16);
IPAddress targetIp(147, 54, 28, 65);
unsigned int targetPort = 80;
unsigned long ethAktivMillis = 0;
bool isFirstTime = true;  //nur beim ersten Mal


// Create a web server on port 80
EthernetServer server(8080);
EthernetClient client;

void setup() {

  Ethernet.init(10);
  //  Ethernet.begin(mac);
  delay(2000);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  delay(2000);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    while (true) {
      delay(1);
    }
  }
  delay(1000);  
  server.begin();
  delay(2000);
}

void loop() {

  EthernetClient serverClient = server.available();
  if (serverClient) {
    String currentLine = "";
    String request = "";
    while (serverClient.connected()) {
      if (serverClient.available()) {
        String request = serverClient.readStringUntil('\r');
        Serial.println(request);
        serverClient.flush();
        if (request.indexOf("GET /api/v1/ping") >= 0) {
          serverClient.println("HTTP/1.1 200 OK");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println(handlePing());
        } else {
          // Handle other requests
          serverClient.println("HTTP/1.1 404 Not Found");
          serverClient.println("Content-Type: text/plain");
          serverClient.println("Connection: close");
          serverClient.println();
          serverClient.println("Not Found");
        }
        serverClient.println();
        break;
      }
    }
    serverClient.stop();
  }
}


String handlePing() {
  String response = "{\"ping\": " + String("Hello World") + "}";
  return response;
}

I believe your problem is in the way you power the project. If it works with the USB the code should be OK. Posting an annotated schematic will get you an accurate answer. I gather you are powering via Vin with 5V, that will not work very well.The processor may run but it will get lost. I like at least 7.5 volts on Vin. Also what else is connected and how is it powered?

Hello,
great!!!
Many thanks for the hint. I tried it with 12V on Vin and now, I've got a continously respond.

Besides the W5500, I've 4 sensors connected to the 3.3V output of the arduino:

  • KY-024 hall sensor
  • SGP30 air quality sensor
  • GY-21 HTU21 temperature/humidity sensor
  • GY-MAX4466 microfon amplifier

What I did in the past:
I used a PoE (power over ethernet) splitter to become the 5V on USB-C and the LAN. I've connected the LAN to the W5500 and the USB-C from the splitter to the USB-C of the arduino.
But, as I mentioned, this did not work stable.

But one thing is not clear for me:
If I connect the pc with USB-C to the arduino, it work fine every time.
If I connect a USB power supply (tried two different ones 5.17V or 4.98V) to the USB-C of the arduino, the network connection did not work correctly.

Do you have an idea why that is?

Bad quality Usb-supplies with low amp rating?

yes, could be the reason, whereat, the usb charger and the splitter should have enough power.

PoE splitter: 5V, 2A
USB-charger: 5v, 2A

The power supply I currently use on Vin has 7.5V, 1A.

Could there also be a problem with the USB-C cable itsself? That the arduino think it is a pc connected and wants to send something over the serial?

2A should be more than enough. But aliexpress 2A can be whatever and the supply can be too noisy. If you have good old Samsung charger, try with that.

Thanks for that! I'll try it with another PoE splitter with 12V directly in Vin. I let you know, if i have tested it (just ordered the splitter).