Using Raspberry PI to control Märklin model railway – Part 1

For my latest project, the Mariannenbahn, I needed to find a way to control a Märklin model railway from different rooms. While this is obviously possible using multiple Märklin Central or Mobile Stations, it was not an option at the time as the project already hit its budgetary ceiling.

This solution is intended as a temporary replacement for an additional Märklin control device and research was done out of curiosity. Go and buy their stuff.

Thanks to an outstanding blog post on as well as Stefan Krauß’ homepage , a solid groundwork was already present.


The Märklin Digital Connector Box is a device containing a signal processor that generates the digital signal on the rails. It communicates with controllers, such as the Mobile Station , via CAN bus using a protocol defined and thankfully published by Märklin. It is therefore possible to use any device capable of transmitting data over CAN to control the model railway. Using a Raspberry PI together with a ready-made CAN extension module seemed like a good and safe solution, as various people already proved that this setup usually works.
Since it is usually common to use the Raspberry PI via SSH instead of a display connected, a piece of software taking commands via Ethernet and hand them over to the CAN bus is required. That way, it is possible to install the PI somewhere close to the Märklin Digital Connector Box and the railway layout. Connected to the LAN, it will take commands from any Ethernet-capable device such as personal computers or even smartphones. Gerhard Bertelsmann has written a wonderful C program called can2udp for exactly that purpose. It will hand over commands between Ethernet/UDP and the CAN bus.
As a final step, software to produce the actual commands to be transmitted to the Digital Connector Box is required. The most obvious solution available for this task is Rocrail.

CAN controller

After some research, I decided to get a PiCAN2 board form SK Pang Electronics . Getting it running is quite straightforward. On the downside, I found it inevitable to solder a 2-way header pin as jumper bridge to the board myself to be able to use the 120Ω terminator already present on the board.

Hardware installation

Activating the terminator resistance is described in the manual. Mounting the board is easy, it only fits one direction. It is possible to either use the DB9 Connector or the screw terminal on the PiCAN2 to connect it to the Märklin Connector Box. The pin assignment on the Märklin Connector Box is described by Stefan Krauß.

Software installation

I was using a Raspberry PI 2 running the latest Raspbian. I wasn’t able to get the PiCAN2 running with the latest (4.4.16) firmware though. I used rpi-update to go back to 4.1.21 which worked for me some time ago. I did not do any investigation on which exact version introduces the breaking changes. If you find out, please let me know. To install a working firmware, rpi-update can be used with the version’s commit hash:

Following the manual, the following three lines have to be appended to /boot/config.txt:

After a reboot, the CAN interface can be brought up.

See the troubleshooting section on information about how to verify the interface is up and running properly.


can2udp is a beautiful C program that hands over messages from Ethernet/UDP to CAN and back. The usage is:

Given the default setup, it is sufficient to call ./can2udp -f -v, which will bring up can2udp in foreground with verbose output. It will send a certain initialization package to the Märklin Digital Connector Box on startup.

Client software

Almost any literature on the topic is using Rocrail as client software. Due to the lack of any real alternative, it is what I am using too at the moment.

I do not really like Rocrail. It was supposed to be open source software licensed under GNUv2 until the owner apparently stared spamming ®s all over and shutting down their public GitHub repository, even admonishing others to remove their forks of the GNUv2 licensed code. To make matters worse, an annoying pop up dialog tries to lure users into buying a support key.

The setup described in this post simulates the use of a Märklin Mobile Station 2, so the setup for Rocrail is identically to that scenario and can be found on their webpage or on

Where to buy

I was able to get a Märklin Digital Connector Box (60113) together with the required Märklin 36VA Switched Mode Power Pack (66361) from a local retailer. Märklin appears not to be selling the Digital Connector Box anymore, but still its quite easy to get one on eBay or Amazon.


If the CAN interface is set up correctly, it will be listed in the output of ifconfig as can0. Both the bcm2835 and the mcp251x controllers must be listed in the output of lsmod. Errors in the initialization process will also show up in the output of dmesg. It might be wise to grep for bcm2835 there.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.