CAN Receiver

Categories: ,

Introduction

In this tutorial, we’ll learn how to use the CAN (Controller Area Network) port on the CONTROLLINO MICRO to receive data. CAN is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other’s applications without a host computer.

Prerequisites

  • Installed Arduino IDE.
  • CONTROLLINO MICRO board.

Example Code

Please grab the following example .ino file from Github:

Overview of the Example Code

The provided example code is a simple CAN Receiver program. It initializes the CAN bus at a speed of 500 kbps and continuously checks for incoming CAN packets. When a packet is received, it prints its details and data to the Serial Monitor.

Key Functions

  • CAN.begin(speed): Initializes the CAN bus at the specified speed.
  • CAN.parsePacket(): Checks for incoming packets and returns their size.
  • CAN.packetId(): Returns the ID of the received packet.
  • CAN.packetExtended(): Checks if the packet uses an extended ID.
  • CAN.packetRtr(): Checks if the packet is a Remote Transmission Request (RTR).
  • CAN.read(): Reads a byte of data from the received packet.

Step-by-Step Guide

Step 1: Setting Up Your Environment

  1. Connect your CONTROLLINO MICRO to your computer.
  2. Open the Arduino IDE and select the correct board and port for your CONTROLLINO MICRO.

Step 2: Understanding the Code

  1. Serial Communication: The Serial.begin(115200); command starts serial communication at a baud rate of 115200. This is useful for debugging and monitoring CAN traffic.
  2. CAN Initialization: CAN.begin(500E3); initializes the CAN bus at 500 kbps. The if statement checks if the CAN bus starts successfully.
  3. Receiving CAN Packets: Inside the loop(), CAN.parsePacket(); checks for incoming packets. If a packet is received, its details (like ID, type, size) are printed to the Serial Monitor.

Step 3: Modifying the Code for Your Application

  • You can modify the loop() function to suit your application needs. For example, you might want to perform specific actions based on the packet’s ID or data.

Step 4: Uploading and Testing

  1. Copy the provided example code into a new sketch in the Arduino IDE.
  2. Upload the sketch to your CONTROLLINO MICRO.
  3. Open the Serial Monitor to view the output.

Step 5: Debugging

  • If you encounter issues, ensure your CAN bus is correctly configured and all devices are properly connected.
  • Check the baud rate and CAN bus settings.

Conclusion

You now have a basic understanding of how to use the CAN port on the CONTROLLINO MICRO to receive data. This setup can be the foundation for more complex applications involving vehicle networks, industrial automation, and more.

Share This Post

More Tutorials to explore

HTTP Web Client

Introduction Welcome to the “HTTP Web Client” tutorial using the CONTROLLINO MICRO. In this tutorial, we will learn how to create a web client that

Scan for I2C Devices

Introduction The I2C (Inter-Integrated Circuit) protocol is a popular means of communication between various sensors and devices. In this tutorial, we will learn how to