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)
GND 6 (GND)
CS 24 (CE0)
MISO 21 (MISO)
MOSI 19 (MOSI)
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):

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

Or for older kernels:

dtparam=spi=on
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

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: http://www.embeddedpi.com/documentation/isolated-canbus/mypi-industrial-raspberry-pi-can-bus-card-configuration).

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…
https://vimtut0r.com/2017/01/17/can-bus-with-raspberry-pi-howtoquickstart-mcp2515-kernel-4-4-x/

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:

Homeautomation: programmable Buttons from the junkcontainer #1

I really like to take a look at the junk container to find something useful. This time I found a 3×3 button matrix partly containing LEDs you can see here below (I’ve already cut the pcb to fit my 3D printed case):

The buttons have a nice pressure point, I could play with’em all day long. I really like mechanical interfaces, because I don’t like touchscreens for simple interfaces. I don’t need to fire up my phone or have a look at the screen to see what I am touching. This way I can control the whole room without even having to take a look at the interface.

Next step was to take some measurements and create a 3D model, so I could see how my case would fit. For creating 3D models I got stuck with OpenSCAD (http://www.openscad.org/). I like programming/coding, so OpenSCAD is easy for me (although it get’s very complicated sometimes, but I like the challenge). To simplify the process I always draw the model on paper first before I create it with OpenSCAD.

First draft in OpenSCAD:

I wanted some keycaps, here they are:

Now I could design a case for it, here is the result in OpenSCAD:

And here is a 3D printed protoype (the orange case has been printed on a 3Dator, the white keycaps have been printed on our modified Makerbot, both in 0.1mm layer height):

Next steps are:

  • Design mounting plate
  • Fit a microcontroller with CAN in the case
  • Connect it to the CAN bus and program it 🙂

Continue reading https://vimtut0r.com/?p=85

 

Makerbot Replicator 2 Replacing Electronics #1

Hi there,

I’m working at the FabLab Darmstadt and we have several 3D Printers. One of them is the Makerbot Replicator 2. This 3D Printer did very good prints until the display failed. An unkown IC on the display pcb was just blown. I then uploaded a sailfish firmware for the Mighty Board with no display, tried to move one of the motors… Bang.. Magic smoke.  Tried another axis, same phenomenon.

I thought this might be a good point to convert the electronics to something open source. I wanted to have more control over the printer and be able to control it remotely via OctoPrint.

My hardware goals were the following:

  • Arduino Mega 2560 with RAMPS Board
  • DRV8825 with 1/32th stepping
  • Display
  • Raspberry PI 3 for running Octoprint
  • Multiple external USB ports to run multiple 3D printers with just one Raspberry PI 3 (I am going to write an extra blog post about this)
  • Reusing the original PSU

So now let’s have a look at the changes I’ve done to the Makerbot.

First thing I’ve done was a new custom backplate. It was  lasercut using our 50W CO2 Laser. The Makerbot now has an Ethernet Port, Powerswitch and Speakon for Power. I chose Speakon, because the connectors are locked and you can’t reverse polarity, although it’s a bit bulky (I know it’s not good to choose connectors which are meant for other purposes, I just hope no one is going to plug this one into a speaker). I disliked the original connector, because you could reverse polarity with just a little bit of force and even the design didn’t use a symmetrie of the connector (yeah because the PSU was off the shelf and so the pinout is fixed).

Next thing was to mount all pcbs. I just drilled some holes and used a lot of distance sleeves. In another HOWTO on thingiverse someone just hotglued all boards onto the metal plate, this is what I wasn’t going to do. To get the power up and running I am using a DC-DC converter to feed 5V to the PI 3. Also it’s very important to remove a diode on the RAMPS board which forwards the motor voltage to the Arduino’s voltage regulator. Because the Makerbot is running on 24V, I had to do this. The voltage regulator cannot handle 24V (only up to 20V). The Arduino is then powered via USB from the PI 3.

In case of maintenance I wanted something to prevent reversing the direction of the motors, so I added a lot of PSK connectors to the RAMPS board.

Here in the back you can see 3 USB ports for additional 3D Printers (or other USB stuff). The red button in the front is the reset button for the MakerBot’s Arduino, with the green button you can activate the access to OctoPrint. This was needed as a security feature, so no one with access to the Network can control the printer without accessing it physically.

You can find my Configuration.h for MARLIN on Github (this file is only useful for steps per mm, because I am not using the stock Makerbot Hotend):

https://github.com/vimtut0r/Marlin/blob/RC/Marlin/Configuration.h

Results:

Here you can see the result of the Makerbot Replicator 2 (white) compared to a print from a 3Dator (https://www.3dator.com) (orange).

Future goals:

  • Add controller for LEDs
  • Add camera
  • Heated bed (maybe automated bed?)
  • Filament sensor