Skip to content

Modbus Write Single Coil Returns Exception Code 01

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Modbus Write Single Coil Returns Exception Code 01. We cover key concepts, practical examples, and best practices.

The Problem

Modbus Function Code 05 (Write Single Coil) returns exception 01 (Illegal Function).

Quick Fix

Wrong

// Writing coil with invalid value
mb.writeSingleCoil(1, 0, 1);  // Value 1 instead of 0xFF00```

Exception 01: Illegal Function. Coil write rejected.


### Right

```cpp
#include <ModbusRtu.h>

Modbus master(1, Serial, 2);

void setup() {
  master.begin(9600, SERIAL_8N1);
  Serial.begin(9600);
}

void loop() {
  // Write Single Coil (FC 05)
  // Value MUST be 0xFF00 (ON) or 0x0000 (OFF)
  int result = master.writeCoil(1, 0, true);  // Coil 0 = ON

  if (result == 0) {
    Serial.println("Coil 0 set to ON");

    // Verify by reading back
    uint8_t verify;
    master.readCoils(1, 0, 1, &verify);
    Serial.print("Verified: ");
    Serial.println(verify ? "ON" : "OFF");
  } else {
    Serial.print("Write coil error: ");
    Serial.println(result);
  }
  delay(3000);
}```

Coil 0 set to ON Verified: ON


## Prevention

Modbus FC 05 writes a single coil. The value must be exactly 0xFF00 (ON) or 0x0000 (OFF). Any other value causes Exception 03 (Illegal Data Value). Some slaves require the coil to be writable (not all coils support write). FC 05 echoes the request on success. For writing multiple coils, use FC 15 (Write Multiple Coils).

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### What values are valid for FC 05?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Only 0xFF00 (ON) and 0x0000 (OFF). Value 0x0001 or 0x0000 are commonly seen in incorrect implementations and cause exception 03.</p>
<h3 id="fc-05-vs-fc-15">FC 05 vs FC 15?</h3><p>FC 05 = Write Single Coil (1 coil). FC 15 = Write Multiple Coils (up to ~2000 coils in one request). FC 15 uses a packed bit format.</p>
<h3 id="can-all-coils-be-written">Can all coils be written?</h3><p>No. Some coils are read-only (status indicators). Check the device documentation. Writing to read-only coils returns Exception 01 or 02.</p>
</div></details>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro