The SparkFun AST-CAN485 is a versatile shield designed for Arduino-compatible boards, enabling communication over CAN (Controller Area Network) and RS485 networks. This shield is particularly useful in automotive and industrial environments where robust and reliable communication is essential. The AST-CAN485 integrates both CAN and RS485 transceivers, offering a bridge between these two popular communication protocols.
Pin Number | Function | Description |
---|---|---|
D2 | INT | Interrupt pin for CAN controller |
D9 | CS | Chip select for CAN controller |
D10 | RS485 DE/RE | RS485 Driver Enable / Receiver Enable |
D11 | MOSI | SPI communication with CAN controller |
D12 | MISO | SPI communication with CAN controller |
D13 | SCK | SPI communication with CAN controller |
A4 | RS485 DI | RS485 Data In |
A5 | RS485 RO | RS485 Data Out |
Mounting the Shield: Attach the SparkFun AST-CAN485 shield onto your Arduino-compatible board ensuring proper alignment of pins.
Wiring for CAN: Connect the CANH and CANL screw terminals to your CAN network. Ensure that the network has proper termination resistors.
Wiring for RS485: Connect the A and B screw terminals to your RS485 network. Ensure that the network is properly terminated.
Power Supply: Ensure that your Arduino board is powered with a suitable power source that can provide adequate current for both the Arduino and the AST-CAN485 shield.
#include <SPI.h>
#include <mcp_can.h>
// Initialize CAN controller with CS pin D9
MCP_CAN CAN(D9);
void setup() {
Serial.begin(115200);
// Initialize CAN bus at 500 kbps
if (CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("CAN bus shield initialized successfully!");
} else {
Serial.println("Error initializing CAN bus shield!");
}
}
void loop() {
// Check for incoming messages
if (CAN_MSGAVAIL == CAN.checkReceive()) {
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
// Read data: len = data length, buf = data byte(s)
CAN.readMsgBuf(&len, rxBuf);
rxId = CAN.getCanId();
Serial.print("Message ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for (int i = 0; i < len; i++) {
Serial.print(rxBuf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
CAN_OK
return code to check for successful initialization of the CAN controller.Q: Can I use the AST-CAN485 shield with a 3.3V Arduino board? A: Yes, the shield is compatible with both 3.3V and 5V systems.
Q: How do I change the CAN bus speed?
A: Modify the CAN.begin()
function in your code to set the desired speed (e.g., CAN_250KBPS
).
Q: What is the maximum length for a CAN or RS485 network? A: The maximum length depends on the baud rate and quality of the cables but can be up to 1200 meters for RS485 and 40 meters for CAN at 1 Mbps.
Q: How do I handle multiple nodes on a CAN network? A: Each node should have a unique ID, and the network should be properly terminated at both ends.
For further assistance, consult the SparkFun AST-CAN485 datasheet and the libraries' documentation.