The CAN Bus Shield by SEEED is an expansion board designed to enable Arduino boards to communicate over a Controller Area Network (CAN). This shield is particularly useful for automotive and industrial applications where multiple microcontrollers need to communicate with each other efficiently without a host computer.
Common applications include:
Pin Number | Function | Description |
---|---|---|
D2 | INT | Interrupt pin, signals the Arduino of incoming messages |
D9 | CS | Chip Select for SPI communication with MCP2515 |
D10 | Not Connected | Reserved for future use |
D11 | MOSI | SPI communication with MCP2515 |
D12 | MISO | SPI communication with MCP2515 |
D13 | SCK | SPI Clock |
GND | Ground | Ground connection |
VCC | Power Supply | 5V power supply from the Arduino |
Before using the shield, you need to initialize the CAN controller and set the desired baud rate for communication. Here's an example of how to do this using the MCP_CAN
library:
#include <mcp_can.h>
#include <SPI.h>
// The SPI Chip Select pin for the CAN controller
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin for CAN controller
void setup() {
Serial.begin(115200);
// Initialize CAN bus at 500 kbps
if (CAN_OK == CAN.begin(CAN_500KBPS)) {
Serial.println("CAN Bus Shield initialized at 500 kbps.");
} else {
Serial.println("CAN Bus Shield initialization failed.");
}
}
void loop() {
// Code to send and receive data goes here
}
To send data over the CAN bus, you can use the sendMsgBuf
function provided by the MCP_CAN
library:
unsigned char message[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
// Send a standard frame with ID 0x100 and length of 8 bytes
CAN.sendMsgBuf(0x100, 0, 8, message);
To receive data, you can use the readMsgBuf
and checkReceive
functions:
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive()) {
CAN.readMsgBuf(&len, buf); // Read data: len = data length, buf = data byte(s)
unsigned long canId = CAN.getCanId();
Serial.print("Message received with ID: ");
Serial.print(canId, HEX);
Serial.print(" Data: ");
for (int i = 0; i < len; i++) {
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
CAN.init_Filt()
and CAN.init_Mask()
functions to configure message filtering if you're only interested in certain CAN IDs.MCP_CAN
library for compatibility and bug fixes.Q: Can I use multiple CAN Bus Shields on the same network? A: Yes, you can use multiple shields on the same CAN network, but each device must have a unique ID.
Q: What is the maximum length of the CAN bus? A: The maximum length depends on the baud rate; for example, at 500 kbps, the maximum length is typically around 100 meters.
Q: How do I know if my CAN Bus Shield is working? A: You can test the shield by sending messages and checking if they are received by another CAN device. The onboard LED indicators can also provide a visual indication of activity.
For further assistance, consult the SEEED forums or contact technical support.