Example Code for Python-RS485 Relay Control
Last revision 2025/12/26
The article demonstrates the use of Python's ModBus library to control a DFR0290 8-channel relay, providing step-by-step guidance and example code for effective relay management through the RS485 protocol.
Hardware Preparation
- DFR0545 LattePanda Alpha & Delta x 1
- DFR0779 RS485 Connector Expansion Shield for LattePanda Alpha&Delta x 1
- DFR0290 8 Channel IoT Ethernet Relay Controller (Support PoE and RS485) x1
Wiring Diagram
- Plug the shield into LattePanda Alpha & Delta:
You need to use nylon columns and nylon screws to fix the expansion board and LattePanda.

Operation Steps
At present, the mainstream third-party libraries for Modbus protocol in Python is shown below:
- Modbus_tk
- Pymodbus
- Minimalmodbus
- Umodbus
| Library name | Source code | 3rd party dependency | Main functions |
|---|---|---|---|
| modbus_tk | https://github.com/ljean/modbus-tk | pyserial | Support modbus TCP and RTU, both master and slave. Don't support modbus ASCII. Heavyweight. |
| pymodbus | https://github.com/riptideio/pymodbus/ | pyserial | Support modbus TCP/RTU/ASCII, master and slave. Heavyweight. |
| minimalmodbus | https://github.com/pyhys/minimalmodbus | pyserial | Only support Modbus RTU and ASCII, and only work in master(clinet). Very lightweight. |
| uModbus | https://github.com/AdvancedClimateSystems/uModbus/ | pyserial | Support both Modbus client amd server (both TCP and RTU). Don't support Modbus ASCII. Lightweight. |
Here we take the use of modbus_tk as an example.
- After connecting Lattepanda Alpha & Delta and RS485 interface extension board, boot up
- Power on RLY-8-PoE-RS485 8-channel network control relay module
- Install the Python environment, modbus_tk, and other dependent packages on LattePanda Alpha & Delta
- According to the ModBus protocol rules of the 8-channel network control relay module, write python code, then control the relay module.
Result
As shown in the figure below, run the Python script on the command line, obtain the device name and control the device turn on and off all the relays in turn

Additional Information
- Click to download the Python3 Script.
Was this article helpful?
