Ryan Herbst

Train Set – Electronics & Control Software

Although I enjoy working on the scenery and structures on the train set, I mostly enjoy building the electronics and writing the software required to operate the train set. This page describes the electrical structure of the train set and the software used to control it.

When the train set was originally built it consisted of a classic control structure where each locomotive was controlled using a locomotive controller which put out a variable voltage to control the speed and direction of each train. A series of switches were used to connect the various track sections to one of the two locomotive controllers. The turn outs were controlled by a set of push buttons which set the thrown/closed state of each turnout. Although this setup worked well at first it was not easily adapted for computer control.

I decided to bring my train set into the 21st century by converting it to DCC. DCC stands for Digital Command Control. DCC replaces direct wiring with a more intelligent method for controlling trains. With DCC the entire track is powered with a constant DC voltage while a carrier signal is sent to a small IC which is implanted in the locomotive. The IC in the locomotive receives the command signals and controls the speed of the locomotive locally using Pulse Width Modulation (PWM). The IC will switch the power from the track on and off in order to form a square wave to the electric motor in the locomotive. This square wave has a fixed frequency and a varying duty cycle. The duty cycle of this square wave is what sets the speed of the locomotive.

There are a number of brands of DCC systems on the market. After doing some research I decided to use a DCC controller made by Digitrax. The reason I chose Digitrax is because their systems have a nice control network, called LocoNet, which is used to link their throttle modules, the track power supply and other control components together. The physical layer of LocoNet consists of three signal pairs allowing CAT5 cable to be used for wiring. The higher level protocol which runs on LocoNet is very simple and can be implemented in a software stack on a PC, in an embedded micro-controller or even directly in hardware. Digitrax has made this protocol public to allow model train enthusiasts to build their own LocoNet equipment or customized control software.

The following block diagram shows the electrical structure of my train set:

Block Diagram

Block Diagram

The heart of the system is the Digitrax DCS100 DCC command station. This command station provides the power to the train track while converting the LocoNet commands received from the throttle & control software to DCC messages to the locomotives and the DS54 Quad Stationary Decoders. The track power and DCC signals are distributed to the train tracks and the stationary decoders using a pair of 18 gauge wires. These wires are connected to the tracks at multiple locations, at least two per track segment, in order to ensure that there are no dead spots on the track.

There are three Digitrax DS54 stationary decoders located around the train set. Each of these DS54’s has four control outputs that can be used to control turn-outs, lights or other accessories. These output signals are controlled by DCC switch control messages. The DS54’s also have 8 general purpose inputs that detect a high or low state depending on the voltage applied to each input. A change in state on any of these inputs will generate a input state message on the LocoNet.

My train set has 10 turn-outs at various locations around the train set. Most of these turn-outs consist of a single track switch that is connected directly to the output of the quad decoder. Two of my turn-outs are made up of two switches that operate in parallel. Due to current drive limitations of the quad decoder these two turn-outs can not be connected directly to the quad decoder outputs. Instead the quad decoder output drives a relay which controls power to the two parrallel switches.

The quad decoders are also connected to 12 optical position sensors that are located at various locations around the train set. These optical sensors consist of a photo transistor circuit that normally holds the quad decoder input in an off state when the photo-transistor is exposed to light. When a train car or locomotive passes over the optical sensor the quad decoder input goes to an on state. There are three spare input signals that can be used for additional optical sensors if I need them.

In addition to the turn-outs and position sensors the quad decoders are also connected to a rail road crossing signal, a track status signal, and a series of push buttons and toggle switches which make up a control panel. Currently the control panel has no purpose but I plan to use it in the future to control certain operations of the automatic control software. The control panel also has a pair of LEDs, one green and one red. These LEDs will be used to indicate the current status of the control software.

The following picture shows the control panel:

Control Panel

Control Panel

When not using the computer to control the train I can use my hand-held throttle to control the speed of the train and the operation of the switches. This hand held control is the DT300 throttle made by Digitrax. This throttle has a nice long cord which allows me to move to every section of the train set without putting down the throttle. There are also infrared and radio versions of this throttle available.

The connection to the computer is made through a device called a LocoBuffer which was designed by John Jabour. This same gentleman also created a device called a LocoIO which can be used as a replacement for the Digitrax Quad Decoder. The LocoBuffer allows a piece of software on a PC to send and receive LocoNet messages without the need to worry about the physical layer & timing requirements of the LocoNet. The LocoBuffer will handle the collision detect and re-transmission functions required when transmitting LocoNet messages.

The LocoBuffer is connected via serial line to my server which runs the Debian Distribution of Linux. On this server runs a piece of software I wrote called Lnetd. Lnetd is a daemon that runs continuously on the server and interfaces directly to the LocoBuffer. This daemon listens for incoming TCP connections on a specific server port. Clients connected to this TCP port will be able to send and receive LocoNet messages through the LocoBuffer. The use of this daemon allows multiple clients to send and receive LocoNet messages through a single LocoBuffer. The protocol I use for sending LocoNet messages over TCP is known as LocoNet Over TCP. My daemon follows this standard protocol for the most part with a few minor changes. More information about my Lnetd software, as well as the source code can be found here.

The client software which controls the train set runs on a PC connected to my home network. This software consists of a LocoNet API package written in Java, a standard Java GUI & some wrapper software specific to my train set. The LocoNet API & Java GUI software packages were written by me. These packages are written as general support packets which can be used by anyone for their own personal train set.

This is a screen-shot of my control software:

Control Software

Control Software

The control software serves a number of purposes. Mainly it allows me to control the train set with a nice GUI which shows me a map of my track. This map shows the state of all of the turn-outs and shows the position of the trains as they pass over the optical sensors. The software GUI and the hand held control can be used at the same time as I switch between controlling the train set while sitting down to standing at track height watching the train operate up close. The main feature of the control GUI is the automation it brings to my train set. The control GUI tracks the position of the two trains, operates the rail road crossing signal when necessary & detects when two locomotives are about to collide. The collision avoidance feature is important because my train set has two locations where tracks cross at the same grade.

Electronics Links

Archives

All entries, chronologically...