In this tutorial you will learn how to use the CAN controller on the Arduino UNO R4 WiFi board. The CAN controller is embedded in the UNO R4 WiFi's microcontroller (RA4M1). CAN is a serial protocol that is mainly used in the automotive industry.
Please note that CAN controller requires an external transceiver to function. Instructions and hardware examples are provided in this tutorial.
The goals of this tutorial are:
* In this tutorial, we are using a SN65HVD230 breakout module.
The CAN bus uses two wires: CAN high and CAN low. On the UNO R4 WiFi, these pins are:
To communicate with other CAN devices however, you need a transceiver module. In this tutorial, we will be using a SN65HVD230 breakout. To connect this, you can follow the circuit diagram available in the section below.
For this tutorial, we will use a simple example that sends a CAN message between two UNO R4 WiFi devices. If you wish, you can also connect an existing CAN device to the UNO R4 WiFi.
To connect the CAN transceiver, follow the table and circuit diagram below:
UNO R4 WiFi | CAN Transceiver |
---|---|
D13 (CANRX0) | CANRX |
D10 (CANTX0) | CANTX |
3.3V | VIN |
GND | GND |
Then, between the CAN transceivers, connect the following:
CAN Transceiver 1 | CAN Transceiver 2 |
---|---|
CANH (HIGH) | CANH (HIGH) |
CANL (LOW) | CANL (LOW) |
The following code examples need to be uploaded to each of the UNO R4 WiFi boards, one will send a message, one will receive it. These examples are available in the Renesas core, and using the Arduino IDE, you can access them by navigating to File > Examples > Arduino_CAN > CANWrite/CANRead
The library used is built into the core, so no need to install the library if you have the core installed.
To initialize the library, use
CAN.begin(CanBitRate::BR_250k)
, where a CAN bit rate is specified. Choose between:To send a CAN message, you can create a
CanMsg
object, which should contain the CAN_ID
, size and message data. Below is an example on how to create such object.1uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};2memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));3CanMsg msg(CAN_ID, sizeof(msg_data), msg_data);
After you have crafted a CAN message, we can send it off, by using the
CAN.write()
method. The following example creates a CAN message that increases each time void loop()
is executed. 1
To read an incoming CAN message, first use
CAN.available()
to check if data is available, before using CAN.read()
to read the message.1
This tutorial shows how to use the CAN bus available on the UNO R4 WiFi, and how to send and receive data using the Arduino_CAN library.
Read more about this board in the Arduino UNO R4 WiFi documentation.