A Simple Tutorial for CANBUS

license

Lan_Makerfabs

By Lan_Makerfabs Follow

How to Create a Datacake Air Monitor Via 4G

3D Printed Encoder Knob

DIY a Laser Rangefinder

I've been studying CAN for three weeks, and now I've completed some applications to validate my learning results. In this tutorial, you will learn how to use Arduino to implement CANBUS communication. If you have any suggestions, welcome to leave a message.

Supplies

Hardware:

Software:

Step 1: What Is CANBUS

About CAN

CAN (Controller Area Network) is a serial communication network that can realize distributed real-time control. It is developed for the automotive industry to replace the complex wiring harness with a two-wire bus.

CAN protocol defines the Data Link Layer and part of the Physical Layer in the OSI model.

The CAN protocol is ISO standardized with ISO11898 and ISO11519. ISO11898 is the CAN high-speed communication standard with a communication speed of 125kbps-1Mbps. ISO11519 is the CAN low-speed communication standard with a communication speed of less than 125kbps.

Here we focus on high-speed CAN.

CAN Transceiver

The CAN Transceiver is responsible for the conversion between the logic level and the physical signal. Convert a logical signal to a differential level or a physical signal to a logical level.

CAN Controller

The CAN Controller is the core component of CAN, which realizes all the functions of the data link layer in CAN protocol and can automatically resolve CAN protocol.

MCU

The MCU is responsible for the control of the function circuit and the CAN controller. For example, the CAN controller parameters are initialized when the node starts, the CAN frame is read and sent through the CAN controller, etc.

Step 2: About CAN Communications

When the bus is idle, all nodes can start sending messages (multi-master control). The node that first accesses the bus gets the right to send (CSMA/CA mode). When multiple nodes start sending at the same time, the node that sends the high priority ID message gets the right to send.

In the CAN protocol, all messages are sent in a fixed format. When the bus is idle, all units connected to the bus can start sending new messages. When more than two cells start sending messages at the same time, the priority is determined based on the identifier. The ID does not represent the destination address of the send, but rather the priority of the message accessing the bus. When more than two cells start sending messages at the same time, each bit of the interest-free ID is arbitrated one by one. The unit that wins the arbitration can continue to send messages, and the unit that loses the arbitration immediately stops sending and receives the work.

The CAN bus is a broadcast type of bus. This means that all nodes can ‘hear’ all transmissions. all nodes will invariably pick up all traffic. The CAN hardware provides local filtering so that each node may react only to the interesting messages.

Step 3: ​Frames

CAN devices send data across the CAN network in packets called frames. CAN has four frame types:

Data frame

There are two types of data frames, standard and extended.

The meaning of the bit fields of the Figure are:

Arbitration

In the bus idle state, the unit that starts sending the message first gets the sending right. When multiple units start sending at the same time, each sending unit starts at the first bit of the arbitration segment. The unit with the largest number of continuous output dominant levels can continue to send.

Step 4: Speed and Distance

The CAN bus is a bus that connects multiple units at the same time. There is theoretically no limit to the total number of units that can be connected. In practice, however, the number of units that can be connected is limited by the time delay on the bus and the electrical load. Reduce the speed of communication, increase the number of units that can be connected, and increase the speed of communication, the number of units that can be connected decreases.

The communication distance is inversely related to the communication speed, and the farther the communication distance, the smaller the communication speed. The longer distance can be 1km or more, but the speed is less than 40kps.

Step 5: Hardware

Maduino Zero CAN-BUS module is a tool developed by Makerfabs for CANbus communication,it‘s based on Arduino, with the CAN controller and CAN transceiver, to create a ready-to-use CAN-bus port.

Step 6: Connection

Connect the DHT11 module to the Maduino Zero CAN-BUS module with wires to be used as an instrument to support CAN communication. Similarly, connect the display to the module to receive the data and display it.

The connection between Maduino Zero CANBUS and DHT11:

Maduino Zero CANBUS -- DHT11 3v3 ------ VCC GND ------ GND D10 ------ DATA

The connection between Maduino Zero CANBUS and OLED:

Maduino Zero CANBUS -- OLED 3v3 ------ VCC GND ------ GND SCL ------ SCL SDA ------ SDA

Use a DB9 cable to connect the two Maduino Zero CANBUS modules.

Step 7: Code

The MAX3051 completes the conversion of differential levels to logical signals. The MCP2515 completes CAN function such as data encoding and decoding. The MCU only needs to initialize the controller and send and receive data.

CAN.sendMsgBuf(0x10, 0, stmp1.length(), stmp_send1); delay(500); CAN.sendMsgBuf(0x11, 0, stmp2.length(), stmp_send2); delay(500);

“0x10” is mean the message ID, “0” is mean standard frame, “stmp1.length()” is mean the message length, “stmp_send1” is the data sent.

Step 8: Show

Power on the two modules, temperature and humidity will be displayed on the display.