Skip to main content

Raspberry Pi CAN Bus



So, I needed to setup a CAN Bus so that I could start writing some software simulators of CAN Bus devices. I required a complete and functional CAN Bus of my own so that I could add devices to it for testing. Those devices will be software simulated devices.
I searched the internet, but, I couldn't find anything that fit my project entirely. There were lots of sources that provided little bits, so, I wrote this blog for other people needing this particular scenario.


Raspberry Pi's

I decided to use Raspberry Pi's as the software simulators that I write can be used directly on the Pi. I couldn't do this with Arduino's. (I don't think so anyway)

I have 2 Raspberry Pi 3B's and I flashed the Micro SD cards with the latest version of Raspbian.











As of 30th September 2018 this was 2018-06-27-raspbian-stretch.img


RPi VNC

I like to setup up my raspberry pi's so that I can access them through VNC as soon as possible as my monitor is my HD TV so the longer the monitor is connected to my Raspberyr Pi, the longer I am not watching TV :) 
Create a VNC cloud account. It is free for non-commercial use up to 5 devices. Once setup you can access your raspberry pi from any way as long as you have the VNC app installed on you PC, Phone or Tablet. https://www.realvnc.com/en/

On first boot of the Raspbian image follow the setup to connect to the internet and update the image.
When finished updating, (this could take about 15 mins), it will ask you to reboot.
After the reboot, open a command shell and type:

sudo raspi-config

Change the hostname to something relevant. I will use 'piblue' in this example, so, in 2. Network Options choose Hostname and change the hostname. You will see that in the VNC software the cloud part recognises the hostnames and displays these in the VNC app for you to choose.

Then in 5. Interfacing options enable VNC

Click Finish and reboot

After your raspberry pi has rebooted, there will be a VNC icon on the top taskbar. Right click this icon and select open. Then log in to your VNC account and select 'direct and cloud connectivity'.
Once this is done, shutdown your pi, disconnect the HDMI cable, keyboard and mouse. Place your pi somewhere safe and power back up.

Using VNC Viewer app on your PC login to pi.


Update

I always want to make sure my pi is up to date so I do the following to make sure.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade


CAN Bus setup

Install the CAN Bus uitils library

sudo apt-get install can-utils

Then you need to setup the SPI interface for the CAN bus. Edit the following file:

sudo nano /boot/config.txt

Uncomment the following line by removing the hash:

dtparam=spi=on

Add the following lines under section that starts with #Additional overlays ......

dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi1-1cs

To save this file use CTRL + O and then press return to accept the default file name and path,
and then use CTRL + X to exit nano

Shutdown your pi so that you can setup the hardware side.


Hardware

So, now the hardware part. This is the bit that gave me the most trouble.
It has to right otherwise the can bus won't initialise.

The MCP2515 devices that I bought off eBay need modifying.
There are a few sources for this information, but, here it is again.

The transceiver chip TJA1050 on the board will not operate correctly with 5v, so,
because the board only comes with 1 5v Vcc pin you have to create another. 3.3v pin.

I followed the instructions here:
http://cosmostreamer.com/wired/howtomake

Once this is done connect the MCP2515 board as follows:

MCP2515 Raspberry Pi
------------- -----------------
Vcc 17 (3V3)
TJA 1050 VCC 2 (5V)
GND 25 (GND)
CS 24 (SPI0_CEO_0)
MISO 21 (SPI0_MISO)
MOSI 19 (SPI0_MOSI)
SCK 23 (SPI0_SCLK)
INT 22 (GPIO25)

Once you are confident that the wiring is complete you can turn on your pi.
Open a shell and type the following:

ifconfig

This will show you that there is no can0 interface
Now setup the can0 interface by using this:

sudo ip link set can0 up type can bitrate 500000

If an error message returns like 'Cannot find device "can0"' then the wiring is probably wrong.
If the cursor returns without error then do ifconfig again to check that the
can0 interface has been created

ifconfig

Check that the can-utils commands are recognised. You should be able to use the cansend command to send data onto the CAN bus:

cansend can0 127#DEADBEEF

You should also check the candump and cansniffer commands are recognised

candump can0

cansniffer can0

You won't see any data traffic at the moment as the CAN bus is
only one device.

The link is only temporary at the moment. If you rebooted
the can0 interface would not be there. To make it permanent edit the following file:

sudo nano /etc/network/interfaces

Add the lines below to end of the file:

auto can0
iface can0 can static
bitrate 500000

When you reboot now the interface will remain permanently.

The CAN Bus



Resources:

https://chadgibbons.com/2016/01/08/can-bus-hacking-with-the-arduino-and-raspberry-pi/
http://cosmostreamer.com/wired/howtomake
https://vimtut0r.com/2017/01/17/can-bus-with-raspberry-pi-howtoquickstart-mcp2515-kernel-4-4-x/
https://www.electronicshub.org/arduino-mcp2515-can-bus-tutorial/



Comments