CAN-Bus with Raspberry Pi: HowTo/Quickstart MCP2515 Kernel 4.4.x+

In this HowTo I will show you how to connect a MCP2515 CAN-Bus module to your Pi and get it running.
I created this HowTo, because almost all other HowTos were outdated and not working with newer kernels and I want to help others with this information.
I am using a recent (on the date of this post) Raspbian image on a Raspberry Pi 2.

First of all run the following command on your Raspbian to check which overlay will work for your kernel:

pi@raspberry:~$ uname -a
Linux raspberrypi 4.4.41-v7+ #942 SMP Mon Jan 9 15:00:25 GMT 2017 armv7l GNU/Linux

It’s necessary to modify the CAN-module from ebay, because it has only one VCC pin, but the MCP2515 needs to be powered from 3V3 and the TJA1050 CAN-transceiver needs to be powered from 5V. Powering both chips from 5V would work, but then a level-shifter for the SPI would be needed. The Pis GPIO pins are NOT 5V tolerant.

In my case I cut a trace on the PCB and soldered a pin onto the trace to deliver 5V only to the TJA1050. Be sure to cut the trace before the capacitor (otherwise if there is no capacitor, add one (about 100nF maybe)).

Here is a closeup (pin 3 is VCC on the TJA1050):

Next connect the module:

MCP2515 Raspberry Pi
VCC 1 (3V3)
TJA 1050 VCC 2 (5V)
CS 24 (CE0)
SCK 23 (SCK)
INT 22 (GPIO25)

First update software and install can-utils:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install can-utils

To activate the driver for the MCP2515 you have to add a kernel overlay, to do so edit the /boot/config.txt

sudo nano /boot/config.txt

And add the following lines (for kernel 4.4.x+) (then exit with Ctrl+X):


Or for older kernels:


In my case the MCP2515 CAN module has a 8MHz quartz, so the oscillator value has to be two times the frequency = 16000000. (This is one of the caveats I ran into, this was the only way I got my module working).

Now reboot, after the reboot try to setup the the can interface:

sudo ip link set can0 up type can bitrate 500000

If no errors occurred, the can interface should be ready now.
Try to send some messages:

cansend can0 127#DEADBEEF

You can also sniff the CAN-bus using candump:

pi@raspberrypi:~$ candump can0
  can0  120   [8]  01 C0 FF EE 00 00 00 00

To make the CAN-interface permanent, add the following lines to /etc/network/interfaces

auto can0
iface can0 can static
	bitrate 500000

If you find any errors please let me know in the comments.

Homeautomation: programmable Buttons from the junkcontainer #2

Last evening I finished the case, electronics and software for the buttons.

I didn’t want to make my own pcb just for 9 buttons, so it was time for some reverse-engineering. Sure it’s nothing fancy, because the buttons are connected in a simple matrix.

To poll the button states I just needed 6 wires (the other jumper wires have already been there for the LEDs).

To control the LEDs too, I added some more wires.

The button pcb fits snugly in the 3D printed case. The white part is a cancelled print from the Makerbot. The orange case has been printed on our Prusa i3. I glued both parts together with superglue. As it turned out later, the case has the perfect depth for the electronics, without the cancelled print glued on, there wouldn’t have been enough space for the electronics.

As CAN module I am using a cheap MCP2515 CAN Module you can get very cheap from eBay. I just glued it to an Arduino Nano.

This DC-DC converter is configured to output 5V for the Arduino Nano. I removed the trimpot to save some space.

Next thing to design was a mounting plate. I didn’t want to print one, because it was late and I didn’t want to wait for a print to finish. So I just used our laser-cutter to cut the plates shown below. The left one is used as mounting-plate for the electronics and prevents short-circuits on the button pcb. The right one carries four M3 nuts so it can be screwed onto the case and also onto a wall or something else (in my case a bed).

The nuts didn’t fit very well, they were too tight, but it still works.

The final result:

I am pretty happy with it. I think I’ll redo the buttons some day, but for now it’s just fine.

Next step was to get the software up and running.  I am scanning each column. This is how it works:

  1. Set all row pins to input with pull-up high.
  2. Set column pin as output and write LOW.
  3. Check each row pins and write current state to button register.
  4. Reset column pin to input (high impedance)
  5. Go on to next column pin and finish after last column is done

The way the button matrix is connected, hazards are possible. Because of this, the microcontroller cannot readout multiple button presses at the same time. (In some cases it’s possible, but in others it’s not).

The LEDs were working (this is why I drilled holes in some button caps), but I’m not going to use them, because they’re not bright enough and it really doesn’t matter for me.

After having the electronics and software finished I moved on to the CAN module for my Raspberry PI 2. With a lot of hassle I got it up and running, it seems like there’s too much outdated information out on the internet. My problem was to get the MCP2515 overlay for Raspbian properly running. But since Kernel 4.4.x+ some things changed. (I got it working using this information here:

How to get the MCP2515 CAN Module running on a Raspberry PI on Kernel 4.4.x+ ???

I will cover this in an extra blogpost very soon…

Around at 4’o clock in the morning I got candump up and running to receive the can messages from my button controller:

You can see a short video below: