Modbus Function Read Coil
DodaTech
1 min read
In this tutorial, you'll learn about Modbus Read Coils Returns Invalid Data. We cover key concepts, practical examples, and best practices.
The Problem
Modbus Function Code 01 (Read Coils) returns unexpected or invalid data.
Quick Fix
Wrong
// Reading coils as registers — wrong data type
uint16_t data[2];
mb.readHoldingRegisters(1, 0, 2, data); // FC 03, not FC 01```
Returns register values instead of coil status (0x0000/0xFF00).
### Right
```cpp
#include <ModbusRtu.h>
Modbus master(1, Serial, 2);
void setup() {
master.begin(9600, SERIAL_8N1);
}
void loop() {
// Read Coils (FC 01) — reads 1-bit values
// Parameters: slaveId, startAddr, count, destArray
uint8_t coils[8];
int result = master.readCoils(1, 0, 8, coils);
if (result == 0) {
for (int i = 0; i < 8; i++) {
Serial.print("Coil ");
Serial.print(i);
Serial.print(": ");
Serial.println(coils[i] ? "ON" : "OFF");
}
} else {
Serial.print("FC 01 error: ");
Serial.println(result);
}
delay(2000);
}```
Coil 0: ON Coil 1: OFF Coil 2: ON ...
## Prevention
Modbus FC 01 reads 1-bit coil values. Each coil is ON (0xFF00) or OFF (0x0000). Coils are packed 8 per byte in the response. Start address is 0-based (coil 1 = address 0). Use readCoils() function (not readHoldingRegisters). Coils support both read and write operations.
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">### FC 01 vs FC 02?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>FC 01 = Read Coils (read/write discrete outputs). FC 02 = Read Discrete Inputs (read-only inputs). Both return 1-bit values packed in bytes.</p>
<h3 id="how-are-coils-addressed">How are coils addressed?</h3><p>0-based. Coil 1 = address 0. Most documentation shows 1-based numbering, but the Protocol Data Unit (PDU) uses 0-based addresses.</p>
<h3 id="what-coil-values-are-valid">What coil values are valid?</h3><p>ON = 0xFF00, OFF = 0x0000. Any other value is invalid. The slave returns 0xFF00 for ON, 0x0000 for OFF on writes too.</p>
</div></details>
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro