ExplainerIoT ProtocolsIoT Software&ToolsSensor & Devices

Controller Area Network (CAN) Basics

Introduction

The Controller Area Network (CAN) is a robust serial communication protocol widely used in automotive, industrial, and embedded applications. Developed by Bosch in the 1980s, CAN allows microcontrollers and devices to communicate without a central host, making it ideal for real-time, high-reliability systems.

This article explores the fundamentals of CAN, including its architecture, message format, error handling, and practical applications.

1. CAN Architecture

Overview

CAN follows a multi-master, message-based communication model where multiple nodes can transmit and receive data over a shared bus.

Key Components:

  1. CAN Nodes: Devices or microcontrollers connected to the CAN bus.
  2. CAN Bus: A two-wire differential signaling bus (CAN_H, CAN_L) that reduces noise interference.
  3. CAN Transceiver: Converts logic-level signals to differential CAN signals and vice versa.
  4. CAN Controller: Manages message arbitration, error detection, and data transmission.

CAN Bus Communication Model:

  • Uses a differential signal (CAN_H and CAN_L) to improve noise immunity.
  • Follows the Carrier Sense Multiple Access/Collision Resolution (CSMA/CR) protocol for message arbitration.

2. CAN Message Format

Standard and Extended CAN Frames

CAN supports two frame formats:

  • Standard CAN (11-bit identifier): 2¹¹ = 2048 unique message identifiers.
  • Extended CAN (29-bit identifier): 2²⁹ = 536 million unique message identifiers.

Frame Structure:

  1. Start of Frame (SOF): Indicates the beginning of a frame.
  2. Identifier (ID): Unique message ID used for arbitration.
  3. Control Field: Contains data length (DLC – Data Length Code).
  4. Data Field: Contains 0 to 8 bytes of payload data.
  5. CRC Field: Ensures data integrity.
  6. ACK Field: Acknowledgment from receiving nodes.
  7. End of Frame (EOF): Marks the frame’s end.

Message Prioritization:

  • Lower identifier values indicate higher priority messages.
  • The arbitration process ensures that only one node transmits at a time.

3. CAN Communication Mechanism

Arbitration Process

  • CAN uses bitwise arbitration to resolve conflicts.
  • A node with a dominant bit (0) wins arbitration over a recessive bit (1).
  • The lowest identifier number (highest priority) gets bus access.

Error Handling in CAN

CAN has built-in error detection and correction mechanisms:

  1. Bit Errors: Detects incorrect bit values during transmission.
  2. Stuff Errors: Ensures proper frame synchronization by inserting stuffing bits.
  3. CRC Errors: Validates data integrity using cyclic redundancy check (CRC).
  4. Acknowledgment Errors: Ensures proper frame reception.
  5. Form Errors: Detects incorrect format in fixed fields.

If an error is detected, the erroneous node sends an Error Frame and retransmits the message.

4. CAN Bus Implementation

Hardware Requirements

  • Microcontroller with CAN Controller: STM32, ESP32, ATmega32M1, etc.
  • CAN Transceiver: MCP2551, SN65HVD230, TJA1050.
  • Termination Resistors (120Ω): Placed at both ends of the CAN bus to prevent signal reflection.

CAN Communication Example (C – STM32 HAL)

CAN_HandleTypeDef hcan;
CAN_TxHeaderTypeDef TxHeader;
CAN_RxHeaderTypeDef RxHeader;
uint8_t TxData[8];
uint8_t RxData[8];
uint32_t TxMailbox;

void CAN_Init() {
    hcan.Instance = CAN1;
    hcan.Init.Prescaler = 16;
    hcan.Init.Mode = CAN_MODE_NORMAL;
    hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
    hcan.Init.TimeSeg1 = CAN_BS1_8TQ;
    hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
    HAL_CAN_Init(&hcan);
}

void CAN_SendMessage() {
    TxHeader.StdId = 0x123;
    TxHeader.DLC = 2;
    TxData[0] = 0xAB;
    TxData[1] = 0xCD;
    HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox);
}

void CAN_ReceiveMessage() {
    if (HAL_CAN_GetRxFifoFillLevel(&hcan, CAN_RX_FIFO0) > 0) {
        HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, RxData);
    }
}

5. CAN Variants and Extensions

CAN FD (Flexible Data Rate)

  • Increases data payload up to 64 bytes.
  • Supports variable bit rates for faster communication.

CANopen & J1939 Protocols

  • CANopen: Industrial automation applications.
  • J1939: Heavy-duty vehicle and automotive applications.

6. CAN Applications

  1. Automotive Systems: Engine control units (ECUs), airbag systems, ABS.
  2. Industrial Automation: Factory automation, motor control, robotic systems.
  3. Medical Devices: Patient monitoring systems, diagnostic equipment.
  4. Aerospace & Defense: Avionics communication, unmanned systems.

Conclusion

CAN is a powerful, reliable, and efficient communication protocol widely used in automotive and industrial applications. Its ability to handle multiple nodes, error detection mechanisms, and robust performance make it a preferred choice for real-time embedded systems.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *