Dear fellow hobbyists,
I have decided to develop a turnout decoder to suit my particular situation. I am a model railroader who does not have a fixed and permanent layout, instead I build my layout as I have opportunity and time. Of course, this means that no two layouts are the same and that individual turnouts (many of which can be controlled electrically) change places. To deal with this situation, I came up with a miniature decoder that is attached to each individual turnout and draws power and commands directly from the turnout's tracks.
Before I continue with the design, I would like to thank Geoff Bunza for his help and the many useful examples, Terry Chamberlain for his help in developing the solution, Alex Shepherd for help with coding, and many others not specifically mentioned here.
The current version of the decoder is 2.0.
Design
The basic requirements for the turnout decoder were:
- it must be small and extra thin to fit well with the turnout,
- it must be simple enough that a hobbyist with average soldering experience can build it,
- the components must be cheap and readily available.
Circuit diagram
Figure 1: The circuit diagram of the miniature turnout decoder.
My starting point was the examples from Geoff Bunza. To minimize the size, I first decided to use a single-sided PCB with surface-mounted components with leads and reasonable minimum pitch (≈ 1mm) and no component smaller than 2012. I also revised parts of the circuit diagram to minimize the circuit footprint. The final schematic is shown in Figure 1.
For the microcontroller unit I replaced Arduino Mini with the much smaller ATtiny85 (U2). The ATtiny85 uses the fantastic NmraDcc C++ library, which does all the heavy lifting like DCC communication and CV management. It only has five programmable pins, but since only one is needed for DCC communication and two for solenoid control, that leaves two that can be used for signal LEDs, jumper programming and debugging.
Four Schottky diodes D1 to D4 are used instead of a bridge rectifier. This reduces the circuit footprint and reduces voltage drop during rectification. They also ensure that no reverse current flows through the diodes when the DCC waveform changes polarity, so less DCC power is wasted due to square waves at a few kHz.
The smoothing capacitor C1 has a relatively large capacitance to smooth out large changes in current draw that can turn off the DCC command station.
Optional resistor R1 can be used to reduce current and voltage at the solenoids. Be careful that it does not draw too much current.
The microcontroller is powered by a very small linear regulator U1 using smoothing capacitor C2. Since the current flow of the solenoids is controlled by a dual MOSFET pack with very small circuit board footprint U3, the microcontroller does not require large currents. As a safety feature (see below), resistors R4 and R5 pull down the MOSFET gates. D5 and D6 are flyback diodes.
And last but not least, instead of an optocoupler, a voltage divider is used with resistors R2 and R3. This is a somewhat controversial decision. It greatly reduces the circuit footprint, but it is also potentially dangerous to the microcontroller. Although the ATtiny allows input voltages from GND-0.5V to VCC+0.5V, it also has protection diodes that attempt to clamp the voltage to this range. The current rating of these protection diodes is 1mA, which means that theoretically only (12V-5V)/1mA = 7kΩ gate resistance would be sufficient for protection in a 12V system. This is considered a safe solution by many electrical engineers, but to make things even safer I decided to use a voltage divider instead. The values of R1 and R2 are optimized for a 12V DCC system. Since the ATtiny will accept any value between 0.6*VCC = 3V and VCC+0.5V = 5.5V as logically high, these resistors can also be easily optimized for the NMRA's typical voltage range of 12V-16V.
Part list
- C1: 47µF 16V, 3528 (B) package tantalum capacitor, https://www.aliexpress.com/item/773515181.html,≈2€/20pcs
- C2: 22µF 10V, 3216 (1206) package ceramic capacitor, https://www.aliexpress.com/item/32506900002.html,≈1€/20pcs
- D1-D6: 1N5819HW, SOD-123 package 1A 40V Schottky diode, https://www.aliexpress.com/item/32377044871.html,≈1€/100pcs
- R1-R5: various values, 2012 (0805) package resistors
- U1: 78L05, SOT23-3 package linear regulator 5V 100mA, https://www.aliexpress.com/item/32925831060.html,≈2€/100pcs
- U2: ATtiny85-20SU, TSSOP-8 wide package
- U3: FDC6305, TSOT23-6 (SuperSOT-6) package N-channel MOSFET dual package, https://www.aliexpress.com/item/32568302710.html,≈3€/10pcs
Due to the global chip shortage, U2 might be hard to come by, but currently (2022) you can get them for about €2/pc. Typical EU VAT included. Shipping not included.
At OshPark, get three boards for just $2 with free worldwide shipping. At Aisler get 21 boards for just €10 with shipping in EU. At JLCPCB, you can panelize up to 20 boards per piece and get 100 boards for just $10 with worldwide shipping.
Total cost per unit ≈3€/$3.
Power dissipation
Large power means large circuit footprint. Solenoids typically draw currents up to 1A. Fortunately, the current draw is in very short pulses, typically up to 0.1s. To save space, the decoder is not designed for long term high current draw operation, as this would destroy its components as well as turnout solenoids. As a safety measure, resistors R4 and R5 pull down the MOSFET gates so that no current flows when the microcontroller pins are in an unknown state (after power up). When considering performance, the voltage drop resistor R1 is the most critical component. If its resistance is comparable to that of the turnout solenoids, it will consume comparable power, although it can dissipate much less heat.
Auxiliary pins
Figure 2: The circuit diagrams of the possible uses of pins 3 and 4.
Five ATtiny85 pins are distributed in the following way: Pin PB2 is used for DCC communication as usual. Pins PB0 and PB1 are used for solenoid control. This leaves two auxiliary pins PB3 and PB4 which can be used for various purposes, signal LEDs, jumper programming and debugging. The possible uses are shown in Figure 2.
Note that only for LED-only setup can both LEDs be turned on or off simultaneously; for all other uses, one of the LEDs is always on. Therefore, by default, the unknown status is programmed to blink between the LEDs. Only with LED-only setup can both LEDs be off in the unknown state.
Jumper programming is used by many commercial turnout decoders. When the pin is grounded by the jumper, the decoder changes its address to the address of the first close or throw request from the DCC command station.
Diagnostics can be useful if you want to expand an existing decoder project. Since only one-way communication is required, only pin TX is needed, which should be connected to pin RX of the computer's serial device.
Special thanks to Armin Joachimsmeyer, the author of ATtinySerialOut, who at my request adapted his library to accept any pin as a TX pin.
Printed circuit board
Figure 3: Printed circuit board
With the requirements listed above, I designed a single sided PCB measuring 20.5mm x 13.2mm. On the other side are six test pads that can be used for programming, as well as places for three resistors and six pads for covering all variants in Figure 2. The Gerber files are available on github.
Configuration variables
The turnout decoder has three configuration variables. CV33 and CV34 (dafault: 5) specify the pulse time of the current sent to the closing and throwing solenoids, respectively. CV35 (default: 0) sets the blinking time. If CV35==0, the blinking time is 1ms and the flashing is so fast that it appears that both LEDs are on. If CV35==255, both LEDs are off in an unknown state (after power up). This only applies to the LED-only setup described above. All times are in multiples of 10ms.
The result
Final considerations
Sketches for ATtiny85, decoder definitions for JMRI and Gerber files are available at github.
I made sure that all the components are available not only from internet electronics stores like Mouser, DigiKey, Farnell, or TME, but also from AliExpress, where you can get dozens of components for the price of one.
I can add more information, such as build and programming steps, upon request.
Disclaimer
I am only a physicist with very little formal training in electronics and programming. Use all the information above at your own risk.
Best regards