Example Code for LattePanda-Python ModBus Library Control

Last revision 2026/01/08

This article presents example code for controlling relay modules using Python ModBus libraries on LattePanda V1, including hardware setup, wiring diagrams, operation steps, and results to achieve effective device control.

Hardware Preparation

Wiring Diagram

  1. Connect the Shield and LattePanda V1

  2. Use the Copper Columns and screws to fix the shield on LattePanda Board.

Connection Diagram

Not Sopport Hot Plugging

Operation Steps

The following are the popular ModBus thrid-party Python libraries at present.

  • Modbus_tk
  • Pymodbus
  • Minimalmodbus
  • Umodbus
Name Source Code Third-party Dependencies Main Function
modbus_tk https://github.com/ljean/modbus-tk Pyserial Support modbus TCP and RTU, both master and slave. Not 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). Not support Modbus ASCII. Lightweight

Here we will take modbus_tk as an example.

  1. Repeat the steps above.
  2. Install Python and modbus_tk and other dependency packages on LattePanda.
  3. Write Python codes to control the relay modudle according to the ModBus protocol principle of the 8 channel relay controller.

Result

Run Python script to get device name and control device to turn on/off all relays in order, shown as below:

Additional Information

Was this article helpful?

TOP