Difference between revisions of "Template:2-CH CAN HAT User Manual"
(44 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
==Introduction== | ==Introduction== | ||
− | This is a 2-Channel CAN bus expansion HAT designed for Raspberry Pi, supports CAN2.0, features multi onboard protection circuits, high anti-interference capability, and stable operation. It suits | + | This is a 2-Channel CAN bus expansion HAT designed for Raspberry Pi, supports CAN2.0, features multi-onboard protection circuits, high anti-interference capability, and stable operation. It suits fields such as automotive devices or industrial automation.<br /> |
− | + | ==Features== | |
− | *Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards | + | *Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards. |
− | *Adopts MCP2515 and SN65HVD230 dual chips combined solution, | + | *Adopts MCP2515 and SI65HVD230(or SN65HVD230) dual chips combined solution, allowing 2-channel CAN communication. |
− | *Integrated power isolation, providing stable isolated voltage, requires no extra power supply for the isolated terminal | + | *Integrated power isolation, providing stable isolated voltage, requires no extra power supply for the isolated terminal. |
− | *Onboard digital isolator, signal isolation makes | + | *Onboard digital isolator, signal isolation makes communication safer, more stable, and better anti-interference. |
− | *Onboard TVS (Transient Voltage Suppressor) diode, provides ESD protection and transient peak voltage protection | + | *Onboard TVS (Transient Voltage Suppressor) diode, provides ESD protection and transient peak voltage protection. |
− | *Onboard voltage translator, select 3.3V/5V operating voltage by jumper | + | *Onboard voltage translator, select 3.3V/5V operating voltage by jumper. |
− | *Onboard 120Ω terminal resistor, configured by jumper | + | *Onboard 120Ω terminal resistor, configured by jumper. |
− | *Breakout SPI control pins, for connecting with host control boards like STM32/Arduino | + | *Breakout SPI control pins, for connecting with host control boards like STM32/Arduino. |
− | *Comes with development resources and manual | + | *Comes with online development resources and a manual. |
− | + | ==Specifications== | |
*CAN controller: MCP2515 | *CAN controller: MCP2515 | ||
− | *CAN transceiver: SN65HVD230 | + | *CAN transceiver: SI65HVD230 (or SN65HVD230) |
*Communication interface: SPI | *Communication interface: SPI | ||
*Operating voltage: 5V | *Operating voltage: 5V | ||
*Logic level: 3.3V/5V | *Logic level: 3.3V/5V | ||
*Dimensions: 65.0 x 56.5 mm | *Dimensions: 65.0 x 56.5 mm | ||
+ | *Mounting hole diameter: 3.0mm | ||
==Interfaces== | ==Interfaces== | ||
===CAN bus=== | ===CAN bus=== | ||
{|border=1; style="width:900px;" | {|border=1; style="width:900px;" | ||
− | |-style="background: | + | |-style="background:green; color:#fff;" align="center" |
|'''PIN'''||'''Raspberry Pi'''<br />(BCM)||'''Raspberry Pi'''<br />(WPI)||'''Description''' | |'''PIN'''||'''Raspberry Pi'''<br />(BCM)||'''Raspberry Pi'''<br />(WPI)||'''Description''' | ||
|-rowspan=2 align="center" | |-rowspan=2 align="center" | ||
Line 31: | Line 32: | ||
|GND||GND||GND||Ground | |GND||GND||GND||Ground | ||
|-align="center" | |-align="center" | ||
− | |MISO ||9(MISO) ||13(MISO)||SPI clock input | + | |MISO ||9 (MISO) ||13 (MISO)||SPI clock input |
|-align="center" | |-align="center" | ||
− | |MOSI||10(MOSI)||12(MOSI) ||SPI data input | + | |MOSI||10 (MOSI)||12 (MOSI) ||SPI data input |
|-align="center" | |-align="center" | ||
− | |SCK||11(SCLK)||14(SCLK)||SPI data output | + | |SCK||11 (SCLK)||14(SCLK)||SPI data output |
|-align="center" | |-align="center" | ||
− | |CS_0||8(CE0)||10(CE0)||CAN_0 chip select | + | |CS_0||8 (CE0)||10 (CE0)||CAN_0 chip select |
|-align="center" | |-align="center" | ||
− | |INT_0|| | + | |INT_0||23 (Default)/22||6 (Default)/5||CAN_0 interrupt output |
|-align="center" | |-align="center" | ||
− | |CS_1||7(CE1)||11(CE1)||CAN_1 chip select | + | |CS_1||7 (CE1)||11 (CE1)||CAN_1 chip select |
|-align="center" | |-align="center" | ||
− | |INT_1|| | + | |INT_1||25 (Default)/24||4 (Default)/3||CAN_1 interrupt output |
|} | |} | ||
+ | <br /> | ||
+ | '''NOTE:''' | ||
+ | INT_0 is soldered by PIN23 by default, and INT_1 is soldered by PIN25 by default. | ||
+ | If you need to modify the pins, you need to modify the soldering pad of the PCBA board, and also modify the setting of /boot/config.txt. | ||
+ | Take the modification of INT_0 as an example: if PIN23 is changed to PIN22, then the soldering pad of the red frame on the left board in the figure below needs to be modified to the soldering pad of the board on the right in the figure below: | ||
+ | [[File:2-CH-CAN-HAT-int-setting.png|500px]] | ||
+ | Similarly, if you need to change INT_1 from the default PIN25 to PIN24, you need to solder the 0 ohm at the arrow of PIN25 to PIN24. | ||
− | |||
===CAN bus=== | ===CAN bus=== | ||
− | CAN module could process packets | + | CAN module could process packets transmitted/received on the CAN bus. Packets transmit the first store packet to the related buffer and control register. |
− | Use SPI interface to set the bits on control register or enable transmit pin for transmitting. | + | Use the SPI interface to set the bits on the control register or enable the transmit pin for transmitting. |
− | Registers could be read | + | Registers could be read to detect communication states and errors. It will first check if there are any errors of packets detected on the CAN bus, |
− | then verify it with filter | + | then verify it with a filter that is defined by the user. And store the packet in one of the buffers if it has no errors.<br /> |
<font color="#FF0000"> | <font color="#FF0000"> | ||
− | Raspberry Pi cannot support SPI bus, so this module | + | Raspberry Pi cannot support SPI bus, so this module uses an SPI interface and on |
board a receiver/transmitter for CAN communication.<br /> | board a receiver/transmitter for CAN communication.<br /> | ||
</font> | </font> | ||
− | Microchip | + | Microchip Technology's MCP2515 is a stand-alone Controller Area Network (CAN) controller that implements the CAN specification, version 2.0B. |
It is capable of transmitting and receiving both standard and extended data and remote frames. | It is capable of transmitting and receiving both standard and extended data and remote frames. | ||
The MCP2515 has two acceptance masks and six acceptance filters that are used to filter out unwanted messages, | The MCP2515 has two acceptance masks and six acceptance filters that are used to filter out unwanted messages, | ||
− | thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via | + | thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via an industry-standard Serial Peripheral Interface (SPI), |
− | that is Raspberry Pi can communicate with MCP2515 via SPI interface without external driver. What we need | + | that is Raspberry Pi can communicate with MCP2515 via SPI interface without an external driver. What we need to do is to enable the kernel driver on the devices tree. |
− | For more details, please refer to datasheet.<br /> | + | For more details, please refer to the datasheet.<br /> |
[[File:MCP2515-PIN.png|400px]]<br /> | [[File:MCP2515-PIN.png|400px]]<br /> | ||
− | SN65HVD230 from TEXAS INSTRUMENTS is a CAN transceiver, which is designed for high communication frequency, anti-jamming and high reliability CAN bus communication. | + | |
− | SN65HVD230 | + | <font color="red">''' Note:''' Since October 2021, due to the lack of stock of the SN65HVD230 chip and the price increase, the chip has been replaced with SI65HVD230, and the software and hardware functions are compatible.</font> |
− | Connect the Tx of CAN controller to | + | |
− | And connect the RX of CAN controller to | + | SN65HVD230 from TEXAS INSTRUMENTS is a CAN transceiver, which is designed for high communication frequency, anti-jamming, and high-reliability CAN bus communication. |
+ | SN65HVD230 provides three different modes of operation: high-speed, slope control, and low-power modes. The operation mode can be controlled by Rs pin. | ||
+ | Connect the Tx of the CAN controller to SN65HVD230's data input pin D, which can transmit the data of the CAN node to the CAN network; | ||
+ | And connect the RX of the CAN controller to SN65HVD230's data input pin R to receive data.<br /> | ||
[[File:SN65HVD230-PIN.jpg|1000px]] | [[File:SN65HVD230-PIN.jpg|1000px]] | ||
Line 72: | Line 82: | ||
The working voltage level of Raspberry Pi is 3.3V, therefore we need to set the VIO of 2-CH CAN HAT to 3.3V as below:<br /> | The working voltage level of Raspberry Pi is 3.3V, therefore we need to set the VIO of 2-CH CAN HAT to 3.3V as below:<br /> | ||
[[File:2-CH-CAN-HAT-3V3.jpg|600px]]<br /> | [[File:2-CH-CAN-HAT-3V3.jpg|600px]]<br /> | ||
+ | <font color="#FF0000"> | ||
+ | Note: When connecting to the Raspberry Pi 2b, 3b, and 4b boards, please connect the booster and nylon post to fix it to avoid the back of the CAN terminal touching the HDMI interface causing a short circuit, and avoid wrong connection or poor contact:<br /></font> | ||
+ | [[File:2-CH CAN HAT 010.png]]<br/> | ||
+ | When connecting to the Raspberry Pi, you must add a booster seat, and then pass the needle through the bottom plate, the effect is as follows:<br /> | ||
+ | [[File:2-CH CAN HAT 020.png]]<br/> | ||
+ | |||
+ | ==Enable SPI interface== | ||
+ | *Open the terminal, and use the command to enter the configuration page.<br /> | ||
+ | <pre> | ||
+ | sudo raspi-config | ||
+ | Choose Interfacing Options -> SPI -> Yes to enable the SPI interface. | ||
+ | </pre> | ||
+ | [[file:RPI_open_spi.png|900px]]<br /> | ||
+ | Reboot Raspberry Pi: <br /> | ||
+ | <pre> | ||
+ | sudo reboot | ||
+ | </pre> | ||
+ | Please make sure that the SPI interface was not used by other devices, you can check in the /boot/config.txt.<br /> | ||
==Install libraries== | ==Install libraries== | ||
*'''bcm2835''' | *'''bcm2835''' | ||
− | Open terminal and run commands below to install bcm2835 library | + | Open the terminal and run the commands below to install the bcm2835 library: |
<pre> | <pre> | ||
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz | wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz | ||
Line 85: | Line 113: | ||
sudo make check | sudo make check | ||
sudo make install | sudo make install | ||
− | # For | + | # For More: http://www.airspayce.com/mikem/bcm2835/ |
</pre> | </pre> | ||
− | + | ===wiringPi=== | |
+ | ====For 32-bit Raspberry Pi system==== | ||
<pre> | <pre> | ||
+ | #Open the Raspberry Pi terminal and run the following command | ||
+ | cd | ||
sudo apt-get install wiringpi | 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 | wget https://project-downloads.drogon.net/wiringpi-latest.deb | ||
sudo dpkg -i wiringpi-latest.deb | sudo dpkg -i wiringpi-latest.deb | ||
gpio -v | gpio -v | ||
− | # Run the command | + | # 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 | ||
+ | sudo gpio -v | ||
+ | # Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error | ||
</pre> | </pre> | ||
+ | ====For 64-bit Raspberry Pi System==== | ||
+ | Copy the resource package to the Raspberry Pi using the command: | ||
+ | wget https://files.waveshare.com/upload/8/8c/WiringPi-master.zip | ||
+ | (optional, you can skip this step if you have used the unzip command) Install the unzip environment: | ||
+ | sudo apt-get install unzip | ||
+ | Go to the file location and execute the unzip command: | ||
+ | unzip WiringPi-master.zip | ||
+ | Go to the file directory (go to the "WiringPi-master" folder): | ||
+ | cd WiringPi-master/ | ||
+ | Run sudo . /build: | ||
+ | sudo ./build | ||
+ | (optional, see point 4 for errors) If . /build does not work, execute "chmod +x . /build" and then "sudo . /build": | ||
+ | chmod +x ./build | ||
+ | Example:<br/> | ||
+ | [[File:TF-Luna LiDAR Range SensorRas 05.png]]<br/> | ||
+ | [[File: TF-Luna LiDAR Range SensorRas 07.png]]<br/> | ||
+ | |||
*'''Python''' | *'''Python''' | ||
− | Install python library | + | Install python library: |
<pre> | <pre> | ||
#python2 | #python2 | ||
Line 117: | Line 172: | ||
sudo pip3 install python-can | sudo pip3 install python-can | ||
</pre> | </pre> | ||
+ | *WiringPi is no longer in the Debian package manager. Here are alternative instructions to install WiringPi: | ||
+ | https://forums.raspberrypi.com/viewtopic.php?t=350016#p2097654 | ||
− | + | ===Preparation=== | |
− | === | + | Insert the module into Raspberry Pi, and then modify the config.txt file:<br /> |
− | Insert the module | ||
<pre> | <pre> | ||
sudo nano /boot/config.txt | sudo nano /boot/config.txt | ||
</pre> | </pre> | ||
− | Append these statements to the | + | {{Note|The above configuration is based on the '''Raspbian OS''' system. If it is used in the '''Ubuntu''' system, the config.txt path is generally "''/boot/firmware/config.txt'' "|warn}} |
+ | Append these statements to the file: | ||
<pre> | <pre> | ||
dtparam=spi=on | dtparam=spi=on | ||
+ | dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=25 | ||
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=23 | dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=23 | ||
− | dtoverlay= | + | dtoverlay=spi-bcm2835-overlay |
</pre> | </pre> | ||
− | *Save and exit, then restart your | + | *Save and exit, then restart your Pi: |
<pre> | <pre> | ||
sudo reboot | sudo reboot | ||
</pre> | </pre> | ||
− | *After restart, check if initialize | + | *After restart, check if initialize successfully:<br /> |
<pre> | <pre> | ||
dmesg | grep spi | dmesg | grep spi | ||
</pre> | </pre> | ||
[[File:2-CH-CAN-HAT-SPI-Init.png|700px]] | [[File:2-CH-CAN-HAT-SPI-Init.png|700px]] | ||
− | *Set up | + | *Set up CAN:<br /> |
<pre> | <pre> | ||
sudo ip link set can0 up type can bitrate 1000000 | sudo ip link set can0 up type can bitrate 1000000 | ||
Line 147: | Line 205: | ||
sudo ifconfig can1 txqueuelen 65536 | sudo ifconfig can1 txqueuelen 65536 | ||
</pre> | </pre> | ||
− | *For more information about CAN kernel instructions, please | + | *For more information about CAN kernel instructions, please check:<br /> |
https://www.kernel.org/doc/Documentation/networking/can.txt | https://www.kernel.org/doc/Documentation/networking/can.txt | ||
− | *Run "ifconfig" | + | *Run "ifconfig":<br /> |
<pre> | <pre> | ||
ifconfig | ifconfig | ||
</pre> | </pre> | ||
[[File:2-CH-CAN-HAT-ifconfig.jpg|700px]] | [[File:2-CH-CAN-HAT-ifconfig.jpg|700px]] | ||
+ | *Turn off CAN device: | ||
+ | <pre> | ||
+ | sudo ifconfig can0 down | ||
+ | sudo ifconfig can1 down | ||
+ | </pre> | ||
===Testing=== | ===Testing=== | ||
Line 161: | Line 224: | ||
*install can-utils: | *install can-utils: | ||
sudo apt-get install can-utils | sudo apt-get install can-utils | ||
− | *Open two terminal | + | *Open two terminal windows: |
− | One terminal input receives CAN0 data | + | One terminal input receives the CAN0 data command: |
<pre> | <pre> | ||
candump can0 | candump can0 | ||
</pre> | </pre> | ||
− | Another terminal input sends CAN1 data | + | Another terminal input sends the CAN1 data command: |
<pre> | <pre> | ||
cansend can1 000#11.22.33.44 | cansend can1 000#11.22.33.44 | ||
</pre> | </pre> | ||
− | *The demonstration effect is as follows: (receiving on the left, sending on the right) | + | *The demonstration effect is as follows: (receiving on the left, sending on the right.) |
[[File:2-CH-CAN-HAT-cansend.png|800px]]<br /> | [[File:2-CH-CAN-HAT-cansend.png|800px]]<br /> | ||
If you have two 2-CH CAN HAT on hand, you can directly connect CAN_H and CAN_L two by two. The effect is the same as the above, pay attention to match the communication rate, identify the ID, and output the interface serial number.<br /> | If you have two 2-CH CAN HAT on hand, you can directly connect CAN_H and CAN_L two by two. The effect is the same as the above, pay attention to match the communication rate, identify the ID, and output the interface serial number.<br /> | ||
==Python example== | ==Python example== | ||
− | *Enter the | + | *Enter the directory of the Python example: |
− | *Run the receiver.py script in receiver | + | *Run the receiver.py script in the receiver terminal: |
<pre> | <pre> | ||
sudo python reveive.py | sudo python reveive.py | ||
</pre> | </pre> | ||
− | *Run the send.py script in sender | + | *Run the send.py script in the sender terminal: |
<pre> | <pre> | ||
sudo python send.py | sudo python send.py | ||
</pre> | </pre> | ||
− | ''' | + | '''【Note】The CAN1 is used as sender, and CAN0 is used as receiver.''' |
===Codes analysis=== | ===Codes analysis=== | ||
====【Python example】==== | ====【Python example】==== | ||
− | The demo codes provided | + | The demo codes provided are based on Python, please check that you installed the python-can library.<br /> |
− | Before you send data, you should create a CAN device | + | Before you send data, you should create a CAN device first: |
<pre> | <pre> | ||
os.system('sudo ip link set can0 type can bitrate 100000') | os.system('sudo ip link set can0 type can bitrate 100000') | ||
os.system('sudo ifconfig can0 up') | os.system('sudo ifconfig can0 up') | ||
</pre> | </pre> | ||
− | This code is used to initialize CAN0 as receiver/sender. If you want to change it to CNA1, you can use this one:<br /> | + | This code is used to initialize CAN0 as the receiver/sender. If you want to change it to CNA1, you can use this one:<br /> |
<pre> | <pre> | ||
os.system('sudo ip link set can1 type can bitrate 100000') | os.system('sudo ip link set can1 type can bitrate 100000') | ||
os.system('sudo ifconfig can1 up') | os.system('sudo ifconfig can1 up') | ||
</pre> | </pre> | ||
− | *Connect to CAN BUS<br /> | + | *Connect to CAN BUS:<br /> |
<pre> | <pre> | ||
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes') | can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes') | ||
</pre> | </pre> | ||
− | *or<br /> | + | {{Note|As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise an error will be reported. |
+ | <source lang="text"> | ||
+ | can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan') | ||
+ | </source > | ||
+ | |warn}} | ||
+ | *or:<br /> | ||
<pre> | <pre> | ||
can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan_ctypes') | can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan_ctypes') | ||
</pre> | </pre> | ||
− | *Create message | + | *Create message: |
− | |||
<pre> | <pre> | ||
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False) | msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False) | ||
</pre> | </pre> | ||
− | *Send message | + | {{Note|As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise an error will be reported. |
+ | <source lang="text"> | ||
+ | msg = can.Message(is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7]) | ||
+ | </source > | ||
+ | |warn}} | ||
+ | *Send message: | ||
<pre> | <pre> | ||
can0.send(msg) | can0.send(msg) | ||
</pre> | </pre> | ||
− | * | + | *or:<br /> |
<pre> | <pre> | ||
can1.send(msg) | can1.send(msg) | ||
</pre> | </pre> | ||
− | *Finally, close CAN device | + | *Finally, close the CAN device: |
<pre> | <pre> | ||
os.system('sudo ifconfig can0 down') | os.system('sudo ifconfig can0 down') | ||
</pre> | </pre> | ||
− | * | + | *or: |
<pre> | <pre> | ||
os.system('sudo ifconfig can1 down') | os.system('sudo ifconfig can1 down') | ||
</pre> | </pre> | ||
− | *Receive | + | *Receive message: |
<pre> | <pre> | ||
msg = can0.recv(10.0) | msg = can0.recv(10.0) | ||
</pre> | </pre> | ||
− | The | + | The variable of recv() function is the timeout of receiving.<br /> |
− | '''For more information, please refer | + | '''For more information, please refer to: https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html<br />''' |
− | |||
====【WringPi example】==== | ====【WringPi example】==== | ||
− | *Blocking the reception, the Raspberry Pi opens the terminal and runs: | + | *Blocking the reception, the Raspberry Pi opens the terminal and runs: |
<pre> | <pre> | ||
cd 2-CH_CAN_HAT_Code/wiringPi/receive/ | cd 2-CH_CAN_HAT_Code/wiringPi/receive/ | ||
Line 243: | Line 314: | ||
sudo ./can_receive | sudo ./can_receive | ||
</pre> | </pre> | ||
− | *Raspberry Pi opens the terminal and | + | *Raspberry Pi opens the terminal and runs: |
<pre> | <pre> | ||
cd 2-CH_CAN_HAT_Code/ wiringPi/receive/ | cd 2-CH_CAN_HAT_Code/ wiringPi/receive/ |
Latest revision as of 07:18, 16 April 2024
Introduction
This is a 2-Channel CAN bus expansion HAT designed for Raspberry Pi, supports CAN2.0, features multi-onboard protection circuits, high anti-interference capability, and stable operation. It suits fields such as automotive devices or industrial automation.
Features
- Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards.
- Adopts MCP2515 and SI65HVD230(or SN65HVD230) dual chips combined solution, allowing 2-channel CAN communication.
- Integrated power isolation, providing stable isolated voltage, requires no extra power supply for the isolated terminal.
- Onboard digital isolator, signal isolation makes communication safer, more stable, and better anti-interference.
- Onboard TVS (Transient Voltage Suppressor) diode, provides ESD protection and transient peak voltage protection.
- Onboard voltage translator, select 3.3V/5V operating voltage by jumper.
- Onboard 120Ω terminal resistor, configured by jumper.
- Breakout SPI control pins, for connecting with host control boards like STM32/Arduino.
- Comes with online development resources and a manual.
Specifications
- CAN controller: MCP2515
- CAN transceiver: SI65HVD230 (or SN65HVD230)
- Communication interface: SPI
- Operating voltage: 5V
- Logic level: 3.3V/5V
- Dimensions: 65.0 x 56.5 mm
- Mounting hole diameter: 3.0mm
Interfaces
CAN bus
PIN | Raspberry Pi (BCM) |
Raspberry Pi (WPI) |
Description |
5V | 5V | 5V | 5V Power input |
GND | GND | GND | Ground |
MISO | 9 (MISO) | 13 (MISO) | SPI clock input |
MOSI | 10 (MOSI) | 12 (MOSI) | SPI data input |
SCK | 11 (SCLK) | 14(SCLK) | SPI data output |
CS_0 | 8 (CE0) | 10 (CE0) | CAN_0 chip select |
INT_0 | 23 (Default)/22 | 6 (Default)/5 | CAN_0 interrupt output |
CS_1 | 7 (CE1) | 11 (CE1) | CAN_1 chip select |
INT_1 | 25 (Default)/24 | 4 (Default)/3 | CAN_1 interrupt output |
NOTE:
INT_0 is soldered by PIN23 by default, and INT_1 is soldered by PIN25 by default. If you need to modify the pins, you need to modify the soldering pad of the PCBA board, and also modify the setting of /boot/config.txt. Take the modification of INT_0 as an example: if PIN23 is changed to PIN22, then the soldering pad of the red frame on the left board in the figure below needs to be modified to the soldering pad of the board on the right in the figure below: Similarly, if you need to change INT_1 from the default PIN25 to PIN24, you need to solder the 0 ohm at the arrow of PIN25 to PIN24.
CAN bus
CAN module could process packets transmitted/received on the CAN bus. Packets transmit the first store packet to the related buffer and control register.
Use the SPI interface to set the bits on the control register or enable the transmit pin for transmitting.
Registers could be read to detect communication states and errors. It will first check if there are any errors of packets detected on the CAN bus,
then verify it with a filter that is defined by the user. And store the packet in one of the buffers if it has no errors.
Raspberry Pi cannot support SPI bus, so this module uses an SPI interface and on
board a receiver/transmitter for CAN communication.
Microchip Technology's MCP2515 is a stand-alone Controller Area Network (CAN) controller that implements the CAN specification, version 2.0B.
It is capable of transmitting and receiving both standard and extended data and remote frames.
The MCP2515 has two acceptance masks and six acceptance filters that are used to filter out unwanted messages,
thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via an industry-standard Serial Peripheral Interface (SPI),
that is Raspberry Pi can communicate with MCP2515 via SPI interface without an external driver. What we need to do is to enable the kernel driver on the devices tree.
For more details, please refer to the datasheet.
Note: Since October 2021, due to the lack of stock of the SN65HVD230 chip and the price increase, the chip has been replaced with SI65HVD230, and the software and hardware functions are compatible.
SN65HVD230 from TEXAS INSTRUMENTS is a CAN transceiver, which is designed for high communication frequency, anti-jamming, and high-reliability CAN bus communication.
SN65HVD230 provides three different modes of operation: high-speed, slope control, and low-power modes. The operation mode can be controlled by Rs pin.
Connect the Tx of the CAN controller to SN65HVD230's data input pin D, which can transmit the data of the CAN node to the CAN network;
And connect the RX of the CAN controller to SN65HVD230's data input pin R to receive data.
Working with Raspberry Pi
The working voltage level of Raspberry Pi is 3.3V, therefore we need to set the VIO of 2-CH CAN HAT to 3.3V as below:
Note: When connecting to the Raspberry Pi 2b, 3b, and 4b boards, please connect the booster and nylon post to fix it to avoid the back of the CAN terminal touching the HDMI interface causing a short circuit, and avoid wrong connection or poor contact:
When connecting to the Raspberry Pi, you must add a booster seat, and then pass the needle through the bottom plate, the effect is as follows:
Enable SPI interface
- Open the terminal, and use the command to enter the configuration page.
sudo raspi-config Choose Interfacing Options -> SPI -> Yes to enable the SPI interface.
sudo reboot
Please make sure that the SPI interface was not used by other devices, you can check in the /boot/config.txt.
Install libraries
- bcm2835
Open the terminal and run the commands below to install the bcm2835 library:
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz tar zxvf bcm2835-1.60.tar.gz cd bcm2835-1.60/ sudo ./configure sudo make sudo make check sudo make install # For More: http://www.airspayce.com/mikem/bcm2835/
wiringPi
For 32-bit Raspberry Pi system
#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 sudo gpio -v # Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error
For 64-bit Raspberry Pi System
Copy the resource package to the Raspberry Pi using the command:
wget https://files.waveshare.com/upload/8/8c/WiringPi-master.zip
(optional, you can skip this step if you have used the unzip command) Install the unzip environment:
sudo apt-get install unzip
Go to the file location and execute the unzip command:
unzip WiringPi-master.zip
Go to the file directory (go to the "WiringPi-master" folder):
cd WiringPi-master/
Run sudo . /build:
sudo ./build
(optional, see point 4 for errors) If . /build does not work, execute "chmod +x . /build" and then "sudo . /build":
chmod +x ./build
- Python
Install python library:
#python2 sudo apt-get update sudo apt-get install python-pip sudo apt-get install python-pil sudo apt-get install python-numpy sudo pip install RPi.GPIO sudo pip install spidev sudo pip2 install python-can #python3 sudo apt-get update sudo apt-get install python3-pip sudo apt-get install python3-pil sudo apt-get install python3-numpy sudo pip3 install RPi.GPIO sudo pip3 install spidev sudo pip3 install python-can
- WiringPi is no longer in the Debian package manager. Here are alternative instructions to install WiringPi:
https://forums.raspberrypi.com/viewtopic.php?t=350016#p2097654
Preparation
Insert the module into Raspberry Pi, and then modify the config.txt file:
sudo nano /boot/config.txt
Append these statements to the file:
dtparam=spi=on dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=25 dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=23 dtoverlay=spi-bcm2835-overlay
- Save and exit, then restart your Pi:
sudo reboot
- After restart, check if initialize successfully:
dmesg | grep spi
- Set up CAN:
sudo ip link set can0 up type can bitrate 1000000 sudo ip link set can1 up type can bitrate 1000000 sudo ifconfig can0 txqueuelen 65536 sudo ifconfig can1 txqueuelen 65536
- For more information about CAN kernel instructions, please check:
https://www.kernel.org/doc/Documentation/networking/can.txt
- Run "ifconfig":
ifconfig
- Turn off CAN device:
sudo ifconfig can0 down sudo ifconfig can1 down
Testing
If there is only one 2-CH CAN HAT on hand, you can connect CAN0_H and CAN1_H and CAN0_L and CAN1_L of the module as shown in the following figure:
- install can-utils:
sudo apt-get install can-utils
- Open two terminal windows:
One terminal input receives the CAN0 data command:
candump can0
Another terminal input sends the CAN1 data command:
cansend can1 000#11.22.33.44
- The demonstration effect is as follows: (receiving on the left, sending on the right.)
If you have two 2-CH CAN HAT on hand, you can directly connect CAN_H and CAN_L two by two. The effect is the same as the above, pay attention to match the communication rate, identify the ID, and output the interface serial number.
Python example
- Enter the directory of the Python example:
- Run the receiver.py script in the receiver terminal:
sudo python reveive.py
- Run the send.py script in the sender terminal:
sudo python send.py
【Note】The CAN1 is used as sender, and CAN0 is used as receiver.
Codes analysis
【Python example】
The demo codes provided are based on Python, please check that you installed the python-can library.
Before you send data, you should create a CAN device first:
os.system('sudo ip link set can0 type can bitrate 100000') os.system('sudo ifconfig can0 up')
This code is used to initialize CAN0 as the receiver/sender. If you want to change it to CNA1, you can use this one:
os.system('sudo ip link set can1 type can bitrate 100000') os.system('sudo ifconfig can1 up')
- Connect to CAN BUS:
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan')
- or:
can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan_ctypes')
- Create message:
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
msg = can.Message(is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7])
- Send message:
can0.send(msg)
- or:
can1.send(msg)
- Finally, close the CAN device:
os.system('sudo ifconfig can0 down')
- or:
os.system('sudo ifconfig can1 down')
- Receive message:
msg = can0.recv(10.0)
The variable of recv() function is the timeout of receiving.
For more information, please refer to: https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html
【WringPi example】
- Blocking the reception, the Raspberry Pi opens the terminal and runs:
cd 2-CH_CAN_HAT_Code/wiringPi/receive/ make clean sudo make sudo ./can_receive
- Raspberry Pi opens the terminal and runs:
cd 2-CH_CAN_HAT_Code/ wiringPi/receive/ make clean sudo make sudo ./can_send