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 code
Serial.begin(115200);
- CAN Initialization: Start the CAN bus at 500 kbps. This is a common speed for CAN networks.cppCopy code
SPI1.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 code
CAN.write('h'); CAN.write('e'); CAN.write('l'); CAN.write('l'); CAN.write('o');
- End Packet: End the CAN packet transmission.cppCopy code
CAN.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 code
CAN.write('w'); CAN.write('o'); CAN.write('r'); CAN.write('l'); CAN.write('d');
- End Extended Packet: Conclude the extended packet transmission.cppCopy code
CAN.endPacket();
Step 4: Loop
The loop()
function sends these packets continuously at one-second intervals.
Testing and Validation
- Monitor Serial Output: Open the Serial Monitor in your Arduino IDE to view the sending process and confirm successful transmission.
- Check CAN Network: If possible, use a CAN analyzer or another CAN device to ensure the packets are being received correctly.
- 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.