TB-4787 · REV C · Technical newsheet

Industrial IoT & MonitoringDevice profile

MicroPython Modbus RTU Slave Passes 500/500 Reads at Two Baud Rates

An MIT-licensed MicroPython Modbus RTU slave passed 500/500 reads at 9600 and 115200 baud on RP2040, RP2350 and ESP32-S3 boards, driving IoTextra Octal2 I/O over RS485.

By Grace Kim5 min read944 words

Features

  • 500/500 consecutive FC03 reads with zero errors on all three boards at 9600 and 115200 baud
  • Average response 51.6 ms at 9600 baud and ~19.8 ms at 115200 baud across RP2040, RP2350 and ESP32-S3
  • MIT-licensed pure-MicroPython slave supporting FC 0x01–0x06, 0x0F, 0x10 and broadcast address 0
  • minimalmodbus passed 24/24 checks; 20/20 back-to-back frames delivered without pauses
  • ESP32-S3 at 115200 baud required os.dupterm(None) to detach the REPL from the shared UART
MicroPython Modbus RTU Slave for IoTsmart + IoTextra Octal2
Device photoMicroPython Modbus RTU Slave for IoTsmart + IoTextra Octal2 — congerdesign / pixabay

Make Things Happy (MTH LLC) completed 500 consecutive Modbus FC03 reads on each of three boards at both 9600 and 115200 baud, with zero errors, zero CRC failures and no lost frames. The tests validated an open-source, pure-MicroPython Modbus RTU slave, released under the MIT license at github.com/makethingshappy/modbus-rtu-slave-example, and demonstrated it driving an IoTextra Octal2 I/O module over a two-wire RS485 bus.

The project pairs three controller boards — IoTsmart RP2040, IoTbase PICO with a Raspberry Pi Pico 2 W (RP2350), and IoTsmart ESP32-S3 — with the IoTextra Octal2 module, which provides four digital inputs and four NPN outputs. Any Modbus master — a PLC, SCADA host, HMI or a Python script on a PC — can read the inputs and switch the outputs over ordinary RS485 twisted pair. Arshia Keshvari, who performed the testing and wrote the Octal2 example for MTH LLC, framed the goal plainly: get a reliable Modbus RTU link between the boards and the Octal2 module over RS485.

The engine implements function codes 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0F and 0x10, plus broadcast address 0, where the write applies but no reply goes out. It runs with no external dependencies and works with an isolated UART-RS485 adapter that has automatic direction control, so no separate DE/RE pin is needed. The register map is hand-written in the example script. The vendor states plainly that this is an example to adapt, not a finished product firmware.

What did the bench measure?

Reliability runs used 500 consecutive FC03 reads per board per speed:

  • IoTsmart RP2040: 500/500 at 9600 baud, average 51.6 ms; 500/500 at 115200 baud, average 19.7 ms
  • IoTbase PICO (Pico 2 W, RP2350): 500/500 at 9600 baud, average 51.6 ms; 500/500 at 115200 baud, average 19.8 ms
  • IoTsmart ESP32-S3: 500/500 at 9600 baud, average 51.6 ms; 500/500 at 115200 baud, average 19.9 ms

Response time tracks the master and the line speed, not the slave silicon. Testing covered three masters: pymodbus (the main suite), QModMaster via GUI, and minimalmodbus, which passed 24 of 24 checks. The functional suite passed at both speeds: reading and writing registers and coils, reading discrete inputs, broadcast write, and exception 0x02 for a wrong address. Three abnormal cases were also exercised: a bad-CRC frame (the slave stays silent and recovers), a truncated frame, and 20 back-to-back frames with no pauses (20 of 20 delivered).

The 10 ms inter-byte timeout (byte_timeout_ms) holds at both baud rates. A 2 or 5 ms gap inside a frame does not break reception; a gap of 12 ms or more splits the frame, the slave stays silent, and reception of the next frame proceeds normally. The team notes one measurement limit: automatic direction switching was judged indirectly, by link stability, because no oscilloscope sat on the bench.

How does the code stay portable?

The code splits into two parts that deliberately know nothing about each other. The engine, modbus_slave.py, parses frames, checks CRC and builds responses and exceptions — it knows nothing about IoTextra. The application creates a RegisterStore and registers an on_write callback; the entire Octal2 application fits in a few lines. On a coil write, on_write receives the block name, start address and values, and switches the matching output.

The Octal2 register map is compact: discrete inputs (FC02) at addresses 0–3 map to digital inputs 0–3; coils (FC01, FC05, FC0F) at addresses 0–3 map to outputs 4–7, front-panel LEDs 1–4. The module connects over I²C through a TCA9534 expander. Analog modules are not supported in the example.

Wiring is fixed in the repository: GPIO0/GPIO1 (UART0) on RP2040 and Pico 2 W, GPIO43/GPIO44 (UART1) on ESP32-S3, plus 3V3 and GND to the adapter. The A/B/GND pair runs to a USB-RS485 adapter on the PC — an FT232R in this setup — with a 120 Ω terminator at the bus ends where needed.

What pitfalls did the bench hit?

Two platform quirks emerged. First, the ESP32-S3 worked immediately at 9600 baud but threw communication errors at 115200. The cause: the UART shared with the MicroPython REPL, whose diagnostic output corrupted Modbus frames. One line at the top of the script detaches the REPL: import os; os.dupterm(None). Whether you need it depends on your configuration and which interface the REPL uses.

Second, on the Pico 2 W running MicroPython 1.29, hardware I²C worked correctly at both 100 kHz and 400 kHz. The TCA9534 showed up in a bus scan, and subsequent reads and writes completed. The example script falls back to SoftI2C if hardware I²C fails, which Keshvari recommends for troubleshooting wiring, pull-ups and noise first.

Keshvari said the cross-platform testing mattered most: "It was important to test not just whether a single request worked, but also whether communication remained stable over repeated requests and under different conditions."

What remains untested?

The team lists explicit gaps:

  • A bus with several slaves on one line — the bench had one board
  • IoTsmart RP2350A as such — the RP2350 was tested via IoTbase PICO with a Pico 2 W
  • IoTextra analog modules
  • Master mode — this code is a slave only

The practical takeaway for integrators: a controller that already speaks Modbus can absorb a MicroPython board as a remote I/O module with no gateway and no converter. The open question the release raises is adoption: whether maintenance teams will accept a hand-written register map and a single-slave validation record in industrial settings where multi-drop buses are the norm.

via hackster.io (Source)

Filed under

  • modbus-rtu
  • micropython
  • rs485
  • remote-i-o
Share this article:

More from Grace Kim

Grace Kim

Show full bio

Correspondent covering companies, deals and strategy in Instruments, test and measurement at Testbench Report.

116 articles

Application notes

« Previous articleNext article »