Usage Example: Raspberry Pi RS-485 HAT Modbus Communication
Last revision 2025/12/17
The article demonstrates how to use a dual-channel RS-485 expansion HAT with Raspberry Pi for Modbus communication, detailing configuration steps, code modifications, and sensor data retrieval.
Hardware Preparation
- Dual-channel RS485 Expansion Hat for Raspberry Pi (SKU:DFR0824)×1
- RS485 Laser Ranging Sensor (SKU:SEN0492)×1
- Raspberry Pi 4 Model B - 8GB (SKU:DFR0697)×1
- 5V Power Supply
Wiring Diagram

| Shield | Pin name | Sensor | Pin name |
|---|---|---|---|
| RS485 Shield | GND | RS485 Sensor | GND |
| RS485 Shield | A1 | RS485 Sensor | A |
| RS485 Shield | B1 | RS485 Sensor | B |
| **Power Supply ** | Power Pin name | Sensor | Pin name |
| 5V Power Supply | +5V | RS485 Sensor | VCC |
| 5V Power Supply | GND | RS485 Sensor | GND |
Download Routine
Run the two statements below in the Raspberry Pi terminal to download the routine to the Raspberry Pi desktop.
cd Desktop
git clone https://gitee.com/DFRobot/DFRobot_CH432T_raspberrypi
Enter the statement below in Raspberry Pi to find ch432t_demo.py and double click to open it. According to Modbus frame format, modify the program to make it suitable for the RS485 laser ranging sensor used here.
cd /home/pi/Desktop/DFRobot_CH432T_raspberrypi/examples/
Instructions for Change
1、

2、

3、

Change Program
The modified codes:
from __future__ import print_function
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import time
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
from DFRobot_CH432T import * # Import DFRobot_CH432T replace importing serial library
addr_sensor = 0x50
sensor_baudrate = 115200
PORT="CH432T_PORT_2"
ser = DFRobot_CH432T(port=PORT, baudrate=115200, bytesize=8, parity='N', stopbits=1)
def main():
print("This is a demo of how to use a Modbus sensor.")
print("If you want it to work, make sure you have connected your Modbus device and changed the relevant parameters in the demo to those of your device!\r\n")
master = modbus_rtu.RtuMaster(ser)
master.set_timeout(5.0)
master.set_verbose(True)
print("ser.name = ", ser.name)
try:
while True:
ser.baudrate = sensor_baudrate
data = master.execute(addr_sensor, cst.READ_HOLDING_REGISTERS, 0x34, 1)
distance = data[0]/10
print("distance = ", distance)
time.sleep(3)
#ser.baudrate = relay_baudrate
except Exception as err:
print(str(err))
if __name__ == "__main__":
main()
Save and exit after completing changing, and enter the command below to enter the config interface.
sudo raspi-config
Select Interface Options -> Serial Port -> SPI to enable the SPI interface


Then reboot Raspberry Pi
sudo reboot
Enter the statement below in Raspberry Pi to find ch432t_demo.py.
cd /home/pi/Desktop/DFRobot_CH432T_raspberrypi/examples/
Enter the statement below to run the program and get data.
python ch432t_demo.py
The result is as shown in the figure below:

The distance read by the RS485 laser ranging sensor can be printed out, which means the RS485 communication is successful.
Was this article helpful?
