CAN Sender

Categories: ,

Introduction

This tutorial will guide you through using the CAN (Controller Area Network) port on the CONTROLLINO MICRO to send data. We’ll use example code to demonstrate how to set up and send both standard and extended CAN messages.

Prerequisites

  • Installed Arduino IDE.
  • CONTROLLINO MICRO board.

Example Code

Please grab the following example .ino file from Github:

Step-by-Step Guide

Step 1: Initialization

  • Serial Begin: Initialize serial communication at 115200 baud rate to monitor the data sending process.cppCopy codeSerial.begin(115200);
  • CAN Initialization: Start the CAN bus at 500 kbps. This is a common speed for CAN networks.cppCopy codeSPI1.setRX(PIN_SPI1_MISO); SPI1.setTX(PIN_SPI1_MOSI); SPI1.setSCK(PIN_SPI1_SCK); if (!CAN.begin(500E3)) { Serial.println("Starting CAN failed!"); while (1); }

Step 2: Sending Standard CAN Packets

  • Begin Packet: Start a CAN packet with a standard ID (11 bits). In this example, 0x12 is used as the ID.cppCopy codeCAN.beginPacket(0x12);
  • Write Data: Send the data bytes. Here, we’re sending the word ‘hello’.cppCopy codeCAN.write('h'); CAN.write('e'); CAN.write('l'); CAN.write('l'); CAN.write('o');
  • End Packet: End the CAN packet transmission.cppCopy codeCAN.endPacket();

Step 3: Sending Extended CAN Packets

  • Begin Extended Packet: Start an extended CAN packet with a 29-bit ID. Example ID: 0xabcdef.cppCopy codeCAN.beginExtendedPacket(0xabcdef);
  • Write Extended Data: Similar to standard packets, but with different data. In this case, ‘world’.cppCopy codeCAN.write('w'); CAN.write('o'); CAN.write('r'); CAN.write('l'); CAN.write('d');
  • End Extended Packet: Conclude the extended packet transmission.cppCopy codeCAN.endPacket();

Step 4: Loop

The loop() function sends these packets continuously at one-second intervals.

Testing and Validation

  1. Monitor Serial Output: Open the Serial Monitor in your Arduino IDE to view the sending process and confirm successful transmission.
  2. Check CAN Network: If possible, use a CAN analyzer or another CAN device to ensure the packets are being received correctly.
  3. Troubleshooting: If there are issues in transmission, check connections, baud rate settings, and CAN IDs.

Conclusion

With this tutorial, you should be able to send both standard and extended CAN messages from your CONTROLLINO MICRO. This functionality is essential for many automotive and industrial applications using the CAN network.

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