Thursday, 19 November 2015

Reading from CAN bus of a Chevrolet Cruze using an Arduino Uno


I'm using the Arduino Uno, CAN bus shield, and OBDII to DB9 Cable using their provided libraries. I was only able to initialize it.


Readings:


While connected to the car:



  • CAN-High: 2.7 V - 3.0 V

  • CAN-Low: 2.4 V - 2.7 V


While not connected to the car:




  • CAN-High: ~2.4 V

  • CAN-Low: ~2.5 V


Note: in both cases the Arduino is connected to my PC via USB (for debugging).


Sample code:


#include    //Software serial port
#include

#define CBRxD 5 // CAN BUS RX
#define CBTxD 4 // TX


#define DEBUG_ENABLED 1

char buffer[512]; //Data will be temporarily stored to this buffer before being written to the file
char tempbuf[15];
char lat_str[14];
char lon_str[14];

int LED2 = 7;
int LED3 = 8;


boolean scan = true;

SoftwareSerial canbusSerial(CBRxD, CBTxD);

void setup() {
Serial.begin(9600);
canbusSerial.begin(4800);
pinMode(0, INPUT);
pinMode(BTTxD, OUTPUT);

pinMode(LED2, OUTPUT);

pinMode(LED3, OUTPUT);

digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);

setupCanbus();
}


void loop() {
char recvChar;
if (canbusSerial.available()) { // Check if there's any data sent from the remote CAN bus shield
recvChar = canbusSerial.read();
Serial.print("CAN: "+recvChar);
}

if (scan) {
digitalWrite(LED3, HIGH);


if(Canbus.ecu_req(ENGINE_RPM,buffer) == 1) { /* Request for engine RPM */
Serial.println(buffer); /* Display data on Serial Monitor */
}
Serial.println(buffer);

if(Canbus.ecu_req(VEHICLE_SPEED,buffer) == 1) {
Serial.println(buffer);
}Serial.println(buffer);

if(Canbus.ecu_req(ENGINE_COOLANT_TEMP,buffer) == 1) {

Serial.print(buffer);
}Serial.println(buffer);

if(Canbus.ecu_req(THROTTLE,buffer) == 1) {
Serial.println(buffer);
}
Serial.println(buffer);
// Canbus.ecu_req(O2_VOLTAGE,buffer);

digitalWrite(LED3, LOW);

delay(100);
}
}

void setupCanbus() {
while (!Canbus.init(CANSPEED_250)) {
Serial.println("CAN Init");
}
Serial.println("CAN Init OK");
delay(1000);

}


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...