RS485 CAN HAT

From Waveshare Wiki
Jump to: navigation, search
RS485 CAN HAT
Raspberry Pi small size and low cost RS485 CAN expansion board
RS485 CAN HAT

RS485 CAN HAT for Raspberry Pi
RS485 CAN HAT (B)
Raspberry Pi Isolated RS485 CAN Expansion Board
RS485 CAN HAT (B).jpg
{{{name3}}}

{{{name4}}}

{{{name5}}}

Introduction

The RS485 CAN HAT will enables your Pi to communicate with other devices stably in long-distance via RS485/CAN functions.

Features

  • Standard Raspberry Pi 40pin GPIO header for Raspberry Pi series boards.
  • CAN function, onboard CAN controller MCP2515 via SPI interface, onboard transceiver SIT65HVD230DR.
  • RS485 function, controlled via UART, half-duplex communication, supports automatic TX/RX control without programming, onboard transceiver SP3485.
  • Onboard TVS (Transient Voltage Suppressor), effectively suppresses surge voltage and transient spike voltage in the circuit for RS485 transceiving, lightningproof & anti-electrostatic.
  • Reserved control header, allows working with other control boards.
  • Comes with online development resources and manual (examples in wiringPi/python).

Specification

  • Operating voltage: 3.3V
  • CAN chip: MCP2515
  • CAN transceiver: SN65HVD230
  • 485 transceiver: SP3485
  • Dimensions: 65mmx30mm
  • Mounting hole size: 3.0mm

Onboard Interface

  • CAN Bus:
Function PIN Raspberry Pi (BCM) Description
3V3 3V3 3.3V Power Input
GND GND Ground
SCK SCK SPI Clock Input
MOSI MOSI SPI Data Input
MISO MISO SPI Data Output
CS CE0 Data/Command Selection
INT 25 Interrupt Output
  • RS485 Bus:
Function PIN Raspberry Pi (BCM) Description
3V3 3V3 3.3V Power Input
GND GND Ground
RXD RXD UART Receives
TXD TXD UART Transmits
RSE 4 Tx/Rx Control

For the RSE pin, you can choose not to use it, the module factory defaults to use the hardware automatic receive and transmit.

Hardware Description

CAN Bus

The functionality of the CAN module involves handling all message reception and transmission on the CAN bus. When sending a message, it is first loaded into the appropriate message buffer and control registers. Sending operations can be initiated by setting the corresponding bits in the control registers via the SPI interface or by using the transmit enable pin. Communication status and errors can be checked by reading the respective registers.
Any messages detected on the CAN bus undergo error checking, and then are matched with user-defined filters to determine whether the message should be moved to one of the two receive buffers.
As the Raspberry Pi itself does not support the CAN bus, we can use the CAN controller with the SPI interface, along with a transceiver, to accomplish CAN functionality.
Microchip's MCP2515 is a CAN protocol controller that fully supports the CAN V2.0B technical specification. The device can send and receive both standard and extended data frames as well as remote frames. With two acceptance mask registers and six acceptance filter registers built in, the MCP2515 can filter out unwanted messages, reducing the overhead on the main microcontroller (MCU). The MCU connects to the device via the SPI interface. For Raspberry Pi, using this chip does not require writing drivers; instead, simply enabling the kernel driver in the device tree is sufficient for usage.
MCP2515-PIN.png
For more details, you can refer to the user manual.
SN65HVD230 is a 3.3V CAN transceiver manufactured by Texas Instruments. This device is suitable for serial communication on CAN buses with higher communication speeds, good noise immunity, and high reliability. SN65HVD230 offers three different operating modes: high-speed, slope, and standby. The mode control can be achieved through the Rs control pin. The output pin Tx of the CAN controller is connected to the data input pin D of SN65HVD230, enabling the transmission of data from this CAN node to the CAN network. Similarly, the receive pin Rx of the CAN controller is connected to the data output pin R of SN65HVD230 for receiving data.
RS485 CAN HAT (B)06.png

RS485 Bus

The SP3485 interface chip is an RS-485 driver chip used for low-power transceiver communication on RS-485 networks. It operates on a single +3.3V power supply and employs half-duplex communication. The RO and DI pins serve as the output of the receiver and the input of the driver, respectively. The ̅(RE) and DE pins act as the enable pins for receive and transmit operations; the device is in receive mode when ̅(RE) is logic low, and in transmit mode when DE is logic high. The A and B pins are the differential signal lines for receive and transmit operations. When A-B > +0.2V, RO outputs logic 1; when A-B < -0.2V, RO outputs logic 0. Matching resistors, typically 100Ω, are placed between the A and B pins.
RS485 CAN HAT (B)0006.png
Where the RE and DE pins of the SP3485 chip are set to receive and transmit;
The default factory setting of this module is to use hardware automatic sending and receiving, or you can choose to control the pins on the software to choose to send and receive, you can choose the control method by soldering the 0-ohm resistor on the board.
Hardware automatic control:
RS485 CAN HAT (B)07.png
Data Rx: When P_TX is high, it's in an idle state. At this point, the transistor conducts, and the RE pin of the SP3485 chip is at a low level, enabling data reception. RO begins to receive data and forwards the data received at the 485AB port to the MCU.
Data Tx: When P_TX is pulled low, indicating the start of data transmission, the transistor is cut off, and the DE pin is set to a high level, enabling data transmission. At this point, if the data being transmitted is '1', the transistor will be conducting. Although reception becomes valid, the chip remains in a high-impedance state during the transmission phase, so it continues to maintain the transmission state, allowing for the normal transmission of '1'.
Note: the use of an automatic transceiver due to the speed of the three-stage tube, will lead to the automatic transceiver baud rate that can not be too high, if you need a very high baud rate is recommended to use the manual transceiver.

Working with Raspberry Pi

Install Library

If you use the bookworm system, you can only use lgpio library, bcm2835 and wiringPi can't be installed and used.

BCM2835

#Open the Raspberry Pi terminal and run the following command
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
# For more, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/

WiringPi

#Open the Raspberry Pi terminal and run the following command
cd
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
# Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error

# Bullseye branch system using the following command:
git clone https://github.com/WiringPi/WiringPi
cd WiringPi
. /build
gpio -v
# Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error

lgpio

#Open the Raspberry Pi terminal and run the following command
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
sudo make install

# You can refer to the official website for more: https://github.com/gpiozero/lg
  • python
sudo apt-get update
sudo apt-get install python-serial
sudo pip install python-can

PI5

sudo apt-get update
sudo apt-get install python-serial
sudo apt-get install python-can

CAN Usage

This demo uses two Raspberry Pi and two RS485 CAN HATs.
Provide python and c demos.

Preparation

Insert the module into the Raspberry Pi, modify the start-up script "config.txt".

sudo nano /boot/config.txt

Add the following content at the file:

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000

Where "oscillator=12000000" is the onboard oscillator with the size of 12M as shown below:
RS485 CAN HAT cry12.png

  • If the purchase date is earlier than August 2019, please use the following:

As shown in the figure, the red box is the 8M crystal oscillator.
RS485 CAN HAT cry.png

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000

After saving and exiting, restart the Raspberry Pi:

sudo reboot

After rebooting, run the command to see if the initialization was successful:

dmesg | grep -i '\(can\|spi\)'

RS485 CAN HAT CAN1.png
If the module is not connected the following may be prompted:
RS485 CAN HAT CAN2.png
Please check if the module is connected. Whether to enable SPI and turn on the MCP2515 kernel driver. Whether reboot is performed.
Make sure that both sides of the Raspberry Pi are handled this way, and connect the H's and L's of the two modules correspondingly.
If you are using another CAN device, make sure that the lines H-H, and L-L are connected.

  • Open CAN:
sudo ip link set can0 up type can bitrate 1000000
sudo ifconfig can0 txqueuelen 65536
  • For more CAN kernel commands, you can refer to:
https://www.kernel.org/doc/Documentation/networking/can.txt
  • View ifconfig:
ifconfig

RS485 CAN HAT cry16.png

Loopback Mode Testing

  • Install can-utils:
sudo apt-get install can-utils
  • Open can0 and loopback mode:
sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000 loopback on
sudo ip link set can0 up type can bitrate 1000000
  • Open two terminal windows, test can0 self-loopback:
candump can0
cansend can0  000#11.22.33.44

Can loopback.png

Demo Download

Run the following content in the Raspberry Pi terminal:

sudo apt-get install unzip
wget https://files.waveshare.com/upload/4/4e/RS485_CAN_HAT_Code.zip
unzip RS485_CAN_HAT_Code.zip
sudo chmod 777 -R RS485_CAN_HAT_Code/

C

  • Blocking receive. Open the terminal on the Raspberry Pi and run:
cd RS485_CAN_HAT_Code/CAN/wiringPi/receive/
make clean
make
sudo ./can_receive

The receiving demo is blocked, and it ends as soon as the data is read.
RS485 CAN HAT B Rasp10.png

  • Send, and the Raspberry Pi opens the terminal and runs:
cd RS485_CAN_HAT_Code/CAN/wiringPi/send/
make clean
make
sudo ./can_send

RS485 CAN HAT B Rasp11.png
At this point, receive the message that receives the corresponding id:
RS485 CAN HAT B Rasp12.png

python

Raspberry Pi opens the terminal and runs:

cd RS485_CAN_HAT_Code/CAN/python/
#Run reception first:
sudo python reveive.py
#The sending terminal:
sudo python send.py

How to Use It with Other CAN devices

1. Make sure the hardware wiring is correct, i.e., H-H, and L-L connections.
2. Ensure that the baud rate settings are the same on both sides, the default routine sets the baud rate to 100K.
RS485 CAN HAT B Rasp13.png
3. Ensure that the CAN IDs on both sides are the same, otherwise it will not be possible to receive.
RS485 CAN HAT B Rasp14.png
4. If there is frame loss when sending data for a long time, you can try to reduce the baud rate to solve the problem.

RS485 Usage

This demo uses two Raspberry Pi and two RS485 CAN HAT modules.
Provide python and wiringPi demos.

Enable UART

Execute the following command to enter the Raspberry Pi configuration:

sudo raspi-config

Choose Interfacing Options -> Serial -> No -> Yes:
You need to disable the login shell and enable the srial port hardware:

L76X GPS Module rpi serial.png

Reboot Raspberry Pi:

sudo reboot

Open the /boot/config.txt file and find the following configuration statement to enable the serial port, if not, add it at the end of the file:

enable_uart=1

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

#dtoverlay=pi3-miniuart-bt

Raspberry Pi 5/2B/zero and some of the changed configurations of the Raspberry Pi, the user's serial device number may be ttyAMA0; you can use the following command line to confirm, serial0 for the selection of the serial device number, as follows:

ls -l /dev/serial*

RM520n-gl faq90.png
And then reboot the Raspberry Pi:

sudo reboot

Make sure that both sides of the Raspberry Pi are handled this way, connecting the two modules A and B correspondingly.
If using other 485 devices, make sure to connect A-A and B-B.

C

  • Blocking reception, the Raspberry Pi opens a terminal and runs:
cd RS485_CAN_HAT_Code/485/WiringPi/receive
make clean
make
sudo ./485_receive

The receiving program is blocking until the data is read and then it ends.
RS485 CAN HAT B RS011.png

  • Send, the Raspberry Pi opens the terminal and runs:
cd RS485_CAN_HAT_Code/485/WiringPi/send
make clean
make
sudo ./485_send

At this point the receiver receives the demo:
RS485 CAN HAT B RS012.png

python Demo

cd RS485_CAN_HAT_Code/485/python/
#Run reception first:
sudo python receive.py
#Sending terminal:
sudo python send.py

Troubleshooting

If the 485 communication is abnormal, please try the following steps:

  1. Verify that logging into the Raspberry Pi shell from serial is disabled;
  2. Determine the hardware version of the Raspberry Pi, if it is a Raspberry Pi ZERO/3B, the serial port in the program may need to be modified to /dev/ttyAMA0;
  3. Determine whether 485's A,B correspond one-to-one with the controlled 485 devices A,B;
  4. You can start by using a USB to 485 device to communicate with the RS485 CAN HAT to ensure that the Raspberry Pi is set up correctly;

X3-Pi Usage

Preparation

Using the commands:

sudo srpi-config

Open the hardware control interface interface, open the serial port, with SPI;
RS485 CAN HAT x3-0.jpg

Download Installation Package

Update the package list:

sudo apt update

Upgrade all installed packages to the latest version:

sudo apt full-upgrade

Install WiringPi:

sudo apt update
sudo apt install git-core
git clone https://gitee.com/study-dp/WiringPi.git
cd WiringPi
./build.sh

Demo Download

wget https://files.waveshare.com/upload/4/4e/RS485_CAN_HAT_X3.zip
unzip -o RS485_CAN_HAT_X3.zip -d RS485_CAN_HAT_X3

RS485

c

Enter the directory:

cd ~/RS485_CAN_HAT_X3/rs485/c/

Receiving example:

cd receive/
make
./rs485_receive

Sending example:

cd send/
make
./rs485_send

python

Enter the directory:

cd ~/RS485_CAN_HAT_X3/rs485/python/

Receiving example:

python3 receive.py

Sending example:

python3 send.py

CAN

Enter the directory:

cd ~/RS485_CAN_HAT_X3/CAN/c/

Receiving example:

cd receive/
make
./receive

Sending example:

cd send/
make
./send

python

Enter the directory:

cd ~/RS485_CAN_HAT_X3/CAN/python/

Receiving example:

python3 receive.py

Sending example:

python3 send.py

Resources

Documents

Demo code

Datasheet

3D Drawing

FAQ


 Answer:
  • The current version is 12M, you can view the front of the module:

RS485 CAN HAT cry12.png
Then the corresponding command in config.txt is:

dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
  • If you have an old version, there should be an 8M crystal oscillator, as shown below:

RS485 CAN HAT cry.png
Then the corresponding command in config.txt is:

dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000


 Answer:

Open the Raspberry Pi terminal and enter the following command to access the configuration screen:

sudo raspi-config
Select Interfacing Options -> Serial to turn off shell access and turn on the hardware serial port

L76X GPS Module rpi serial.png
Raspberry Pi 2B/zero, the user serial device number is ttyAMA0; you can use the following command line to confirm that serial0 is the selected serial device number, as follows:

ls -l /dev/serial*

RM520n-gl faq90.png

  • For Raspberry Pi 3B/2B/Zero, the serial port is typically used for Bluetooth. When you comment out this setting, it defaults to S0. However, Raspberry Pi 4 does not have this statement by default.
#dtoverlay=pi3-miniuart-bt

And then reboot the Raspberry Pi:

sudo reboot

{{{5}}}


 Answer:
  1. Make sure the baud rates on both sides are the same;
  2. The fixed frame ID is set in the demo: 0X123, please set the sending and receiving CAN ID of the other end of your CAN to be x0123;


 Answer:
This is an entry-level 485 and CAN, both without isolation.


 Answer:
  1. Determine the hardware version of the Raspberry Pi, if it is the Raspberry Pi ZERO/3B, the serial port in the program needs to be modified to /dev/ttyAMA0;
  2. Check whether the serial communication of the Raspberry Pi has enabled flow control;
  3. Determine whether A and B of 485 correspond to the controlled 485 devices A and B one by one;
  4. You can use the USB to 485 devices to communicate with the RS485 CAN HAT first to ensure that there is no problem with the settings of the Raspberry Pi;
  5. Check the setting of odd and even bit parity of serial communication parameters.


 Answer:
  1. Mainstream Ubuntu system config.txt files are usually in the /boot/firmware folder
  2. Or use the SD card of the Raspberry Pi to read and change the config.txt file under the computer (or other hosts that can recognize the SD card) through the card reader.


 Answer:

The sensor may send hex data (sometimes it is necessary to send hex data to the sensor to request data), follow the steps below to send and receive hex data:

wget https://files.waveshare.com/upload/0/00/RS485-CAN-HAT-For-Hex.zip
unzip RS485-CAN-HAT-For-Hex.zip
sudo chmod 777 RS485-CAN-HAT-For-Hex.zip
cd RS485-CAN-HAT-For-Hex
#Receive Hex
sudo python3 RS485-CAN-HAT-send-hex.py
#Send Hex
sudo python3 RS485-CAN-HAT-receive-hex\.py


 Answer:

If it means that the output part of data processing becomes slower, it should help to improve the performance of the upper computer; you can also consider clearing the cache before each read to read only the latest data for processing.

{{{5}}}


 Answer:

Can be used without connecting to the GND, connected to the GND signal may be more stable, connected to GND to obtain better performance, reliability, and anti-interference ability, and long-distance communication. Hence, it is recommended to connect the GND.

{{{5}}}


 Answer:

After entering "myenv", you can install the corresponding Python library again. In the "myenv" environment, all commands are executed without "sudo".

sudo apt install python3-venv
python3 -m venv myenv &&source myenv/bin/activate
source myenv/bin/activate
pip install requests

{{{5}}}


 Answer:
  • The CAN rate of the host and the slave should be the same.
  • Both ends of the 120 ohm balancing resistors are turned on.
  • The frame ID of the transmitter should correspond to the filter ID of the receiver.

{{{5}}}


 Answer:
  • The CAN rates of the upper and lower units should be the same.
  • Both ends of the 120-ohm balancing resistors are turned on.
  • The frame ID of the sender should correspond to the filter ID of the receiver.

{{{5}}}


Support



Technical Support

If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 AM GMT+8 (Monday to Friday)