Main Content

Enable and Configure Raspberry Pi for SPI and CAN Communication Using MCP2515 CAN Controller

Follow these steps to set up the MCP2515 CAN interface on a Raspberry Pi® board.

Hardware Requirements

You need these components to set up CAN bus communication using Raspberry Pi:

  • Raspberry Pi board

  • MCP2515 CAN controller module

  • SI65HVD230 CAN transceiver

  • Jumper wires

  • CAN bus cables

Hardware Connections

Use the connections specified in this table to connect a Raspberry Pi board to a CAN bus using the MCP2515 CAN controller and the SI65HVD230 CAN transceiver.

Raspberry PiMCP2515 CAN ControllerSI65HVD230 CAN TransceiverCAN Bus

VCC

VCC

VCC

N/A

GND

GND

GND

GPIO 8 (CE0)

CS

N/A

GPIO 11 (SCLK)

SCL

GPIO 10 (MOSI)

SI

GPIO 9 (MISO)

SO

GPIO 25

INT

N/A

TXD

D (Driver Input)

RXD

R (Receiver Output)

N/A

CANH

CANH

CANL

CANL

Enable SPI Interface for CAN Communication

Raspberry Pi uses the serial peripheral interface (SPI) protocol to communicate with the CAN controller. Follow these steps to enable SPI on your Raspberry Pi, which, in turn, enables communication over the CAN bus.

  1. Open the Raspberry Pi terminal.

  2. Enter the following command to access the configuration interface.

    sudo raspi-config
  3. Use the arrow keys to select Interfacing Options and press Enter.

    Enable SPI Interfacing Options

  4. Select SPI from the list and press Enter.

    Enable SPI Interface on Raspberry Pi Board

  5. Select Yes to enable the SPI interface.

    Enable SPI Confirmation Window

  6. To apply the changes, reboot your Raspberry Pi.

    sudo reboot
  7. Verify if the SPI module is loaded on your Raspberry Pi.

    lsmod | grep spi

    For example, when you execute this command, the terminal lists spi_bcm2835 SPI driver.

  8. Verify if that SPI devices are available.

    ls /dev/spi*

    For example, when you execute this command, the terminal lists /dev/spidev0.0 and /dev/spidev0.1.

Set Up MCP2515 CAN Controller with SocketCAN

Modify the hardware settings in the config.txt file to enable the SPI interface and configure the MCP2515 CAN controller. Make sure to set up the hardware connections before you begin modifying the file.

  1. Open the Raspberry Pi terminal.

  2. Open the config.txt file. Note that the location of the file varies depending on the operating system running on your Raspberry Pi board. This table lists the file paths on the Bullseye and Bookworm operating systems and the commands to use to open the config text file.

    Operating SystemPath to config.txt fileCommand to execute on Raspberry Pi terminal
    Bullseye/boot/firmware/config.txt
    sudo nano /boot/firmware/config.txt
    Bookworm/boot/config.txt
    sudo nano /boot/config.txt

  3. Enable SPI interface.

    dtparam=spi=on
  4. Individually configure the CAN controllers. For this example, configure the can0 and can1 MCP2515 CAN controllers. Set the oscillator frequency for the controllers to 16000000 Hz. Set the interrupt pins 23 and 25. You can adjust the oscillator frequency, interrupt pins according to your requirements. Refer to the CAN vendor datasheet for more details on the number of CAN controllers supported, the supported oscillator frequency, and the interrupt pins.

    dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
    dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=23
  5. Save the file and exit.

  6. To apply the changes, reboot your Raspberry Pi.

    sudo reboot
  7. Verify CAN controller initialization.

    dmesg | grep spi
  8. Check availability of CAN interface on Raspberry Pi.

    ifconfig

    For example, when you execute this command, the terminal lists can0 and can1 controllers.

After you have successfully enabled and configured the MCP2515 CAN controller using your Raspberry Pi board, you can now use the CAN Transmit and CAN Receive blocks in a Simulink® model and configure the CAN properties in the Configuration Parameters dialog box.

See Also

| | | |

Topics

External Websites