Main Content

Transmit and Receive Data Using Arduino® CAN Blocks

This example shows how to use Simulink® Support Package for Arduino® Hardware to transmit and receive data from the CAN network using the specified CAN device.

Supported Hardware

  • Arduino Mega 2560

  • Arduino Mega ADK

  • Arduino Uno

  • Arduino Due

  • Arduino MKR1000

  • Arduino MKR WIFI 1010

  • Arduino ZERO

  • Arduino Micro

  • Arduino Leonardo

  • Arduino Nano 33 IoT

Introduction

In this example, the Arduino CAN Transmit model with a CAN Transmit block is used to transmit data. The Arduino CAN Receive model with a CAN Receive block is used to receive data.

Prerequisites

Before you start this example, you must:

Required Hardware

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

  • Supported Arduino board

  • Connecting wires

  • OBD(On-board diagnostics) Connector

  • MCP2515 based CAN shield

Configure CAN hardware

1. Connect the CAN Shield on the Arduino board.

2. Connect the OBD Connector from the vehicle to the MCP2515 based CAN shield. For more information on MCP2515, see MCP2515.

Configure the Model for Supported Arduino Hardware

1. In your Simulink model, click Modeling tab and then select Model Settings to open Configuration Parameters dialog.

2. Select the Hardware Implementation pane and select your required Arduino hardware from the Hardware board parameter list. Do not change any other settings. For information on setting or modifying the CAN properties, see Model Configuration Parameters for Simulink Support Package for Arduino Hardware

3. Click OK.

Transmit Data From Arduino Hardware

This example uses the Arduino CAN Transmit model, with a CAN Transmit block, to transmit 1 byte of data from the Arduino hardware. To open the model, enter the following command at the MATLAB command prompt.

open_system('arduino_CAN_transmit');

1. Set the CAN Transmit block parameters as shown in this image.

2. In your Simulink model, click the Hardware tab and then click Monitor & Tune to transmit the data.

Receive Data with Arduino Hardware

This example uses the Arduino CAN Receive model, with a CAN Receive block, to receive 1 byte of data by the Arduino hardware. To open the model, enter the following command at the MATLAB command prompt.

open_system('arduino_CAN_receive');

1. Set the CAN Receive block parameters as shown.

2. In your Simulink model, click the Hardware tab and then click Monitor & Tune to receive the data. The Scope block displays the received data in the target hardware.

Other things to Try