Skip to Content

How to connect an HMI screen using Arduino PLC

Kinco HMI as Modbus TCP slave and M-Duino PLC as master: settings, code and video
December 24, 2020 by
How to connect an HMI screen using Arduino PLC
Boot & Work Corp. S.L, Marti Guillem Cura

To connect an HMI screen to an Arduino based PLC, use Modbus: Modbus TCP over Ethernet or Modbus RTU over RS-485. In this example a Kinco G100E HMI is a Modbus TCP slave (IP 192.168.0.5, port 502, station 1) and an M-Duino 21+ is the master: with the Industrial Shields Modbus library it writes coil 0 to the HMI bit LB 0 and holding register 0 to the HMI word LW 0, once per second.

HMI touch screens are the usual operator interface on a machine: buttons, lamps and values that the PLC reads and writes. This guide sets up a Kinco screen in Kinco DTools, wires it to the PLC over Ethernet and adapts two examples of the Industrial Shields Modbus library. A video at the end shows the whole setup.

  1. Step 1Wire and powerHMI at 24 Vdc, PLC at 12-24 Vdc, Ethernet cable between them
  2. Step 2HMI as Modbus slaveNetwork Device Setting: Modbus TCP Slave, port 502, station 1
  3. Step 3HMI screenA Bit State Lamp on LB 0, a value on LW 0
  4. Step 4PLC as masterWrite coil 0 and register 0 with the Modbus library

What do you need?

ItemUsed in this example
PLCM-Duino 21+: Arduino Mega 2560 inside, Ethernet, 13 inputs and 8 outputs
HMIKinco G100E: 10.1 inch, 1024 x 600, Ethernet, serial ports COM0 (RS-232, RS-485, RS-422), COM1 (RS-485, RS-422) and COM2 (RS-232)
HMI softwareKinco DTools (V3.5.1 in the screenshots), from the Kinco website
PLC softwareArduino IDE with the Industrial Shields boards and the Industrial Shields Modbus library (version 1.1.0 when this guide was updated)
Wiring24 Vdc supply for the HMI, 12-24 Vdc supply for the PLC, an Ethernet cable, a USB cable for the PLC and a USB cable for the HMI

The roles are fixed by the HMI configuration: the HMI is the Modbus TCP slave (server) and waits for requests on port 502; the PLC is the master (client) and sends them. Many HMIs can also work the other way round, as masters that poll a PLC slave; see Modbus TCP slave with an Arduino based PLC for that case.

How do you wire and power the HMI and the PLC?

  • Power the HMI with a 24 Vdc supply and the PLC with its 12-24 Vdc supply (12 Vdc in this example).
  • Connect the PLC and the HMI with an Ethernet cable, directly or through a switch. Both must be in the same subnet: 192.168.0.4 for the PLC and 192.168.0.5 for the HMI in this example.
  • Connect the PC to the PLC with a USB cable to upload the program, and to the HMI with a USB cable to download the screen project.

Step 1: create the HMI project in Kinco DTools

Open Kinco DTools and create a new project. In the New Project dialog, choose the HMI Model (G100E here); the dialog shows its hardware information (Figure 1). Press Next, skip the following dialog and press Finish. A block for the screen, HMI0, appears in the center of the project (Figure 2).

Kinco DTools New Project dialog with HMI Model G100E selected and its hardware information: 10.1 inch, 1024 x 600, Ethernet
Figure 1. New Project dialog with the G100E model.
Kinco DTools project window with the G100E HMI0 block in the center and the project structure tree on the right
Figure 2. The HMI0 block in the project window.

Step 2: configure the HMI as a Modbus TCP slave

Double-click the HMI block, or right-click it and choose Attribute. In the HMI tab, press Network Device Setting (Figure 3), press Add and set the new device as in Figure 4:

FieldValue
Device TypeHMI
Device No.HMI0
IP Address192.168.0.5
Port Num502
Comm ProtocolModbus TCP Slave
Station NO.1
Kinco DTools HMI Attribute dialog, HMI tab, with the Network Device Setting button and the Network 0 IP settings
Figure 3. HMI Attribute dialog, HMI tab.
Kinco DTools Network Device Setting: device type HMI, HMI0, IP 192.168.0.5, port 502, protocol Modbus TCP Slave, station 1
Figure 4. Network Device Setting for a Modbus TCP slave.

In Figure 3 the Network 0 field still shows the default address 192.168.0.100. Whatever address the HMI finally uses, the PLC program must point to that same address (192.168.0.5 in this example).

Step 3: add the screen elements

Right-click the HMI block and choose Edit to open the screen. The Graph element window on the left lists the elements you can add.

  • For a bit, drag a Bit State Lamp to the screen and set Addr Type: LB and Address: 0. Keep the other default values. For more bits, use the next addresses (LB 1, LB 2...). In the bit state options you choose how the element behaves; this example uses toggle.
  • For a value, use an element with the LW address type (word), and increase the word length if you need more than 16 bits.

Compile with Tools > Compile. If there are no errors, download the project with Tools > Download: set the communication type to USB port and press Download.

Kinco KHDownload window with HMI0 selected, Data file checked and communication type USB port, before pressing Download
Download of the project to the HMI over USB.

Step 4: program the PLC as Modbus TCP master

Install the Industrial Shields boards in the Arduino IDE, download the Modbus library from GitHub as a ZIP file and add it with Sketch > Include Library > Add .ZIP Library. Restart the IDE if the examples do not appear.

Arduino IDE Sketch menu with Include Library and Add .ZIP Library selected, used to install the Industrial Shields Modbus library
Adding the Modbus library as a ZIP file in the Arduino IDE.

Select your M-Duino Arduino PLC model and port, and open File > Examples > Modbus > ModbusTCPMasterWriteSingleCoil. Change the configuration block at the top to the addresses of this setup:

// Modbus master IP (the M-Duino)
#define ETHERNET_MASTER_IP IPAddress(192, 168, 0, 4);
// Modbus slave IP (the Kinco HMI)
#define ETHERNET_SLAVE_IP IPAddress(192, 168, 0, 5);
// Modbus slave port
#define ETHERNET_SLAVE_PORT 502
// Modbus address of the slave (Station NO.)
#define MODBUS_SLAVE_ADDRESS 1
// Coil address to write (LB 0 in the HMI)
#define MODBUS_COIL_TO_WRITE 0
// Milliseconds between requests
#define MS_BETWEEN_REQUESTS 1000

The example opens a TCP connection to the HMI, reconnects if it drops, and every 1,000 ms sends a Write Single Coil request (function code 05) that toggles coil 0. The Bit State Lamp on LB 0 changes state each second.

static void sendModbusRequest(void) {
  static bool coilValue;

  if (master.writeSingleCoil(slaveEth, MODBUS_SLAVE_ADDRESS, MODBUS_COIL_TO_WRITE, coilValue)) {
    coilValue = !coilValue;
  }
  else {
    // For some reason, the request could not be completed.
    Serial.println("Request to write a coil failed");
  }
}

To send a value, open ModbusTCPMasterWriteSingleRegister and change the same lines (the register address is MODBUS_HOLDING_REGISTER_TO_WRITE, 0). It sends a Write Single Register request (function code 06) that alternates the value of register 0, the HMI word LW 0, between 0 and 1000:

if (master.writeSingleRegister(slaveEth, MODBUS_SLAVE_ADDRESS, MODBUS_HOLDING_REGISTER_TO_WRITE, registerValue)) {
  registerValue = registerValue == 0 ? 1000 : 0;
}

Upload the program to the PLC. We compiled both examples with these changes for the M-Duino 21+ (ATmega2560) with version 1.1.0 of the library; test them with your own HMI before you use them in a machine.

PLC requestModbus functionAddressHMI element
writeSingleCoil05, Write Single CoilCoil 0Bit State Lamp, LB 0
writeSingleRegister06, Write Single RegisterHolding register 0Word element, LW 0

More about the master library and its read functions in Modbus TCP master with Arduino and ESP32 based PLCs.

Video: Kinco HMI and M-Duino Arduino PLC over Modbus TCP

The video shows the HMI configuration and the communication between the PLC and the screen, step by step.

 
 

Troubleshooting

  • The serial monitor prints "The first connection attempt failed": check the Ethernet cable, that the PLC and the HMI are in the same subnet, and that the IP in the sketch is the one the HMI really uses.
  • The connection works but nothing changes on the screen: check that the station number in the sketch (MODBUS_SLAVE_ADDRESS) and in DTools is the same, and that the element uses LB 0 or LW 0.
  • The response contains an error: the HMI answered with a Modbus exception; check the address type and the address of the element.

FAQ about connecting an HMI to an Arduino based PLC

Can I connect the HMI over RS-485 instead of Ethernet?

Yes, with Modbus RTU, if the HMI has an RS-485 port (the G100E has it on COM0 and COM1). The PLC then uses the RTU master examples of the same library. See Modbus RTU with Arduino and ESP32 based PLCs.

Does it work with other HMI brands?

Yes, with any HMI that supports Modbus TCP or Modbus RTU. Only the configuration tool changes; the PLC side is the same.

Should the HMI be the master or the slave?

Both work. With the HMI as slave, as here, the PLC decides when to write. With the HMI as master, the HMI polls the PLC, which runs a Modbus slave; that is the usual choice when the screen must read many PLC values.

Can the PLC read values from the HMI?

Yes. The library has read functions for coils and holding registers; use them to read buttons (LB) or setpoints (LW) written by the operator.

Which Modbus protocol should I choose for a plant?

Modbus TCP between PLCs, HMIs and SCADA, Modbus RTU on the field bus. The guide to Modbus RTU, Modbus TCP and OPC UA explains where each one fits.

Take it further

Operator screens around a PLC in real use: a belt with a Panel PC, a fermentation cellar with a panel PC and a touchscreen with status LEDs.

An HMI and a PLC from the same catalog. The TouchBerry Pi is a Raspberry Pi panel PC with Node-RED dashboards and a few I/O of its own, and it talks Modbus TCP with M-Duino Arduino PLC, ESP32 PLC and Raspberry PLC.

See the TouchBerry PiChoose an industrial controller

​Search in our Blog

How to connect an HMI screen using Arduino PLC
Boot & Work Corp. S.L, Marti Guillem Cura December 24, 2020
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC Comparison