Using Arduino Uno R3 with Inland CANbus Shield (Micro Center)
microcenter. com/product/623880/inland-ks0411-can-bus-shield
I have purchased, separately, an OBD2-to-DB9 cable and confirmed the pinout at both ends.
I used SparkFun's CANbus tutorial to find this Github Repository.
Generic 'CAN Read Demo' Sketch
/****************************************************************************
CAN Read Demo for the SparkFun CAN Bus Shield.
Written by Stephen McCoy.
Original tutorial available here: http://www.instructables.com/id/CAN-Bus-Sniffing-and-Broadcasting-with-Arduino
Used with permission 2016. License CC By SA.
Distributed as-is; no warranty is given.
*************************************************************************/
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
//********************************Setup Loop*********************************//
void setup() {
Serial.begin(9600); // For debug use
Serial.println("CAN Read - Testing receival of CAN Bus message");
delay(1000);
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);
}
//********************************Main Loop*********************************//
void loop(){
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
//if(message.id == 0x620 and message.data[2] == 0xFF) //uncomment when you want to filter
//{
Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
Serial.print(message.header.length,DEC);
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data[i],HEX);
Serial.print(" ");
}
Serial.println("");
//}
}}
}
'SparkFun CAN Demo' Sketch
CAN-Bus Demo
Toni Klopfenstein @ SparkFun Electronics
September 2015
https://github.com/sparkfun/CAN-Bus_Shield
This example sketch works with the CAN-Bus shield from SparkFun Electronics.
It enables the MCP2515 CAN controller and MCP2551 CAN-Bus driver, and demos
using the chips to communicate with a CAN-Bus.
Resources:
Development environment specifics:
Developed for Arduino 1.6.5
Based off of original example ecu_reader_logger by:
Sukkin Pang
SK Pang Electronics www.skpang.co.uk
This code is beerware; if you see me (or any other SparkFun employee)
at the local, and you've found our code helpful, please buy us a round!
For the official license, please check out the license file included with the library.
Distributed as-is; no warranty is given.
*************************************************************************/
#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);
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'){
Canbus.ecu_req(VEHICLE_SPEED, buffer);
Serial.print("Vehicle Speed: ");
Serial.println(buffer);
delay(500);
}
else if (UserInput=='2'){
Canbus.ecu_req(ENGINE_RPM, buffer);
Serial.print("Engine RPM: ");
Serial.println(buffer);
delay(500);
}
else if (UserInput=='3'){
Canbus.ecu_req(THROTTLE, buffer);
Serial.print("Throttle: ");
Serial.println(buffer);
delay(500);
}
else if (UserInput=='4'){
Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
Serial.print("Engine Coolant Temp: ");
Serial.println(buffer);
delay(500);
}
else if (UserInput=='5'){
Canbus.ecu_req(O2_VOLTAGE, buffer);
Serial.print("O2 Voltage: ");
Serial.println(buffer);
delay(500);
}
else if (UserInput=='6'){
Canbus.ecu_req(MAF_SENSOR, buffer);
Serial.print("MAF Sensor: ");
Serial.println(buffer);
delay(500);
}
else
{
Serial.println(UserInput);
Serial.println("Not a valid input.");
Serial.println("Please enter a valid option.");
}
}
}
It does not seem to be working. I have tried both sketches with no luck.
I tried baud rates of 125k, 250k, 500k. No change.
In tinkering with the code, it seems the buffer is empty.
Interestingly, when I plug it into my car (tried 2 cars now) it turns on weird lights on my dash. On my 2008 Tundra, it turns on the Traction Control light and another one. On my 2010 Hyundai Elantra, it turns on the Electric Power Steering and Battery lights.
On the Hyundai, these lights go away when unplugging OBD2. On the Tundra the lights turn off when power cycling the vehicle.
Not sure if this is an indicator that some wiring/pins are incorrect.
I have read some suggestions that maybe the crystal oscillator in the Inland CANbus shield is not the expected 16mHz frequency as the SparkFun (or others) shields.
When inspecting mine, it does not have the typical silver, oval shaped oscillator (maybe the size of 2-3 grains of rice) that the Arduino and others have. It seems to be much smaller, maybe about 1/2 grain of rice with no useful markings.
Any help would be appreciated.