RS485 CAN HAT
| ||
| ||
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: 65mm x 30mm
- 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 realize 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, that is, the Raspberry PI connects to the chip through 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.
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 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.
Among them, 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:
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 on-off speed of the transistor, will lead to the automatic transceiver baud rate that can not be too high, if you need a very high baud rate, it is recommended to use the manual transceiver.
Working with Raspberry Pi
Install Library
If you use bookworm system, only the lgpio library is available, bcm2835 and wiringPi libarary cannot be installed or used. Please note that the python library does not need to install, you can directly run the demo.
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
wget https://github.com/joan2937/lg/archive/master.zip unzip master.zip cd lg-master sudo make install #for more details, you can refer to https://github.com/gpiozero/lg
Python3
sudo apt-get update sudo apt-get install python3-serial sudo apt-get install python3-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 sudo nano /boot/firmware config.txt #Bookworm/Ubuntu
Add the following content at the file:
dtparam=spi=on dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
"oscillator=12000000" is the onboard oscillator with the size of 12M as shown below:
- 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.
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\)'
If the module is not connected the following may be prompted:
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 and L 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 sudo ifconfig can0 up
- For more CAN kernel commands, you can refer to:
https://www.kernel.org/doc/Documentation/networking/can.txt
- View ifconfig:
ifconfig
Simple Test
- This demonstration uses a Raspberry Pi, an RS485 CAN HAT (B) module and a USB-CAN-A module. Python and C language programs are provided:
- Open CAN:
sudo ip link set can0 up type can bitrate 1000000 sudo ifconfig can0 txqueuelen 65536
- For more CAN kernel commands, please refer to:
https://www.kernel.org/doc/Documentation/networking/can.txt
- Check ifconfig:
ifconfig
- Install can-utils:
sudo apt-get install can-utils
- Receive:
The command for inputting the receiving data in the terminal:
candump can0
The receiving tool is blocked. When running the tool without parameters, it will always be in the receiving state. Use Ctrl+C to exit.
A description of the parameters of the tool can be used:
candump -h
- Sending
Input the command for sending the data in the terminal:
cansend can0 000#11.22.33.44
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
- Block receiving: 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.
- Send: the Raspberry Pi opens the terminal and runs:
cd RS485_CAN_HAT_Code/CAN/wiringPi/send/ make clean make sudo ./can_send
At this point, receive the message corresponding to id:
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. Ensure that 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 demo sets the baud rate to 100K.
3. Ensure that the CAN IDs on both sides are the same, otherwise it will not be possible to receive.
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 interface
Open the Raspberry Pi terminal and enter the following command to enter the configuration interface
sudo raspi-config Select Interfacing Options -> Serial, disable shell access, and enable the hardware serial port
For Raspberry Pi 3B users, the serial port is used for Bluetooth and needs to be commented out:
#dtoverlay=pi3-miniuart-bt
For Raspberry Pi 5/2B/zero and some Raspberry Pi that have been changed in configuration, 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*
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 demo is blocked until the data is read and then it ends.
- 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:
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:
- Verify that logging into the Raspberry Pi shell from serial is disabled;
- 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;
- Determine whether 485's A,B correspond one-to-one with the controlled 485 devices A, B;
- You can first use a USB to 485 device to communicate with the RS485 CAN HAT to ensure that the Raspberry Pi is set up correctly.
Resources
Documents
Demo code
Datasheet
3D Drawing
FAQ
- The current version is 12M, you can view the front of the module:
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:
Then the corresponding command in config.txt is:
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000
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
- Raspberry Pi 5/3B/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*
- 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}}}
- Make sure the baud rates on both sides are the same;
- 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;
- This is an entry-level 485 and CAN, both without isolation.
- 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;
- Check whether the serial communication of the Raspberry Pi has enabled flow control;
- Determine whether A and B of 485 correspond to the controlled 485 devices A and B one by one;
- 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;
- Check the setting of odd and even bit parity of serial communication parameters.
- 1. In Mainstream Ubuntu system, the config.txt files are usually in the /boot/firmware folder:
sudo nano /boot/firmware config.txt
Add the following content at the end:
dtparam=spi=on dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000 enable uart=1
- 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.
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
If it means that the output part of data processing becomes slower, it should help to improve the performance of the host computer; you can also consider clearing the cache before each read to read only the latest data for processing.
{{{5}}}
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. For long-distance communication. Hence, it is recommended to connect the GND.
{{{5}}}
After entering "myenv", install the corresponding Python library. 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}}}
- 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}}}
The RS485 CAN HAT is an adapter only.
For Modbus transmit, you can only transmit modbus command directly.
{{{5}}}
- Download 64-bit Arm wiringpi
wget https://gitee.com/LJYSCIENTIST/raspberry-pi-software-storage/raw/master/wiringpi-2.60-1_arm64.deb
- Install 64-bit Arm wiringpi
sudo dpkg -i wiringpi-2.60-1_arm64.deb
{{{5}}}
- It is possible to verify the functionality with loopback mode:
- Install can-utils.
sudo apt-get install can-utils
- Open can0 and set 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 the self-transmission of can0:
candump can0 cansend can0 000#11.22.33.44
{{{5}}}
Yes, it supports.
{{{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 PM GMT+8 (Monday to Friday)