ModPackQTModPackQT
Node-RED ExamplesResourcesSign up
HomeResourcesNode-RED ExamplesWrite a Single Coil ON/OFF (FC05)
WritesNode-RED Modbus Example

Write a Single Coil ON/OFF (FC05)

Writing a coil controls a digital output — a relay, an LED, a solenoid. FC05 sets exactly one coil at a time. This example toggles a coil in response to a dashboard button press.

What you need

  • Node-RED v3+
  • node-red-contrib-modbus installed
  • Modbus device with writable digital outputs (coils)

Flow Overview


┌──────────────┐
│ Inject ON    │──┐
└──────────────┘  │   ┌────────────────────┐   ┌──────────────────────────┐
                  ├──▶│ Function           │──▶│ Modbus Flex Write FC05   │──▶ Debug
┌──────────────┐  │   │ fc:5, value:1 or 0│   │ addr:0                   │
│ Inject OFF   │──┘   └────────────────────┘   └──────────────────────────┘
└──────────────┘

Node-RED Flow JSON

Node-RED Flow JSON
[
  {"id":"inj8a","type":"inject","name":"Turn ON","payload":"true","payloadType":"bool","wires":[["fn8"]],"x":120,"y":800},
  {"id":"inj8b","type":"inject","name":"Turn OFF","payload":"false","payloadType":"bool","wires":[["fn8"]],"x":120,"y":840},
  {"id":"fn8","type":"function","name":"Build coil write","func":"msg.payload = { value: msg.payload ? 1 : 0, address: 0, quantity: 1, unitid: 1, fc: 5 };
return msg;","wires":[["req8"]],"x":330,"y":820},
  {"id":"req8","type":"modbus-flex-write","name":"FC05 Write Coil","server":"srv1","wires":[["dbg10"],[]],"x":540,"y":820},
  {"id":"dbg10","type":"debug","name":"Coil response","active":true,"wires":[],"x":740,"y":820},
  {"id":"srv1","type":"modbus-client","name":"My Slave","clienttype":"tcp","tcpHost":"192.168.1.100","tcpPort":502,"unit_id":1}
]

To import: open Node-RED → Hamburger menu → Import → paste this JSON → Deploy.

Expected Output

// ON: value=1 → wire sends 0xFF00, OFF: value=0 → wire sends 0x0000
// Slave echoes the request back confirming the write

Common Gotchas

  • In the node-red-contrib-modbus flex-write, set value to 1 (ON) or 0 (OFF) — the node translates to 0xFF00/0x0000 internally.
  • Do NOT send 0xFF00 as the payload.value — the node expects 0 or 1, not the raw protocol values.
  • The fc field in the payload must be 5 (number, not string '5').
  • Coil address 0 = coil 00001 in 1-based notation.
  • If you need to write multiple coils at once, use fc:15 with an array of 0/1 values.

Same in ModPackQT — in 30 seconds

ModPackQT has a coil toggle in every row of the register table. Click once to set ON, click again for OFF — no flow wiring, no function node needed.

Was this example helpful?

More Writes examples

All Node-RED Modbus Examples (30)

ModPackQT · Node-RED Modbus Examples · Updated 2026