MCP2515 CAN Module Receiver not working

CAN SENDER CODE #include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication (GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library) //Library for using DHT sensor
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//#include <SPI.h>

struct can_frame canMsg;

int var1;
int var2;

MCP2515 mcp2515(10);

float m=-0.3376;//slope
float b=0.7165;//y intercept
float R0=2.82;//SENSOR RESISTANCE IN FRESH AIR

float m1 = -0.6527; //Slope
float b1 = 1.30; //Y-Intercept
float R01 = 7.22; //Sensor Resistance in fresh air

double ppm_1;
double ppm_2;

void setup()
{
while (!Serial);
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();

canMsg.can_id = 0x036; //CAN id as 0x036
canMsg.can_dlc = 8; //CAN data length as 8
}

void loop()
{
float sensor_volt_1; //Define variable for sensor voltage
float RS_gas_1; //Define variable for sensor resistance
float ratio_1; //Define variable for ratio

float sensor_volt_2; //Define variable for sensor voltage
float RS_gas_2; //Define variable for sensor resistance
float ratio_2; //Define variable for ratio

var1=analogRead(A0);
var2=analogRead(A1);

//Just for conversion
Serial.print("GAS SENSOR RAW VALUE OF MQ135 = ");
Serial.println(var1);
sensor_volt_1 = var1*(5.0/1023.0); //Convert analog values to voltage
Serial.print("Sensor value in volts = ");
Serial.println(sensor_volt_1);
RS_gas_1 = ((5.0*10.0)/sensor_volt_1)-10.0; //Get value of RS in a gas
Serial.print("Rs value = ");
Serial.println(RS_gas_1);
ratio_1 = RS_gas_1/R0; // Get ratio RS_gas/RS_air
Serial.print("Ratio = ");
Serial.println(ratio_1);
double ppm_log_1 = (log10(ratio_1)-b)/m; //Get ppm value in linear scale according to the the ratio value
ppm_1 = pow(10, ppm_log_1); //Convert ppm value to log scale
Serial.print("Our desired PPM for MQ135 = ");
Serial.println(ppm_1);

Serial.print("GAS SENSOR RAW VALUE OF MQ7 = ");
Serial.println(var2);
sensor_volt_2 = var2*(5.0/1023.0); //Convert analog values to voltage
Serial.print("Sensor value in volts = ");
Serial.println(sensor_volt_2);
RS_gas_2 = ((5.0*10.0)/sensor_volt_2)-10.0; //Get value of RS in a gas
Serial.print("Rs value = ");
Serial.println(RS_gas_2);
ratio_2 = RS_gas_2/R01; // Get ratio RS_gas/RS_air
Serial.print("Ratio = ");
Serial.println(ratio_2);
double ppm_log_2 = (log10(ratio_2)-b1)/m1; //Get ppm value in linear scale according to the the ratio value
ppm_2 = pow(10, ppm_log_2); //Convert ppm value to log scale
Serial.print("Our desired PPM for MQ7 = ");
Serial.println(ppm_2);

//canMsg.can_id = 0x036; //CAN id as 0x036
//canMsg.can_dlc = 8; //CAN data length as 8
canMsg.data[0] = ppm_1; //Update humidity value in [0]
canMsg.data[1] = ppm_2; //Update temperature value in [1]
canMsg.data[2] = 0x00; //Rest all with 0
canMsg.data[3] = 0x00;
canMsg.data[4] = 0x00;
canMsg.data[5] = 0x00;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;

if (mcp2515.sendMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.println("Messages sent");
}
else
{
Serial.println("Msg1 TX error");
}

}


**CAN RECEIVER CODE**
#include <SPI.h>              //Library for using SPI Communication 
#include <mcp2515.h>      //Library for using CAN Communication (https://github.com/autowp/arduino-mcp2515/)
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
 
//LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x3F for a 16 chars and 2 line display
 
struct can_frame canMsg;
 
MCP2515 mcp2515(10);                 // SPI CS Pin 10
 
 
void setup()
{
  Serial.begin(115200);                //Begins Serial Communication at 9600 baudrate
  SPI.begin();                       //Begins SPI communication
  Serial.println("CAN BUS STARTING");
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();//Sets CAN at normal mode
  //Serial.println("CAN WORKING");
}
 
 
void loop()
{
  Serial.println("Inside Msg Loop");
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) // To receive data (Poll Read)
  {
    int x = canMsg.data[0];
    int y = canMsg.data[1];
Serial.println("Messages Received");
 
    Serial.print("MQ135 CO2: ");         //Display Temp & Humidity value received at 16x2 LCD
    Serial.print(x);
    Serial.println("PPM");

     Serial.print("MQ7 CO: ");         //Display Temp & Humidity value received at 16x2 LCD
    Serial.print(y);
    Serial.println("PPM");
    
  }
  else{
    Serial.println("Messages not Received");
  }
}




MCP2515 at the transmitter side is able to send msg.But the issue is in receiver CAN where on powering up the control is not going to polling statement mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK.I am getting Messages not received at the output.

For further info,i have connected 
Vcc to 5V,GND to GND,CS to D10,SO to D12,SI to D11,SCK to D13,INT to D2.

Arduino board which holds CAN Tx is connected to external power supply and Arduino board which holds CAN Rx is connected using USB flashing Cable.


Please to respond as soon as possible

I do not follow word problems or frizzys. Can you post a simple schematic as to how it is wired, be sure to show all connections. Also be sure to show the termination setup you are using, if that is wrong it will not work. Is that two different sets of code posted?

It is important that you know a transmitter will not work unless its message is acknowledged, that is part of CAN protocol. There is no one way communications, it is a bidirectional bus and must operate that way for CAN to work.

Yes two sets of code is posted CAN transmitter and CAN receiver.The issue is CAN Tx transmits the msg but at receiver end i was not able to see beacuse polling fails.

mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK --> CAN Rx is not going inside this if loop and i am getting output as Message not Received(as per my code)

Edit your post using the :pencil2: in the tool bar just below your post

select the code part and press the <code/> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

➜ do yourself a favour and please read How to get the best out of this forum

1 Like

Hi, @sridhar_aptiv
Welcome to the forum.

Have you written your code in stages?
If so have you code that JUST communicates with each of the CanBus units, to prove your communication hardware?

How do you know your system is transmitting?

Have you tried the Tx and Rx example codes that come with the library?

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Please no Fritzy cut and paste images.

Thanks... Tom... :smiley: :+1: :coffee: :australia:

1 Like

I have included a Serial.println to check whether the control goes inside the loop or not.No i haven't tried Tx and Rx that comes with library.I am using Arduino Mega.Same way i have included Serial.Println to check whether the control goes inside mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK this loop or not.But control doesn't go.mcp2515.Only the else message gets printed.Yes i have tried Rx and Tx that comes with library.That too is not working

1 Like

Can I suggest that you do.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Please no Fritzy cut and paste images.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

3 posts were split to a new topic: MCP2515 Trouble

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.