RS485 Expansion HAT for Raspberry Pi Wiki - DFRobot

1. Introduction

This dual-channel RS-485 expansion HAT designed for Raspberry Pi adopts an isolated transceiver to achieve isolation for RS-485, which increases the reliability and anti-interference of data transmission in each channel. And it features embedded protection circuits such as lightning & surge protector, TVS diode, and power supply isolation to protect the Raspberry Pi mainboard effectively.
Besides, the order includes a screw accessory kit that allows users to tightly fix this board onto a Raspberry Pi controller.

2. Features

3. Specification

4. Dimensions

5. Board Overview

Num Label Description
1 Raspberry Pi 40 Pin Header DIP Raspberry Pi 40 Pin Header
2 RS-485 Interface RS-485 interface, GND, A1, B1, A2, B2 are connected to the corresponding RS485 port respectively
3 TX/RX Indicator The RX/TX indicator of the corresponding port will flash during RS-485 communication, TX for blue and RX for green
4 120Ω Terminal Resistor There may be a garbled signal during long-distance RS-485 communication, at this time the 120Ω resistor should be connected. In normal cases, just put it to NC.

6. Introduction to Communication Protocols

6.1 RS485

RS-485 is one of the typical serial communication standards.

And the RS-485 interface features good noise rejection, long transmission distance, multidrop capability, simple wiring, and other advantages, so it is widely used in industrial applications.

RS-485 communication protocol only defines transmission voltage, impedance and other electrical characteristics, but does not define the software protocol. It transmits data signals using differential transmission method, which makes it able to suppress common-mode interference. And the bus transceiver has high sensitivity and can detect voltages as low as 200mV. In addition, it's very convenient for users when using RS-485 for multi-point interconnection as it can save a lot of signal lines.
RS-485 uses a pair of twisted-pair cables often defined as A and B respectively. Generally, the positive level between the transmitting drivers A and B is +2 - +6V, which is a positive 1 logic state; the negative level is -2 - 6V, which is a negative 0 logic state. And it's transmission distance can reach up to about 1200 meters, and the transmission rate is inversely proportional to the distance. The highest rate can only be obtained at very short distance. To eliminate signal reflection from long-distance communication, RS-485 requires 2 terminal resistors, and their resistance values must be equal to the characteristic impedance(generally 120Ω) of the transmission cable. Meanwhile, the terminal resistor needs to be connected to the two ends of the transmission bus.

6.2 ModBus

The Modbus protocol is a master/slave architecture protocol. Except for the master node, all nodes that use Modbus protocol for communication are slave nodes. Each slave device has a unique RS-485 address, through which the device that executes a ModBus command can be recognized. When the master sends a command, all slave devices will receive it, but only the one with the same RS-485 address as in the command will execute and respond to it (address 0 is an exception, since the command to designate address 0 is a broadcast one, all devices that receive the command will run but not respond to it).
All Modbus commands include the check code used to ensure that the command sent by Master and received by Slave isn't damaged or lost. Basic Modbus commands can instruct an RTU to change a value in one of its registers, control or read an I/O port, and command the device to send back one or more data contained in its registers.

Modbus frame format: here we take the RS485 laser ranging sensor as an example.

Slave Address Function Code Register Address High-bit Register Address Low-bit Read Length High-bit Read Length Low-bit CRC High-bit CRC Low-bit
0x50 0x03 RegH RegL LenH LenL CRCH CRCL

Sensor address: 0x50 (default);
Read holding register: 0x03;
Address of register for measuring distance: 0x34;
Read length: 1 bit;
Holding register: The unit of the register is not a bit but two bytes, which means that it can store a specific amount of data that can be read and written. The distance data measured by the sensor is stored in this register.

7. Configuration

7.1 Install Library

Check the python version number using the command below before installing the relevant library.

pip -version

sudo pip install pyserial
sudo pip install modbus_tk
sudo pip3 install pyserial
sudo pip3 install modbus_tk

7.2 Enable UART

Open the Raspberry Pi terminal and enter the command below to access the configuration interface.

sudo raspi-config

Select Interfacing Options -> Serial Port, disable shell access and enable hardware serial port

Reboot Raspberry Pi:

sudo reboot

Find the location of the config.txt file

cd /boot

Open the /boot/config.txt file and find the statement below, if there is not, you can add it at the end of the file.

enable_uart=1

Then enter the command below to save and exit the file

:wq

For Raspberry Pi 3B users, the serial port is used for Bluetooth and needs to be commented out:

#dtoverlay=pi3-miniuart-bt

Reboot Raspberry Pi

sudo reboot

Make wire connections for the device according to the port instructions when Raspberry Pi is fully configured.

8. Program Demonstration

Hardware Requirements

Connection Diagram

8.1 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. Modify the program to adapt it for the RS485 laser ranging sensor used here according to the Modbus frame format above.

cd /home/pi/Desktop/DFRobot_CH432T_raspberrypi/examples/

8.2 Instructions for Change

1.

2.

3.

8.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_

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.

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFshopping_car1.png Get RS485 Expansion HAT for Raspberry Pi from DFRobot Store or DFRobot Distributor.

Turn to the Top