Märklin model trains, traffic automation and control

Hi all,

I have recently completed another model train control project. This time, it has to do with traffic automation.

The vintage Märklin H0 track system I use provides a series of special track sections (contact or switching tracks) that are used to control accessories, they work differently but the end result is the same: a passing train will close a circuit that establishes a connection to ground, which activates connected accessories (turnout, signal, light bulb etc...).

This is used to automate traffic. If planned well, one can get to a point where trains run themselves on a block system, but things get quite complex rapidly and generate a huge amount of wiring. To avoid collisions, the special tracks need to be placed strategically, most of the time several meters away from the target accessory. Wire spaghetti heaven! Additionally, things are not easy to re configure, as it usually involves a lot of re patching.

I had already developed a Nodered/Tasmota wireless solution for the manual controls of accessories, and I decided to expand the functionality a bit. I designed ESP8266/MCP23017 sensor modules to which all the contact and switching tracks are connected. The sensor passes all contact information via MQTT to Nodered. Nodered in turn processes the information and uses it to change signals and turnouts by sending relevant messages to the wireless switches controlling the signals and turnouts.

This eliminates the need for long wire runs, and makes the configuration much easier. on my current layout, I removed 75% (30m) of wiring, not a bad ratio.

I have designed a dashboard tab to manage all this, based on scenarios that use a different set of configuration data, so I am able to reconfigure the layout at the push of a button.

The nodered part was done with zero coding, using default NR nodes and parameters.

Here is a little YouTube video about this:

Thanks for your attention...


Woo - that is impressive.
Here's a link to a Block-Signalling system I created 2-years ago using Finite State Machines (FSMs).


very nice concept :slight_smile:

That's really cool. Nice job!

I'm not a model train guy myself but this reminds me of a friend back in '88 who was getting into Marklin trains. At the time we both had Apple ][ systems and he was pondering how he could connect the two. I never heard how that turned out but I suspect it went nowhere.

Thanks a lot! Good little project to keep the brain going.

In 1985, Marklin introduced their digital system, and at the same time, they provided (well, sold...) an interface module that could be connected to a computer using RS232.

It was meant to be used with BASIC, at least, that was the recommended way at the time and the manual gave a few examples to get people started.

Look at this sexy green screen, might even be an apple 2 screen...

1 Like

You got my attention, for sure.

My other indoor hobby is automating my model railway. Since we moved it's all in boxes, taken out only once a year or so to have a layout on the floor. But the wires are always a mess. I was following efforts on the net with wireless feedback sensors, but nothing so far has convinced me. So I stayed with the S88n based feedback.

I always thought that MQTT would be the perfect place to relay all feedback and commands. I'm using iTrain to control my railway, but I always wanted to start creating my own control sw with node-red. We'll see.

Thanks for sharing your work.

1 Like

Strange you should menton that, as I'm collecting my thoughts on using MQTT to control a model railway.

Before I retired I worked as a computer science and microelectronics lecturer. One of my tasks was thinking-up and running projects for the students. Over a period of time I got a series of students to build a model railway based on CAN bus (as CAN was used by the major employer who supported the college).
The speed of the train was controlled by PWM as applied to track's voltage.

I use Wemos D1 Mini (ESP8266) modules around my house for reporting temperature/humidity and for controlling relays and operating LEDs - all via MQTT mesages and Node-RED.

My idea is to modify the train's motor connection...

  • disconnect it from the track's pickup
  • install an ESP-01S (tiny microcontroller) in the body of the train or the tender
  • install an H-bridge chip and attached the motor to it

So the ESP-01S is powered from the track via an on-board voltage regulator. The (full) track's voltage is applied to the H-bridge with is driven by some pins on the ESP-01S (e.g. PWM and Dir).

I'm planning on working on this as a Winter-2022 project with a couple of my grandsons. I've done a few simple tests with a length of track and have got promising results. I implemented a similar PWM control system for a couple of slot racing cars way back in 2019 - which was just pure fun.

I should add that my projects are 'trivial' compared with @Mrb32 magnificent layout/system.

Here a photo of an ESP-01S (25mm by 15mm)

Hello Dave
The digital controllers from Märklin, Tams, etc contain highly sophisticated control logic to handle the motors. The coolest part is that they switch of the power to the motor, and then use the moving motor as a generator to obtain a speed measurement. And this is only a small part of the code in these controllers.
I advice against building your own motor controller for your railway, if you aim for a good result. It might still be fun doing it anyway.

1 Like

Glad I got your attention.

I wasn't sure about this either, based on views expressed in various parts of the internet... But you never know until you try something out by yourself.

I had also looked at using something like iTrain or JMRI + digital decoders before starting all this, but I found the control software too complicated for my needs and the digital decoders would have needed their own power circuit. All I needed at the time, was a way to retrofit manual controls to an existing automated layout with minimum fuss ie. no wires, so I developed wireless switches in way they could integrate in the märklin ecosystem (compact format, power from the existing 16VAC lighting circuit etc etc). That bit has been in operation for 2 years or so with no issues.

From the perspective of physical connectivity, I am not sure you'd save much wiring compared to your existing S88 set-up on the feedback side: you'd gain MQTT and wifi, but you'd still need a physical connection to each contact track, power for the feedback module, so the only thing you'd save is the S88 bus connection. but then any excuse to play with node-RED is a good one :slight_smile:

Thanks for your comments which I appreciate. This is just a fun project for my grandsons as a way of improving their programming (Python) skills and electronic engineering abilities. I might introduce the project to my IoT students although the issue might be finding a suitable room in the school where the track could be assembled and left from one week to the next.

@Mrb32 Very nice project, looks amazing.

@ 14:36 To get the text in the button you can use Ground<br>Loop to break the line in 2 in the label line.


Thanks! Someone has really been paying attention :slight_smile:

you show some custom pcb's with labels like U1, what chips are on those boards?

The sensor module used an ESP8266 and a MCP 23017 IC.

This is a very nice project, I must go back and watch the entire series from the start. Using scenarios is a great idea. I never used commercial train control software (like TrainController), I don't know how easy it is reconfigure the layout, but I am pretty sure it is not a single button click like in your case.

Thanks a lot! I think most specialised software has some form of itinerary feature, with all sorts of bells and whistles (pun...), far too elaborate and new to useable with my vintage analogue equipment.

I shall look for a bump in the channel stats :wink: