I have an Arduino Mega 2560 (Genuine) and a Can Bus Shield V2 (Seed Studio), in order to read data from CAN Bus (non transmit).
I need to communicate between them via SPI and I did the following connections:
SCK - Arduino Mega pin 52 <-> CAN Shield pin D13
MISO - Arduino Mega pin 50 <-> CAN Shield pin D12
MOSI - Arduino Mega pin 51 <-> CAN Shield pin D11
CS - Arduino Mega pin 9 <-> CAN Shield pin D9
Power supply to CAN Bush Shield is from pin 5V of Arduino Mega to 5V pin of CAN Shield.
If not, (connecting Vin pins the CAN communication doesn't work).
#include <SPI.h>
#include "mcp2515_can.h"
const int SPI_CS_PIN = 9;
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin for CAN shield
void setup(void) {
Serial.begin(9600); // debug serial
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // can bus : baudrate = 500k
Serial.println(F("CAN init fail, retry..."));
delay(100);
}
Serial.println(F("CAN init ok!"));
}
void loop(void) {
Serial.println(F("Looping"));
}
CAN Init Fail error occurs with CAN bus connected and trasmitting and without CAN connected.
I tried also Examples CODE and I also tried to change pin 9 to others but nothing.
I used in the past the same can bus shild with an arduino nano and it worked. Now I have only the Mega board.
I'm also afraid that the Can Shield is damaged but it is strange because is was in a box during inutilization period.
I tried both (4 occurrences):
CS - Arduino Mega pin 9 <-> CAN Shield pin D9
CS - Arduino Mega pin 9 <-> CAN Shield pin D10
CS - Arduino Mega pin 10 <-> CAN Shield pin D9
CS - Arduino Mega pin 10 <-> CAN Shield pin D10
Theoretically the shield has as default CS pin the D9, in order to change it is necessary an hardware trick (with dedicated pads in the bottom side of the board) and I leaved the default D9 pin.
One other question, if CAN bus isn't connected, the initialization will be OK or will returns error?
I don't remember the result of the initialization process without CAN BUS connected.
Thanks but the connections are ok.
I also changed the can shield with another one working from a friend of mine.
The problem is the same.
Pinout connection are correct based on PINOUT schematics for Mega and Can shield v2.
I've found two things,
First:
is mandatory for MCP2515 declare also the INT pin (2), if not the communication doesn't works.
Second:
if the CAN shield board is not mounted on Arduino board, connecting only:
SCK, MISO, MOSI, CS, +5V, GND, Interrupt (pin2), the board doesn't work, interrupt dowsn't work and CAN doesn't init.
Mounting the CAN Shield on the Arduino board it works only if both boards are powered by USB OR via 5V pin, NOT Vin pin (also using +5V as power supply).
Vin needs at least 7V to function, it is the input to the onboard 5V regulator, it is a linear reg and so needs about 2V or more higher input than 5V output.
If you look at the diagram I posted in post #5, it shows the free pins and the pins that need connection, there are more than just the pins you have listed.
Among them is D2 which is the interrupt pin.