Model railway - Hardware
Control device
The heart of a digital model railway is a control device, which creates the elecrical signals to power and control the trains. Digital starter packs come with such a device, but it's usually only good enough for controlling a few trains. Only the most expensive packs come with a device that has a computer connection. Fortunately, there's a number of control devices on the market with varying feature sets, ranging from mid-priced to expensive.
My starter pack in 2006 came with a Märklin Mobile Station, which is a small handheld device capable of controlling ten trains. I wouldn't try keeping more than two in motion at once though. I quickly upgraded to an Uhlenbrock Intellibox, one of the more reasonably priced control devices available. It didn't have a lot of fancy features, but I was going to implement those myself on the computer anyway. I was happy with it for several years, and it's still my backup control device.
Over the years my locomotive collection grew to include several locomotives with MFX decoders, a more sophisticated protocol not supported by Intellibox. These could still be controlled using the older Motorola protocol, but limited to 14 speed steps and up to four functions (as opposed to 126 and 16). In late 2010 I acquired a second-hand Märklin Central Station. It's a small computer in its own right, with a graphical touchscreen interface. Unfortunately it broke after only two years. I'm fairly sure the fault is in its firmware and not the electronics, but so far I haven't found a way in to debug and fix it.
Faced with the high prices of commercial MFX-capable control devices again, I decided to build my own. The hardware side turned out to be surprisingly simple. Since the track always receives full voltage and the digital signal is encoded with polarity switches, a microcontroller and an H-bridge is the minimum requirement. I also included a current sensor and some status leds. A rectifier bridge is used to accept low-voltage AC from common model railway transformers, and a regulator provides power for the microcontroller.
Feature-wise, my custom device is incredibly basic. It only acts as a protocol encoder and has almost no long-term state. It also has no physical inputs. As such, it won't be any good on its own. Packet sequencing and other higher level functions are implemented in the PC software, which continuously communicates with the device. At some point I might create an add-on module that turns the device into a standalone controller.
Track circuits
To implement traffic control, the system needs to be kept informed of the positions of the trains at all times. The primary method I'm using for this is track circuits. Märklin has their own special circuit tracks, but they are rather expensive and only available in one form. Fortunately, it's easy to modify any C-track piece to be used as part of a track circuit by cutting the metal conductor conneting the rails at both ends of the track (see pictures below). The modification is completely invisible from the topside of the track and does not prevent using the track for other purposes.
The principle of track circuits is to isolate the rails from each other and adjacent tracks so that only one is connected to ground. The other is connected to a sensor box, such as Märklin's s88. When a train enters the sensor track, its axles connect the isolated rail to ground, completing the circuit with the sensor box. Multiple modified tracks can be chained together to form a single large sensor block. This improves reliability, since some wagons are not terribly heavy and may occasionally not make contact with one axle. Longer wagons could also have a wheelbase long enough to fit a single-track sensor between axles. Thus the sensor block must be able to hold enough axles that at least one of them has good contact.
Infrared gates
To supplement track circuits, I also use infrared gates. They consist of an IR led on one side of the track and a matching phototransistor on the other side. When a train passes between the two, the beam is broken and the phototransistor ceases conducting. The advantage is that an IR gate occupies a very short length of track, allowing more accurate detection in places that can't fit a track circuit. One such situation at the ends of a station platform or siding, where IR gates can be used to reduce the necessary safety zone. The most prominent disadvantage is the need for extra components, which do not come for free.
Wireless sensor system
On a large layout, the wires connecting individual sensors to sensor boxes quickly become unmanageable. I've developed a wireless sensor system to reduce the clutter. It consists of a receiver module plugged to the end of the s88 bus and one or more transmitters collecting sensor data from around the layout. As a bonus, it's even cheaper per sensor than Märklin's official s88 modules.
Turnout control
All of Märklin's C-track turnouts can be retrofitted with electric turnout mechanisms, which are hidden under the track base. Most can also be retrofitted with similarly hidden digital turnout decoders. However, the three-way turnout has no room for the twin decoders that would be needed. Thus, I'm using Uhlenbrock's "magnetartikeldecoder" to control that one. It fits between two adjacent tracks and does not look too terrible.
The turnout mechanism contains two microswitches to cut power when the actuator reaches the end of its travel. These are prone to failure after an extended number of cycles. A failed turnout mechanism can cause a train to go the wrong way, potentially causing a collision. With my own control device I'm able to detect turnout operation from the current spike caused by the solenoid. If the spike does not occur shortly after sending the switching command, the turnout is deemed to have failed.
Schematics and firmware
Schematics and firmware source code for all of my custom hardware can be found in a Git repository They are distributed under the GNU General Public License..