Difference between revisions of "Creating CAN-BUS Shield V2.0 Tutorial"

From Embedded Lab Vienna for IoT & Security
Jump to navigation Jump to search
Line 36: Line 36:


Pigiback the CAN shields onto the Arduinos and connect both terminals. Bare in mind connecting CAN-H on the first board with CAN-H on the second board and CAN-L on the first board with CAN-L on the second board. This would look something like this:
Pigiback the CAN shields onto the Arduinos and connect both terminals. Bare in mind connecting CAN-H on the first board with CAN-H on the second board and CAN-L on the first board with CAN-L on the second board. This would look something like this:
[[File:Arduino_ECUs_connected.jpg|400 px]]


=== Step x ===
[[File:Arduino_ECUs_connected.jpg|500 px]]
 
==== Step 2 ====
Download and install the Arduino IDE. Afterwards we need to install the CAN-Shield library which can be found here https://github.com/Seeed-Studio/Seeed_Arduino_CAN. Download this repository as zip file and import it to the Arduino IDE:
[[File:Add_lib.JPG|750px]]
 
==== Step 3 ====
Next we are going to need a sending node, which will send CAN messages at periodic intervalls and we will need an receiving node, which listens on the CAN Bus and creates an interrupt everytime a package is received. The following code is based on the send example from the library.
 
<syntaxhighlight lang="c">
#include <SPI.h>
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
    #define SERIAL SerialUSB
#else
    #define SERIAL Serial
#endif
 
#define CAN_2515
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
 
#ifdef CAN_2515
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif                              // Set CS pin
 
void setup() {
    SERIAL.begin(115200);
    while(!Serial){};
 
#ifdef CAN_2518FD
    while (0 != CAN.begin((byte)CAN_500K_1M)) {            // init can bus : baudrate = 500k
#else
    while (CAN_OK != CAN.begin(CAN_500KBPS)) {            // init can bus : baudrate = 500k
#endif       
        SERIAL.println("CAN BUS Shield init fail");
        SERIAL.println(" Init CAN BUS Shield again");
        delay(100);
    }
    SERIAL.println("CAN BUS Shield init ok!");
}
 
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop() {
    // send data:  id = 0x00, standrad frame, data len = 8, stmp: data buf
    stmp[7] = stmp[7] + 1;
    if (stmp[7] == 100) {
        stmp[7] = 0;
        stmp[6] = stmp[6] + 1;
 
        if (stmp[6] == 100) {
            stmp[6] = 0;
            stmp[5] = stmp[6] + 1;
        }
    }
 
    CAN.sendMsgBuf(0x00, 0, 8, stmp);
    delay(100);                      // send data per 100ms
    SERIAL.println("CAN BUS sendMsgBuf ok!");
}
</syntaxhighlight>
 
 
==== Step x ====
Nodes are talking to each other:
Nodes are talking to each other:
[[File:Nodes_talking.gif]]
 
[[File:Nodes_talking_console.JPG]]
[[File:Nodes_talking.gif|500 px]]
 
[[File:Nodes_talking_console.JPG|1000 px]]





Revision as of 19:49, 23 February 2021

(DRAFT)

Summary

This documentation introduces the CAN-BUS Shield V2.0 and all its key facts. It also contains an example implementation, which is able to read and write CAN messages. In another step a real car communication is captured and presented.

CAN-BUS Shield V2.0

The CAN-BUS Shield V2.0 is an add-on shield which can be used with an Arduino Board. It comes with the MCP2515 CAN Bus Controller and the MCP2551 CAN transceiver. When used with an appropriate OBD 2.0 cable and library it can function as a diagnostic tool for a car.

InkedCAN-BUS Shield V2.0 desc LI.jpg

  1. Terminal: CAN-H, CAN-L
  2. Led Indicator: PWR=power;TX=blink when the data is sending;RX=blink when there's data receiving;INT=data interrupt
  3. V_OBD: Indicates if the power comes from OBD
  4. DB9 Interface: to connect to OBD 2.0 via DB9-OBDII-cable
  5. Arduino Pinouts
  6. I2C Connector
  7. Serial Connector
  8. ICSP Pins

CAN is the abbrevation for Controller Area Network and is a common and reliable bus system. It is used in almost every car since 1994. CAN was founded by BOSCH. Its main aim is to shrink cable harness.

In order to complete the following steps, you should be familiar with the Arduino UNO and the Arduino IDE.

Description

In order to create a functioning CAN message reader we need to set up a lab, which consits of two CAN-BUS Shield V2.0 and two Arduino UNOs. The Arduinos will be used to simulate two ECUs, where one of them sends the data and the other is receives it. After this works as planned we will take a step further and integrate the CANtact Bundle cable. This will enable is to take the CAN-Node (Arduino and CAN-Shield) and plug it into the cars OBD 2.0 port in order to see what is going on on the car CAN-BUS.

CAN-BUS Shield V2.0
Arduino UNO
CANtact Bundle

Step 1

Pigiback the CAN shields onto the Arduinos and connect both terminals. Bare in mind connecting CAN-H on the first board with CAN-H on the second board and CAN-L on the first board with CAN-L on the second board. This would look something like this:

Arduino ECUs connected.jpg

Step 2

Download and install the Arduino IDE. Afterwards we need to install the CAN-Shield library which can be found here https://github.com/Seeed-Studio/Seeed_Arduino_CAN. Download this repository as zip file and import it to the Arduino IDE: Add lib.JPG

Step 3

Next we are going to need a sending node, which will send CAN messages at periodic intervalls and we will need an receiving node, which listens on the CAN Bus and creates an interrupt everytime a package is received. The following code is based on the send example from the library.

#include <SPI.h>
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
    #define SERIAL SerialUSB
#else
    #define SERIAL Serial
#endif

#define CAN_2515
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif

#ifdef CAN_2515
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif                              // Set CS pin

void setup() {
    SERIAL.begin(115200);
    while(!Serial){};

#ifdef CAN_2518FD
    while (0 != CAN.begin((byte)CAN_500K_1M)) {            // init can bus : baudrate = 500k
#else
    while (CAN_OK != CAN.begin(CAN_500KBPS)) {             // init can bus : baudrate = 500k
#endif        
        SERIAL.println("CAN BUS Shield init fail");
        SERIAL.println(" Init CAN BUS Shield again");
        delay(100);
    }
    SERIAL.println("CAN BUS Shield init ok!");
}

unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop() {
    // send data:  id = 0x00, standrad frame, data len = 8, stmp: data buf
    stmp[7] = stmp[7] + 1;
    if (stmp[7] == 100) {
        stmp[7] = 0;
        stmp[6] = stmp[6] + 1;

        if (stmp[6] == 100) {
            stmp[6] = 0;
            stmp[5] = stmp[6] + 1;
        }
    }

    CAN.sendMsgBuf(0x00, 0, 8, stmp);
    delay(100);                       // send data per 100ms
    SERIAL.println("CAN BUS sendMsgBuf ok!");
}


Step x

Nodes are talking to each other:

Nodes talking.gif

Nodes talking console.JPG


Used Hardware

Device to be used with this documentation Maybe another device to be used with this documentation

Courses

References