Skip to Content

PLC on Ethernet with no ping? 6 checks

Short answer: when a PLC has an IP address and does not answer the ping, check the subnet, the mask, the gateway, DHCP and duplicate IP or MAC addresses and, in the code, the order of the arguments of Ethernet.begin(), in this order.

Download the checklist (PDF, 2 pages)

Descarga la lista de comprobación (PDF, 2 páginas)

These checks apply to any IPv4 device: PLC, gateway or PC. Only check 6 is specific to the Arduino Ethernet library, used by Arduino PLC and ESP32 PLC. Before you start, rule out power, cable and link: on an Arduino PLC, USB alone does not power the Ethernet port, so power the PLC with 12 to 24 Vdc. IPv6, VLANs and Modbus TCP are outside this lesson.

Six checks, in this order

CheckWhat to doGood result
1. SubnetThe mask decides which part of the address is the network. With 255.255.255.0 (/24), PLC and PC need the same first three numbers and a different last one, for example 192.168.1.20 and 192.168.1.10. With another mask the rule changes: with 255.255.0.0, only the first two numbers have to match.Same network part, different host part.
2. MaskSet the same mask on both devices. With a wrong mask, a device can take a local address for a remote one and send it to the gateway: communication fails with some nearby networks and works with distant ones.The same mask on both sides.
3. GatewayThe gateway is used only to reach other networks. Between two devices on the same subnet, the traffic goes directly through the switch. To reach another network, the gateway is the address of a router inside your subnet. With a wrong gateway, the device still talks to its own segment but fails with some or all remote networks.A router address inside the subnet, used only for other networks.
4. DHCP or staticWith DHCP, the server can give the same address, a new one or none when the lease is renewed or after a reboot. If other devices connect to the PLC, use an address reserved in the DHCP server or a fixed address outside the DHCP range, agreed with whoever manages the network. In the Arduino library, Ethernet.begin(mac) uses DHCP and returns 1 or 0; call Ethernet.maintain() regularly to renew the lease.The IP does not change after a reboot.
5. DuplicatesThe same IP or the same MAC on two devices causes intermittent faults that are hard to trace; a switch that learns the same MAC on two ports reports MAC flapping. On Industrial Shields PLCs the MAC is defined in the code, and the examples share the same value: give each device its own. To test, disconnect the PLC and ping its IP: a reply comes from another device.No reply with the PLC disconnected; one MAC per device.
6. CodeIn the Arduino Ethernet library the order is Ethernet.begin(mac, ip, dns, gateway, subnet). With three values, the third is the DNS, not the gateway. Arguments left out take defaults: dns and gateway are the device IP with the last octet set to 1, and subnet is 255.255.255.0. Old examples written for begin(mac, ip, gateway, subnet) pass the gateway as DNS and the mask as gateway.Arguments in this order; Ethernet.localIP() prints the expected address.

Symptom, cause and what to do

  • No ping, the IP belongs to another network: the PLC is outside the subnet. Use an IP and a mask from the same range.
  • Ping works with the PC next to it, not with a device on another network: the gateway. Set the router of the subnet as gateway.
  • It works, and after a reboot the client cannot find it: DHCP. Reserve the address in the server or use an agreed fixed IP.
  • It works on and off, or a second PLC drops the first: duplicate IP or MAC. One IP and one MAC per device.
  • The code compiles and the IP is right, but other networks or names do not work: the order of the arguments. Use mac, ip, dns, gateway, subnet.
  • hardwareStatus() returns EthernetNoHardware or linkStatus() returns LinkOFF: hardware, power or cable. Fix it before the six checks.

Arduino PLC and ESP32 PLC

The Industrial Shields examples for M-Duino and ESP32 PLC use Ethernet.h. The improved version of the library comes with the board packages, from version 1.1.45 for M-Duino and 2.3.0 for ESP32 PLC, and, in the ESP32 PLC package 2.7.1 checked here, begin() keeps the same order of arguments and the same defaults. If the Arduino Ethernet library is also installed in Documents/Arduino/libraries, the Arduino IDE may compile that one instead: the Used: line of the compile log tells you which copy was used.

Example configuration

Example network, with private addresses (change them for your network):

 PC  192.168.1.10/24 ---+
                        +--- switch --- router 192.168.1.1 --- other networks
 PLC 192.168.1.20/24 ---+
 PC <-> PLC: direct through the switch (the gateway is not used)
 PLC <-> other network: through 192.168.1.1 (gateway)

Reference code with Ethernet.h and a static address:

#include <Ethernet.h>

// A different MAC for each device on the network (change at least the last byte)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x21 };

IPAddress ip(192, 168, 1, 20);       // outside the DHCP range, or reserved
IPAddress dns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);   // only to leave the subnet
IPAddress subnet(255, 255, 255, 0);

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

  // Static: the order is mac, ip, dns, gateway, subnet
  Ethernet.begin(mac, ip, dns, gateway, subnet);

  // DHCP alternative: returns 1 if it gets an address, 0 if not
  // if (Ethernet.begin(mac) == 0) { Serial.println("DHCP failed"); }

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet controller not found");
  }
  if (Ethernet.linkStatus() == LinkOFF) {     // W5200 and W5500 only
    Serial.println("Cable not connected");
  }
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // With DHCP, call it often to renew the lease
  // Ethernet.maintain();
}

Typical mistake with the same values: Ethernet.begin(mac, ip, gateway, subnet); passes the gateway as DNS and the mask as gateway, and the subnet stays at the default 255.255.255.0. The code compiles without errors or warnings for ESP32 PLC with the Industrial Shields ESP32 boards 2.7.1; it has not been compiled here for Arduino PLC.

Next steps

Sources

Microsoft Learn: Understand TCP/IP addressing and subnetting basics. IETF: RFC 5227, IPv4 Address Conflict Detection. Arduino Ethernet library 2.0.2 reference: Ethernet.begin() and Ethernet.maintain(). Cisco: Troubleshoot MAC address flap notifications. Industrial Shields: Ethernet troubleshooting, Ethernet connection with an ESP32 PLC or an M-Duino PLC, Improved Ethernet library for M-Duino and ESP32 PLCs.

Seis comprobaciones, de la subred al código, para un PLC que tiene dirección IP y no responde al ping.

Commenting is not enabled on this course.