I have 2nos Arduino UNO and 02nos MCP2515 CAN Modules
The run a simple Blink program and it works fine when the Arduino are connected to PC and the Serial Monitor is ON.
But this setup doesn't work without connecting the PC and powering the Arduino and MCP2515 externally.
Is it necessary to connect PC and Serial Monitor running for CAN communication????
Please help....
Can you please link to the CAN-BUS module you're using and post your code.
Unless the sketch requires some serial input to commence the can-bus communication, then definitely not.
Imagine what your car would look like if each can node had a PC and serial monitor. Not needed unless your application requires it or for debugging. You can power it with an external power supply, that is basically what the USB is along with a serial connection. Remember to terminate the CAN bus properly or it will drive you bonkers.
Blink Sender Code
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
struct can_frame canMsg1; //LED ON OFF
MCP2515 mcp2515(10); // chip select pin 10
void setup()
{
Serial.begin(9600);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
mcp2515.setNormalMode();
canMsg1.can_id = 0x10;
canMsg1.can_dlc = 1;
}
void loop()
{
canMsg1.data[0]= 1;
mcp2515.sendMessage(&canMsg1);
delay(1000);
canMsg1.data[0]= 0;
mcp2515.sendMessage(&canMsg1);
delay(1000);
}
Blink Receiver Code
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
struct can_frame canMsg;
MCP2515 mcp2515(10); // chip select pin 10
void setup()
{
pinMode(7,OUTPUT);
Serial.begin(9600);
SPI.begin(); //Begins SPI communication
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
}
void loop() {
int a;
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
{
if (canMsg.can_id == 0x10)
{
a = canMsg.data[0];
}
}
if(a == 0)
{
digitalWrite(7,LOW);
}
if(a == 1)
{
digitalWrite(7,HIGH);
}
}
Sir I uploaded my code, can you please check if i made any mistake...
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.
Please edit your post with the code, select all code and click the </> button to apply code tags; next save your post. It makes it easier to read and copy, maintains indentations and prevents that the forum software from thinking that they are formatting instructions.
You can remove SPI.begin(); from both sketches.
However, both sketches compile and there's nothing to suggest that the program would only work when the serial monitor is open. Maybe you have connected the external power source incorrectly.
Hi,
When you disconnect the PC, how are you powering both UNO and 2515?
Tom...
Sir I provide external 5v to VIN of Uno and Vcc of MCP2515 and Ground to GND of UNO and MCP2515
There's your problem. connect the external 5v+ power supply directly to the 5V pin or the USB input to the Arduino.
VIN pin connects to the input of the linear regulator. The output to that regulator is 5V, which powers the microcontroller. The input range of the regulator connected to the VIN pin is roughly 7-12V on most UNO's. 5V into the linear regulator is too low.
Blink Receiver Code
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
struct can_frame canMsg;
MCP2515 mcp2515(10); // chip select pin 10
void setup()
{
pinMode(7,OUTPUT);
Serial.begin(9600);
SPI.begin(); //Begins SPI communication
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
}
void loop() {
int a;
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
{
if (canMsg.can_id == 0x10)
{
a = canMsg.data[0];
}
}
if(a == 0)
{
digitalWrite(7,LOW);
}
if(a == 1)
{
digitalWrite(7,HIGH);
}
}
Blink Sender Code
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
struct can_frame canMsg1; //LED ON OFF
MCP2515 mcp2515(10); // chip select pin 10
void setup()
{
Serial.begin(9600);
SPI.begin();
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ);
mcp2515.setNormalMode();
canMsg1.can_id = 0x10;
canMsg1.can_dlc = 1;
}
void loop()
{
canMsg1.data[0]= 1;
mcp2515.sendMessage(&canMsg1);
delay(1000);
canMsg1.data[0]= 0;
mcp2515.sendMessage(&canMsg1);
delay(1000);
}
Thank You very much sir,
I will try out and come back to you,
Thank you once again
God bless you
On an Uno, Mega or Leonardo, in that scenario it's not advisable to connect 5V and USB at the same time.
One potential issue with this is that the variable a is uninitialised.
So your tests later on might result in false 0 or 1 evaluations for that variable. The beliw will remove that risk
void loop() {
int a = -1;
That's true. However, I did not suggest to supply 5v+ to both USB input and 5v pin simultaneously.
(edited the post for further clarity)
I know that you did not suggest that, it's more of a warning to OP.
Thank you sir
Sir, I checked practically and now the project is working absolutely fine. It was the problem of the Power supply connection. Thank you sir.