This project will only cover a way to control the trains.
MARKLIN ANALOG CONCEPT:
An analog HO train consists out of a motor that will move the train over the tracks. The motor used in Marklin analog HO trains are universal (AC-DC motor), that said it can be powered by an AC- or DC-power supply. Another big advantage is that it has a high starting torque.
To control the train, a variable voltage is applied to the motor, through the rail tracks. Marklin uses a 3 rail system to power the motor.
To switch the direction of the train , Marklin uses a relay to switch the polarity of the stator coil.
In order to control the train with a computer I came up with a concept that uses RS485 to send and retreive data from the train.
One of my problems was that I also wanted to use the old rail tracks, that's why it was necessary to apply the communication and power supply on the same line. That's why I use RS485 combined with the necessary filters to power my trains on the same line as the RS485 bus.
Texas Instruments posted a document where they cover a concept to combine RS485 with a DC power supply with only 2 wires: https://www.google.be/urlsa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiHrceI_JDWAhXCfFAKHU0sAWQQFggmMAA&url=http%3A%2F%2Fwww.ti.com%2Flitv%2Fpdf%2Ftidu993&usg=AFQjCNE7Hq9cw3zu4q3g5tu-ir0BBcZS6w
I designed a PCB that seperates the RS485 data and power, controlled by an Atmega328P microcontroller.
The combined power and data bus is seperated by input filters, the RS485 data is converted to UART and read by the microcontroller. Power is used to power the microcontroller (converted to 5V) and to power a single channel DC motor driver (MAX14870) that will drive the motor of the Marklin train.
The motor driver can change the direction and speed(voltage applied) of the motor through PWM.
I use a Polulu board with the MAX14870 already soldered on, it's small and it can easily be put inside the train along with the microncontroller PCB. It also contains a feature to detect over-current drawn from the motor. One of the best features is that it is capable to deliver a continous 1,7A to the motor which is perfectly in the range of the Marklin motor. The input voltage is variable between 4 - 36Vdc.
I will power the rail tracks with 24Vdc, and each motor will draw around 900mA (@22Vdc). Even with a high ballast weight applied to the train , the current will never exceed 1,7A which makes the MAX14870 ideal.
A further improvement would be too integrate the MAX14870 IC into the design of the microcontroller board.
In order to change the direction I can use two concepts:
The first one is to change the stator coil of the Marklin motor with a permanent magnet. This method is used by a lot of DIY'ers that want to convert their analog Marklin trains to digital ones. Their are companies that sell magnets that can replace the stator coils. But it comes with a price tag, that I'd rather try to avoid. With this method the direction is changed by inversing the anker of the motor. This is done by the MAX14870 IC.
A second method is too keep the stator coils and switch between those two coils with a small relay. I use a micro relay (5VDC latching) from Omron which is half the price of a stator magnet replacement kid as mentioned above.
Both methods can be used with microcontroller board that I designed.
The dimensions of the PCB board had to be very small. Marklin trains in HO scale don't have a lot of space inside (because of the scale used). By removing the old relay of the stator coils I had enough space to put the microcontroller board (21mm x 28mm) and the Polulu board (12mm x 15mm) with an Omron micro relay inside the train.