What does an Automation Specialist do around xmas?

Fun

He automates his Model Train…

Märklin Z Train with 1 EUR coin size comparission

It’s Xmas time and it’s time to partake in some seasonal activities. For me (and a lot of others), this means indulging in a bit of good old-fashioned nostalgia where I unpack my model train set and reminisce about the good old times before Taxes, Posts and Work. Automation is my field, and I enjoy tinkering with electronics so, this led me to wonder whether I could indeed automate some of my model train functions. I own a Märklin gauge Z. Gauge Z is VERY small, and therefore ideal as it doesn’t take up much space for a nice setup, however, because it’s so small there is also no space for any modern automation equipment inside the Engines as you can find in Gauge N and H0 trains. I read that someone started to use HC-05 modules (Bluetooth) in the Engines directly, which is pretty impressive but…that would mean investing a lot of $ and retiring my old set which as you can appreciate isn’t an option, so that’s not a solution for me. Researching a bit more showed me that there aren’t really any posts about the topic and anyone who knows me knows I love to be challenged ….so the challenge was set:

  1. could I automate my train set

  2. and if I could how far could I go….

Setting the Stage

Before we go and choose any equipment that can be used to control the model train we need to know more about what electrical properties are involved. The Märklin Z train sets are controlled with a Transformer that says it has an output of 10V, however, a quick multimeter check reveals that it delivers DC 16V without a load. The Märklin Z Engines are rated to about a maximum of 12V and should normally not run at more than 10v to increase lifespan. The 16V drops to a maximum of 12V when the train is running on my track, which is inside the specifications. Switch Points, signals, lights or other equipment are connected to the transformer via AC 10V. Both are documented as 8VA.
This is mostly good news, as controlling DC is pretty straightforward and easy. The AC component for the Switches is a bit different and will be discussed in an additional post.

If you want to run two trains on the same track, you will have to use Switch Points and Interrupters and such to automate the whole lot. Don’t get me wrong, I love the manual bit, but I wanted to see if I can automate it and run multiple trains on the same track and do even more fancy stuff.

The tool of choice for automation is Arduino. If you haven’t heard about Ardunio yet, it’s pretty fun. Basically, they are microprocessors that are cheap, easy to program and you can attach all kinds of sensors and gadgets. An Arduino Nano will cost you around 25 EUR for 3 (or one for 10EUR). A Nano is not state of the Art and not very powerful but for the first steps, it’s fine. I will use a Nano for my first builds as well as Nano with an integrated Bluetooth (HC-05) module. Later I will probably use an ESP32 that comes with Bluetooth and Wifi and a lot more power. The Nano is my tool of choice for experiments, as I have a lot of Nanos laying around and if my experiments kill one, it’s not really tragic.

 

Getting started with Arduino

If you are interested in Adruino and would like to learn I would suggest the Elegoo Uitmate Starter Kit that comes with a UNO (much more power than a Nano and a good selection of sensors and other fun stuff to tinker around with and also contains an easy-to-understand guide with a lot of examples. That’s the Kit I started out with some 4 years ago and haven’t regretted it. The Arduino Uno is a bit dated now and the ESP32 is better and has more to offer…but well…this set is a classic.

As an alternative, I would recommend the Freenove Ultimate Starter Kit which comes with an ESP32 and as well a lot of extras.

There is also the Arduino Website and one of many ESP32 sites which contain a lot of links and information.

 

But back to the main topic…..

The first project is to get the Train moving.

Making the train run is properly the most important thing, but it’s not all about that. I wanted to make sure that I could switch directions and control the speed. I used an L298N Motor Driver Controller Board (3 for 10EUR) to set the direction of the train as well as its speed. The L298N regulates input voltages and can use a frequency input (PWM) to regulate the voltage. This made it the ideal tool to use for this project. For testing, I also used the 5V regulator to power the Nano (Pls see notes below).

For this first experiment,I kept it extremely simple and just attached the L298N to the Nano, put in a small programme and gave it a go. Here is what I did: and here are the steps if you would like to try it out for yourself.

Wire diagram to control Model train using an L298N

  • Stick the Nano on a Breadboard and attach it to your computer’s USB.

  • Use Arduino IDE to upload the code to it.

  • Build up a track loop and clean the tracks and the Engine.

  • Wire up the Transformer to the L298N, the Nano and the track as seen in the schema.

  • Pull the Transformer to the RIGHT to full power and watch it GO!

Some comments on the wiring and on the code:

  • As the transformer switches GND and 16V depending on which direction you turn the control, we have to make sure to only turn it to the RIGHT, as this would then power the L298N internal 5V converter and also the Arduino.

  • PIN 3 and 5 are digital frequency out ports (PWM). You can see the PIN out of the Nano here.

  • The L298N has TWO drivers so you could hook up another track on the other side.

  • If you don’t crank up the Transformer the train will not go fast and/or the Nano might not have enough Voltage to work (min 3.3V). Alternatively, you can use a external power supply. However, as the Transformer also gives us the AC for the Switch Points it’s a better choice than the transformer.

  • The L298N will drop about 2V between input and output, so the train will not reach max speed.

  • The manual of the L298N recommends a maximum of 12V DC on the input, however, 46V is the absolute maximum rating so 16V without load is fine.

  • When running a train it takes 4V of the 16V and each additional train takes another 2V to run.

  • You can disable the jumper for the internal 5V, if you run more then 12V input into the L298N as the internal regulator is not rated above 12V. If the jumper is removed an external 5V input needs to be supplied.

  • In the code, I use D2 and D4 for the directions and D3 for the speed control so that I can use D4 and D6 for the second Directions and D5 for the second speed control later.

  • The code has a loop to ramp the speed up and down slowly (as in reality). The variable delayTime is used to define how slow the speed should be ramped up or down. Depending on the Engine (Steam, electric, old or new) this can be adjusted.

  • The initial speed setting is 30, as I found that the Train emits some strange and unpleasant noise if starting (or stopping) at 0.

  • GitHub for the Code: https://github.com/DanielLangenhan/maerklinZ

So far so good…

but as I said I love a challenge, so let’s see what’s next….

Previous
Previous

Use Bluetooth to control Märklin Z Model Train