1

I'm hoping to get some help with this as I am unable to get the CAN to Initialize.

I have the sparkfun Can-Bus shield: https://www.sparkfun.com/products/13262

and an Arduino Leonardo board.

I'm using the following code to try to connect my 2008 Audi S6 to the Arduino board:

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <Canbus.h>
char UserInput;
int data;
char buffer[456]; //Data will be temporarily stored to this buffer before being written to the file
//********************************Setup Loop*********************************//
void setup(){
Serial.begin(9600);
while(!Serial);
Serial.println("CAN-Bus Demo");
if(Canbus.init(CANSPEED_500)) /* Initialise MCP2515 CAN controller at the specified speed */
 {
Serial.println("CAN Init ok");
 } else
 {
 Serial.println("Can't init CAN");
 } 
 delay(1000); 
Serial.println("Please choose a menu option.");
Serial.println("1.Speed");
Serial.println("2.RPM");
Serial.println("3.Throttle");
Serial.println("4.Coolant Temperature");
Serial.println("5.O2 Voltage");
Serial.println("6.MAF Sensor");
}
//********************************Main Loop*********************************//
void loop(){
while(Serial.available()){
 UserInput = Serial.read();
if (UserInput=='1'){
 data=Canbus.ecu_req(VEHICLE_SPEED, buffer);
 Serial.print("Vehicle Speed: ");
 Serial.print(data);
 Serial.println(" km/hr ");
 delay(1000);
}
else if (UserInput=='2'){
 data= Canbus.ecu_req(ENGINE_RPM, buffer);
 Serial.print("Engine RPM: ");
 Serial.print(data);
 Serial.println(" rpm ");
 delay(1000);
}
else if (UserInput=='3'){
 data= Canbus.ecu_req(THROTTLE, buffer);
 Serial.print("Throttle: ");
 Serial.print(data);
 Serial.println(" %% ");
 delay(1000);
}
else if (UserInput=='4'){
 data =Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
 Serial.print("Engine Coolant Temp: ");
 Serial.print(data);
 Serial.println(" degC");
 delay(1000);
}
else if (UserInput=='5'){
 data=Canbus.ecu_req(O2_VOLTAGE, buffer);
 Serial.print("O2 Voltage: ");
 Serial.print(data);
 Serial.println(" V");
 delay(1000);
}
else if (UserInput=='6'){
 data=Canbus.ecu_req(MAF_SENSOR, buffer);
 Serial.print("MAF Sensor: ");
 Serial.print(data);
 Serial.println(" g/s");
 delay(1000);
}
else
{
 Serial.println(UserInput);
 Serial.println("Not a valid input.");
 Serial.println("Please enter a valid option.");
}
}
}

Overall, I just can't get the CAN to initialize. I've looked online and just can't seem to find a solid solution. I've tried other libraries as well to no avail.

Please assist!

asked Oct 27, 2015 at 23:53
2
  • Leonardo has SPI pins on the ICSP header. Your shield expects SPI pins on pins 11,12 and 13. Maybe this will help: seeedstudio.com/wiki/… Commented Nov 6, 2015 at 1:45
  • I got that far; The strange part is that even if I bring the SPI pins to pins 11,12, and 13 it still doesn't work.. are there more pins I need bring over? Commented Nov 7, 2015 at 14:01

2 Answers 2

1

Had this problem myself. Found this fixed it

Change the following line...

_delay_us(10);

To...

_delay_us(50);

In the MCP2515.c file in the library

jfpoilpret
9,1627 gold badges38 silver badges54 bronze badges
answered Nov 30, 2016 at 18:59
1

Increasing that delay in the MCP2515.c file also fixed this issue for me. I am running the sparkfun Can-Bus shield on an Arduino pro-mini, and here is my serial monitor output before the change, and after:

12:56:38.580 -> CAN Read - Testing receival of CAN Bus message 12:56:39.556 -> Can't init CAN 13:01:40.992 -> CAN Read - Testing receival of CAN Bus message 13:01:42.016 -> CAN Init ok

answered Oct 6, 2020 at 18:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.