Control Accessories via the CAN-BUS.

Control Accessories via the CAN-BUS.

Postby MartinViljoen » Mon Mar 09, 2015 4:42 pm

thumbnail.png




Say you want to install aftermarket lights and you want to be able to turn them on using the existing factory light switch.

With the below setup you can.
You need to use a CAN-BUS Shield , and I recommend the Seeed Studio, It has served me well and is very reliable.
I'm using an Arduino UNO attached to a Seeed Studio CAN-BUS Shield, and the Arduino IO pins are then connected to an 8 Channel Relay Board.

It monitors the CAN-BUS until it sees the message that's responsible for turning on the main beams.
Once the message has been detected it will DigitalWrite to an IO pin of your choice.

Off coarse the CAN-BUS Shield is making use of some of the Arduino's IO pins so you are limited to the available pins.
But you will at least have 5 IO pins at your disposal (If not more), look on the CAN-BUS shield to see which pins are used by following the PCB tracks towards the pins.




Below is the code i used, Since the CAN-BUS messaging system is not standard across the board you obviously need to find the message first.
I found these messages with the Super-Sniffer Software. http://techtinker.co.za/forum/viewtopic.php?f=14&t=18

Code: Select all

#include <SPI.h>
#include "mcp_can.h"

INT32U canId = 0x000;

unsigned char len = 0;
unsigned char buf[8];
char str[20];

String CanMessage="";
int HeadLightsDetected=0;
int RelayCHN01=7;

void setup()
{
     //INIT RELAY PIN
     pinMode(RelayCHN01, OUTPUT);
     //TEST RELAY PIN
     TESTRElay();
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_125KBPS))
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}


void loop()
{
    if(CAN_MSGAVAIL == CAN.checkReceive()) 
    {
        CAN.readMsgBuf(&len, buf);
        CanMessage="";
        canId = CAN.getCanId();
       
        //Detect Main Beam HeadLisghts
        if (canId==680)
        {
          //Build Complete Message Without CAN ID From BUS
          for(int i = 0; i<len; i++)    // print the data
          {
            CanMessage = CanMessage + buf[i];
          }
          //Check Main Beams / Brights
          if (CanMessage=="3200000"){HeadLightsDetected=1;}else{HeadLightsDetected=0;}
         
          if (HeadLightsDetected==1)
          {
            //PUT CODE HERE To TURN ON Transistor/Relays
            Serial.println("Brights turned on!!!!!");
            digitalWrite(RelayCHN01, HIGH);
          }
          else
          {
           digitalWrite(RelayCHN01, LOW);
          }
        }
 
    }
}

void TESTRElay()
{
digitalWrite(RelayCHN01, HIGH);
delay(200);
digitalWrite(RelayCHN01, LOW);
delay(200);
digitalWrite(RelayCHN01, HIGH);
delay(200);
digitalWrite(RelayCHN01, LOW);
delay(200);
digitalWrite(RelayCHN01, HIGH);
delay(200);
digitalWrite(RelayCHN01, LOW);
delay(200);
digitalWrite(RelayCHN01, HIGH);
delay(200);
digitalWrite(RelayCHN01, LOW);
delay(200);

}


Something about the code you should note.

Depending on your read interval it will turn on and off the relay all the time, If not done as I did in the above mentioned code by first detecting the message using variable.
HeadLightsDetected

The message comes from CAN-ID 680, and will send 0000000 when off and 3200000 when on.
ID 680 gets broadcasted all the time followed by the state of the switch 00 or 32,

So when If finds ID 680 and 32 it will turn on and it will turn of if it found 680 and anything else. (Hope this makes sense)

Here is a Youtube video of it in action.
https://youtu.be/YK2GefWiG4w

An example application where this could be used.
ExampleProject.png
You do not have the required permissions to view the files attached to this post.
Image
User avatar
MartinViljoen
 
Posts: 109
Joined: Mon Oct 06, 2014 5:23 pm
Location: Centurion South Africa

Return to OBD2 & CAN-BUS Projects

Who is online

Users browsing this forum: No registered users and 3 guests

cron