Main Content

Read and Write CAN Messages with Arduino Hardware

This example shows you how to use the MATLAB® Support Package for Arduino® Hardware to read and write data from the CAN network using the specified CAN device.

Required Hardware

To run this example, you must have the following hardware:

  • Sparkfun CAN-Bus Shield

  • Arduino Uno

  • MKR CAN Shield

  • Arduino MKR1000

Connect the CAN Shields to the Arduino boards. The two Arduino boards form a CAN network. In this example, the Arduino Uno is used as a transmitter, and the Arduino MKR1000 is used as a receiver. Connect the CANH and CANL pins in the two shields to complete the connection.

This figure shows an example of connection for CAN communication between MKR CAN Shield and Sparkfun CAN-Bus Shield.

CAN_image_update.png

Create Connection to CAN Channel to Write CAN Messages

Create an arduino object with the Arduino Uno board and include the CAN library.

arduinoUnoObj = arduino('COM15','Uno','Libraries','CAN')
arduinoUnoObj = 
  arduino with properties:

                  Port: 'COM15'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a CAN channel object connected to the Sparkfun CAN-Bus Shield to write messages.

txObj = canChannel(arduinoUnoObj,'MCP2515','D10','D2')
txObj = 
  Channel with properties:

          Device: 'MCP2515'
    ProtocolMode: 'CAN'
        BusSpeed: 500000
        Database: []

Show all properties

Create Channel to Read CAN Messages

Create a connection to the Arduino MKR1000 board with the CAN library.

arduinoMKRObj = arduino('COM5','MKR1000','Libraries','CAN')
arduinoMKRObj = 
  arduino with properties:

                  Port: 'COM5'
                 Board: 'MKR1000'
         AvailablePins: {'D0-D14', 'A0-A6'}
  AvailableDigitalPins: {'D0-D14', 'A0-A6'}
      AvailablePWMPins: {'D0-D8', 'D10', 'A3-A4'}
   AvailableAnalogPins: {'A0-A6'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'CAN', 'SPI'}
Show all properties

Create a CAN channel object connected to the MKR CAN Shield to read messages.

rxObj = canChannel(arduinoMKRObj,'MKR CAN Shield')
rxObj = 
  Channel with properties:

          Device: 'MKR CAN Shield'
    ProtocolMode: 'CAN'
        BusSpeed: 500000
        Database: []

Show all properties

Write Data from Transmitter CAN Channel Object

Write a CAN message into the channel.

write(txObj, 100, false, [1:8])

Read Data from Receiver CAN Channel Object

Read the CAN message from the channel.

readMsg = read(rxObj)
readMsg=1×8 timetable
              Time              ID     Extended       Name          Data        Length      Signals       Error    Remote
    ________________________    ___    ________    __________    ___________    ______    ____________    _____    ______

    06-Feb-2020 14:33:52.901    100     false      {0×0 char}    {1×8 uint8}      8       {0×0 struct}    false    false 

readMsg.Data
ans = 1×1 cell array
    {1×8 uint8}

readMsg.Data{:}
ans =

     1     2     3     4     5     6     7     8

Clean Up

When finished, clear the connections to the hardware.

clear txObj;
clear arduinoUnoObj;
clear rxObj;
clear arduinoMKRObj;