Arduino-STM32-CAN
Arduino-STM32-CAN copied to clipboard
Can Example for Arduino Core STM32
Arduino-STM32-CAN
Basic Extended CAN(BxCan) communication example for Arduino Core STM32.
Hardware requirements
- STM32F072 Development Board.
- STM32F103 Development Board.
- STM32F303 Development Board.
- STM32F405 Development Board.
- STM32F407 Development Board.
- STM32F446 Development Board.
- CAN Transceiver.
MCP2551/2561/2562(5V)
TJA1040/1050/1055(5V)
SN65HVD230/231/232(3.3V)
NOTE
3V CAN Trasnceviers are fully interoperable with 5V CAN trasnceviers.
Check here.
Software requirements
Arduino core support for STM32 based boards.
https://github.com/stm32duino/Arduino_Core_STM32
There is some core library for STM32.
It doesn't work with Arduino STM32.
Wirering
- USB power supply (board with 5V pin)
Do not use the Windows USB port.
- Board without 5V pin, such as BlackPill, or ST-Link power supply
Transmitter
-
from STM32F072
-
from STM32F103
-
from STM32F303
-
from STM32F407
-
from STM32F446
Receiver
Communication with Arduino-UNO
You can use this library.
Communication with Arduino-DUE
You can use this library.
Communication with ESP8266
You can use this library.
Communication with ESP32
ESP-IDE has a CAN Network example.
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/twai/twai_network
You can use it.
Communication with Raspberry Pi
Edit /boot/config.txt and reboot.
dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
Make sure the device is ready after reboot.
$ dmesg | grep mcp251x
[ 19.992025] mcp251x spi0.0 can0: MCP2515 successfully initialized.
$ ls /sys/bus/spi/devices/spi0.0
driver modalias net of_node power statistics subsystem uevent
$ ls /sys/bus/spi/devices/spi0.0/net
can0
$ sudo /sbin/ip link set can0 up type can bitrate 1000000
$ dmesg | grep can0
[ 19.992025] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[ 1309.525795] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
$ sudo ifconfig can0
can0: flags=193<UP,RUNNING,NOARP> mtu 16
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 10 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
$ sudo ifconfig can0 txqueuelen 1000
Install can-utils.
$ sudo apt-get install can-utils
# Receiver
$ candump can0
# Transmitter
$ cansend can0 123#11223344AABBCCDD
Troubleshooting
There is a module of SN65HVD230 like this.
There is a 120 ohms terminating resistor on the left side.
A transmission fail will occur.
I have removed the terminating resistor.
And I used a external resistance of 150 ohms.
A transmission fail is fixed.
If the transmission fails, these are the possible causes.
- There is no receiving app on CanBus.
- The speed does not match the receiver.
- There is no terminating resistor on the CanBus.
- There are three terminating resistors on the CanBus.
- The resistance value of the terminating resistor is incorrect.
- Stub length in CAN bus is too long.
Though the ISO11898 Standard specifies a maximum bus length of 40m and maximum stub length of 0.3m at 1Mbps. See here. - Noise effects.
It occurs when the wire cable is thin or wire cable is crossing.
It is preferable to use wires that are noise-proof.
I changed the single wire to twisted wire. It's fixed.
Reference
https://github.com/nopnop2002/Robotell-USB-CAN-Python
https://github.com/nopnop2002/esp-idf-CANBus-Monitor