IMDTouchKnowledge BaseShop main store

How-to

Running an HMI touchscreen from a microcontroller over Modbus RTU

HMI and touchscreens Published 7 min read

Short answer

Yes: an HMI touchscreen will run from a small microcontroller if the panel is set as the Modbus RTU master and the microcontroller answers as a slave holding a register table. You need RS-232 level shifting, identical serial settings, replies inside the panel's timeout, and a 3.5-character-time frame gap for detecting frame ends.

On this page
  1. Understand who polls whom
  2. Wire the physical layer
  3. Match the serial settings on both ends
  4. Implement the RTU frame handling
  5. Map screen objects to registers
  6. Verify the link before building screens
  7. Avoid the mistakes that waste the most time
  8. Questions asked

Yes, an HMI touchscreen will run from a small microcontroller such as a PIC18F452. The panel does not care whether the other end is a PLC; it needs a device that answers its polls correctly. The working arrangement is: configure the panel as a Modbus RTU master, implement a Modbus RTU slave on the microcontroller that holds a register table, shift the RS-232 levels, and reply inside the panel's configured timeout. Modbus is the one open, documented protocol for this; a microcontroller can also speak some vendors' proprietary serial protocols, but those are closed and undocumented, so Modbus RTU is the only practical choice. This article walks through the wiring, the timing numbers, the register mapping and the bench test, and links deeper material where it helps.

Understand who polls whom #

The panel configured for Modbus RTU is the master: it polls, your microcontroller is the slave and never speaks unless asked. This is the same relationship the panel has with a PLC, so the microcontroller must emulate a PLC's register table. The same master/slave split explains many faults on PLC-class links too; the pattern behind an HMI Modbus timeout caused by two masters on one line is the mirror image of your situation, where the microcontroller must strictly obey its slave role.

Wire the physical layer #

The panel's serial port is RS-232 level, with swing up to plus and minus 15 V. The microcontroller's hardware UART is 5 V TTL logic. Never connect them directly; direct RS-232 into a microcontroller pin damages the input.

Use a MAX232-class converter module between the two. These bidirectional converters safely translate RS-232 levels to TTL and back. Wire it like this:

  1. Panel TX goes through the converter into the microcontroller RX pin; panel RX goes through the converter into the microcontroller TX pin. TX and RX cross between the ends.
  2. Connect signal ground between panel, converter and microcontroller.
  3. Check the pinout of the panel's serial connector in its manual, since it varies by model and port.
  4. If you move the link to RS-485 later, the same slave firmware applies, but the wiring and termination change; the migration from an RS-232 HMI connection to RS-485 is its own topic.

For a broader view of how controllers attach to RS-232 and RS-485 serial devices, the article on connecting ControlLogix to RS232 and RS485 serial devices covers the same physical-layer decisions at PLC scale.

Match the serial settings on both ends #

In the panel's connected-device settings, select its Modbus RTU serial driver and set the baud rate, data bits, parity and stop bits. Note the slave (station) number it polls and its response timeout.

Set the microcontroller UART to identical settings. On a PIC18F452 EUSART in asynchronous mode with BRGH=1, baud = Fosc / (16 x (SPBRG + 1)). Pick a crystal that lands within about 2% of the target rate; the datasheet's error table lists the exact deviation for each combination.

Worked example, 9600 baud from a 4 MHz crystal:

  • SPBRG = 4000000 / (16 x 9600) - 1 = 25.04, so SPBRG = 25.
  • Actual baud = 4000000 / (16 x (25 + 1)) = 9615.4 baud.
  • Error = (9615.4 - 9600) / 9600 = 0.16%, well inside the roughly 2% limit.

Enable continuous receive (CREN) and service reception from an interrupt or a fast polling loop.

Implement the RTU frame handling #

An RTU frame is binary: slave ID, function code, data, then a two-byte CRC with the low byte first on the wire. Frame boundaries are marked by silence, not by delimiters, so you need a timer:

  • An inter-character gap longer than 1.5 character times marks a broken frame.
  • A silent gap of at least 3.5 character times marks the end of a frame.

Worked example at 9600 baud, 8 data bits, even parity, 1 stop bit, so 11 bits per character:

  • One character time = 11 / 9600 = 1.146 ms.
  • Inter-character limit = 1.5 x 1.146 ms = 1.72 ms.
  • Frame-end gap = 3.5 x 1.146 ms = 4.01 ms.

Accumulate received bytes into a buffer, restart the timer on every byte, and when 4.01 ms of silence passes, treat the buffer as a complete frame. At higher baud rates the Modbus specification allows using a fixed minimum gap instead of the computed value; check the serial-line specification for the exact guidance if you run above 9600.

The reply handler then does this:

  1. Check that byte 0 matches your slave ID. Ignore the frame silently if it does not.
  2. Dispatch on the function code: 01 (read coils), 03 (read holding registers), 05 (write single coil), 06 (write single register), 15 (write multiple coils) and 16 (write multiple registers) cover every object type the panel will use.
  3. Validate the address range and quantity. For an invalid address, return an exception response (function code with bit 7 set, plus exception code 02) rather than staying silent; a silent slave looks like a comm timeout to the panel.
  4. Build the reply, append the CRC, transmit, and make sure the whole thing lands inside the panel's configured response timeout. The timeout must leave the slave enough time to transmit after the master's last character, which is the same requirement PLC serial modules document.
  5. Handle OERR and FERR in the receive routine. On the PIC18 an overrun stops reception until you clear it by toggling CREN; skipping this is the usual reason a link works for minutes and then dies.

Keep the handler fast and interrupt driven. A long delay or display routine in the main loop makes the reply late and the panel times out.

Map screen objects to registers #

The mapping is not magic. In the panel's screen editor, every object is bound to an address of the connected device, and you pick that address when you place the object:

  • Bit objects (buttons, switches, lamps): coils, read with function 01, written with function 05 or 15.
  • Word objects (numeric display and entry, text boxes): holding registers, read with function 03, written with function 06 or 16.
  • Text: character pairs packed into consecutive 16-bit registers. Fix the string length and byte order on both sides.

On the microcontroller, keep a plain array of 16-bit holding registers and an array of bit flags. The Modbus handler reads and writes those arrays; your application logic reads and writes the same arrays. The panel never sees anything else. Mapping word data over serial is also where addressing conventions bite; if values come through shifted by one, the fix is the same one covered for Modbus register address conventions between a PLC and an HMI.

Watch byte order. Modbus sends the high byte first on the wire; many compilers store 16-bit values low byte first, so swap when building and parsing frames.

Keep the panel project and the microcontroller register map in one document: each object, its function code and its address.

Run this sequence on the bench, in order:

  1. Put a single numeric display on a blank screen bound to one holding register. Have the microcontroller increment that register once per second; the value should count on the panel.
  2. Add a numeric entry bound to a second register. Type a value on the panel and confirm the array changes in the debugger or on a spare output.
  3. Add one button on a coil and one lamp on another coil. Confirm both directions.
  4. Test the failure path: unplug the cable, confirm the panel reports an error, reconnect, and confirm it recovers without a power cycle.
  5. Run for at least an hour with the panel polling every screen you intend to use. Count CRC errors and overruns in the firmware.

A scope or a PC terminal on the line tells you in seconds whether the panel is transmitting at all; debug the wiring before the protocol.

Avoid the mistakes that waste the most time #

  • Check the address base first. If every value is off by one, the panel and microcontroller disagree on zero-based versus one-based numbering. Fix the offset in one place in firmware.
  • Do not poll excessively. Many objects on one screen mean many requests. Group registers into contiguous blocks so the panel reads them in few polls; the same grouping logic that keeps an HMI's indicators updating over Modbus RTU polling applies here.
  • Do not skip the level converter, and do not block in the main loop. Both failures are covered above and both are the two most common causes of a first attempt failing.

One safety note: this is a bench and integration technique. If the microcontroller drives anything a person can reach, keep the machine's own safety functions in the PLC or safety controller, not in your register table.

Questions asked

Why does the HMI show a communication timeout when the microcontroller receives the requests?

The slave is hearing the poll but not answering correctly. Check that the slave ID byte matches, recompute the CRC, and make sure the reply goes out inside the panel's configured response timeout. Also return an exception response, function code with the high bit set plus exception code 02, for addresses you do not support instead of staying silent, because a silent slave looks exactly like a dead cable to the panel.

Why must the microcontroller be the Modbus slave and not the master?

An HMI's device driver for a Modbus RTU connected device makes the panel the polling master: it sends every request. The microcontroller's job is to hold the register table and reply when asked. The panel never accepts unsolicited data from the slave side, so a slave that transmits on its own just corrupts the line.

Why does my microcontroller stop receiving after a few minutes?

On a PIC18-class EUSART, a receive overrun sets the OERR flag and halts reception until it is cleared by toggling CREN. If your receive routine never handles OERR (and FERR for framing errors), the link works for a while and then dies. Clear overrun in the receive interrupt or polling loop, and count overruns in testing to catch the underlying cause.

What baud rate and crystal should I pick for the serial link?

Pick a baud rate your crystal can hit within about 2% error. For example, a 4 MHz crystal with BRGH=1 gives SPBRG=25 for 9600 baud: 4000000 divided by 16 times 26 equals 9615.4 baud, a 0.16% error. Check the error table in the microcontroller datasheet for the rate you actually want before committing to a crystal.

How are screen objects assigned to memory in the microcontroller?

Every object in the panel's screen editor is bound to an address of the connected device: a coil, discrete input, input register or holding register. You pick that address when you place the object, and the panel reads or writes it over the serial line. In the microcontroller, keep an array of 16-bit holding registers and an array of bit flags, and point the screen objects at indexes in those arrays.

Keep reading

  • Troubleshooting

    Fixing HMI Modbus Device No Response Over RS-485

    Device no Response means the slave never returned a valid frame to that request. When a PC polling tool reads the same scanner, the bus is probably carrying two masters: run one master at a time, then copy the known-good read's serial settings, station address, and register offset into the HMI.

    HMI and touchscreens

  • How-to

    Adding a Modbus RTU Driver and External Variables in Vijeo Designer

    Add one Modbus RTU equipment driver on the HMI serial port, set the baud, parity and stop bits every slave shares, and give each device a unique slave address. Then create one external variable per value, pointing it at that device and its documented register with the matching data type. Mismatched serial settings or duplicate addresses show up as read timeouts, so commission one device at a time.

    HMI and touchscreens

  • How-to

    Connecting an XBTGT HMI to an M241 PLC over Modbus TCP

    Delete the Generic Modbus TCP slave from the M241 device tree and activate the Modbus server on the controller's Ethernet interface instead — that slave makes the M241 a client, so both devices poll and neither answers. Then map Vijeo Designer driver address 40001 to PLC word %MW0, keep TCP port 502 open between panel and controller, and shift the address map by one word if a test read lands one register off.

    HMI and touchscreens