Pygmalion

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:

  1. it must be small and extra thin to fit well with the turnout,
  2. it must be simple enough that a hobbyist with average soldering experience can build it,
  3. the components must be cheap and readily available.

Circuit diagram

dt2.png
 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

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

alternatives.png
 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

dt2_pcb.png
 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

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 4
Geoff Bunza geoffb

@Marko Great Job

Hi Marko,

This is a great little decoder!

Would you please zip and upload the file(s) you used to fabricate the board (Gerbers files, or .brd file from Eagle PCB)?

Would you be willing to post a parts list/bill of materials, with specific sizes and parts you used? You could add it to your Github list or upload it as a file here.

I am interested in trying your design out myself. Also could you label the pads (hole and PCB pads) with corresponding schematic designations? From the video, it looks like you used some of these for LED indicators to show turnout direction. Is this correct? Which ones?

You should ask MRH if they were interested in this as a submitted article too.

Thanks for sharing your great work.

Have fun! 
Best regards,
Geoff

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Prof_Klyzlr

Stall-motor adaption?

Dear Marko,

Given there are 2 pins used for the solenoid, could these be adapted to act as a H-bridge to drive a Tortoise or similar? I know it would take it out of "self-contained mobile-turnout" form factor, but even for "larger installation" applications the idea of "one decoder per turnout" esp at ATTiny prices, holds some appeal...

Fantastic work, well done!

Happy Modelling,
Aim to Improve,
Prof Klyzlr

Reply 2
MikeHughes

Only a physicist …[rofl]

You’ve done very well with this.  Cool project.  I can see Kato or Atlas wanting to license this from you for their turnouts.  

Presumably there is some code somewhere, for programming the address, etc?  Operationally, is it using the DCC switch command?  
 

With a DS64 running about $85 Cdn, I’m curious what the build cost is for these? 

I'd like to build a couple and try them out with the MP5 as the DS64 is no longer available, DS7X replacement is even more costly and may not work with MP5s. (https://forum.mrhmag.com/post/mp5-advanced-model-railway-turnout-switch-motor-and-digitrax-ds64-application-notes-12218041)

Reply 0
Pygmalion

@Geoff Re:great job

Hello Geoff,

I have already posted the sketch, the JMRI file, and the Gerber files on  github. This way it will be easier for those who want to modify the project to fork it.

I have also added labels. I did my best - it's really a small board and there's not a lot of room for the text.

I have included the parts list directly in my original post. I will continue to expand the post as demand requires.

For LEDs I designed my own very simple PCB and soldered 10 SMD LEDs (see below). I can also provide the Gerber files of this project.

lt1_pcb.png 

All the PCBs are extremely tiny, so I actually panelized them, so there were 8 turnout decoder PCBs, 4 LED PCBs and 8 locomotive decoder PCBs on a single 10cm x 10cm 1mm thick board. Then I cut the board with tin snips and sanded the parts with sandpaper. I did not use a dremel, as the PCB dust is supposed to be toxic.

I'll report on my locomotive decoder later, maybe in a month or two if all goes well.

Best regards,

Marko

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
Pygmalion

@klyzlr Re:motor adaptation

Dear Klyzlr,

thank you for your kind words.

Based on my limited knowledge of electronics, I think you would need four MOSFETs and four flyback diodes for a proper H-bridge/motor use. This means that the board would have to be redesigned for such use and would be somewhat larger with the price practically intact.

However, as you may have noticed on  github, I am already preparing a small locomotive decoder. There I simply use an H-bridge in a single chip DRV8870 and an ATmega328 instead of ATtiny85. However, these components are a bit more expensive (10xDRV8870 costs $10 and 1xATmega328 costs $2.50 at AliExpress).

In any case, the sketches for the ATmega328 and ATtiny85 would have to be rewritten, and that is perhaps the trickiest part of the job.

Best regards,
 

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
Pygmalion

@Mike Re:DCC and programming details

Hello Mike,

thank you for your kind words. Well, I am sure Kato and Atlas could do better if they decided so - they have proper electrical and programming engineers and they could manufacture decoders with even much smaller components. However, my project has the advantage that anyone can build it and - as far as I know - there is no such project from major DCC manufacturers yet.

The sketch (program) provided on  github is written so that the decoder accepts DCC turnout commands. It can also be programmed in JMRI on program and main track. I have tested all of this.

Also, the sketch can be compiled to support jumper programming (see the original post).

As for programming with other command stations, the situation is a bit unclear as the NMRA accessory decoder standard is a big mess. Alex Shepherd , the (co-)author of the NmraDCC library, has helped me with this and even he is confused.

The build cost might only be a abour $3 if you make a lot of decoders. For a single decoder, though, it could be quite expensive - five 10cm x 10cm PCBs cost about $10 with shipping from China. You can panelize 20 decoders on a single PCB, so the cost drops dramatically if you make multiple decoders. Similarly with the components. You can get very good prices per piece on AliExpress, but you have to buy them in quantities of 10 or 100.

Best regards,

 

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 1
Geoff Bunza geoffb

@Marko re: Board cost

Hi Marko,

If you order your boards from OSHPark.com  the cost is $2.00 per 3 boards, or $0.67 per board with free shipping. Not bad!

Have fun! 
Best regards,
Geoff

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 1
Pygmalion

@Geoff re:Board cost

Hello Geoff,

that's a fantastic price. They even ship to Europe for free. If I lived in Germany I would have no extra costs, but the stupid Slovenian customs would add 3€ for tax and tax processing.  (AliExpress solved this problem by doing tax processing in Hungary and then resend to Slovenia.)

I'll add that information to the original post.

By the way, I apparently failed to upload new Gerber files with labels yesterday. github does this to me occasionally. I'll do a new upload today when I get back home.

[EDIT: it's uploaded now.]


Best regards,

Marko

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 1
Pygmalion

Commercial solutions!

I was just notified by another railroad model enthusiast Phil Grainger that there are commercial solutions for individual turnouts, specifically the Zimo MX820 family of decoders.

Of course my solution can not compete with the commercial ones - the size is comparable, 19mm x 11mm, but the Zimo decoders accept voltages up to 40V, they have thermal and overload protection, more light outputs, dimming and other options...

Given the design requirements of my decoder, specifically "simple enough that a hobbyist with average soldering experience can build it", I do not think the hardware can be significantly improved. However, the software can be greatly improved and/or tailored to the specific needs of a particular layout. Anyone is welcome to take the sketch on  github and customise it as they see fit!

And of course, the commercial solution costs about $25, while my design only about $3, which can be substantial if you need multiple decoders. The final decision is up to you!

Best regards,

 

 

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 1
Jim at BSME

Wow

Wow, definitely need to save this and looking forward to seeing your posts on the locomotive decoder.

Of course I don't really need another project.

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
nite

Great work

I'm very interesting trying this out myself.

I'm using 16 volts DC to power DCC++EX, so I'll need to work on the voltage divider resistors. Other components should be fine looking at the datasheets, maybe the regulator will get a bit hot. Possibly some rectifier diodes with a higher voltage drop will do the trick there.

Looking forward for more.

Niels

Reply 0
nite

Nice work

Hi, I'm very excited about this and can't wait to try it myself.

My setup uses 16V DC to power DCC++EX, so tracks voltage will be higher than 12V.

As far as I could see that poses no issue for the components used, except some tinkering with the voltage divider resistors. Potentially the regulator will run a bit hot, using rectifier diode with a higher voltage drop might fix that.

Looking forward what is to come.

BR, Niels

Reply 0
Jim at BSME

Excellent project

This is awesome, of course with a project like this requests will come, so here are mine.

First one is easy, how do you solder the surface mount components?

The next two not as easy, admittedly I have not looked at your sketch yet but:

As the ATTiny85 has PWM pins would it be possible to adopt this to control a servo?

And would one be able to add local push buttons or toggle switch control? I am thinking of the situation where one has the brakeman at the opposite end of a train needing to through a turnout for the engineer.

 

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
Pygmalion

Higher voltages

I think the same circuit should be fine for 16V input voltage. The diodes can withstand up to 40V, and the maximum input voltage for the voltage regulator is at least 25V. The only question is whether your solenoids can handle such voltages.

As for the power dissipation of the voltage regulator, the current draw of the AtTiny85 is quite low, about 5mA at 8MHz. So if no LEDs are used, the power dissipation of the voltage regulator is 5mA * (16V - 5V) = 55mW. From what I found on the internet, it should be fine up to 350mW. If you use LEDs, you have to make sure that they do not consume more than 25mA, which is way too much for normal brightness. The consumption by MOSFETs can be neglected.

 

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
Pygmalion

@Jim Re: other questions

Hello,

I solder by hand and I am not a pro, so I designed the board so I can solder it myself. It's a little easier with a thin soldering tip and thinner solder wire. I usually put the solder on one of the pads. Then I position the component and let the solder flow again until I am satisfied with the position of the component. After that, I solder all the other pins. I start in the middle and work my way to the edges of the board. Components with multiple pins are soldered first, i.e. U3 - U2 - U1 and then the other components.

This design is not suitable for motors. For that you need an H-bridge (four MOSFETS), because you need to switch the current direction. I am currently designing another small decoder for locomotives that I think can be reprogrammed to work with servo motors - perhaps dr. Geoff's sketch should be adapted slightly.

This one will not be functional very soon as I am busy with other things, but you can see the circuit here: https://github.com/marko-pi/MPED-DIY
/>
I did not quite understand your question about push buttons. You want to control the turnout with both DCC and two pushbuttons?

I hope this is of some help to you. Sincerely,

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 1
Jim at BSME

re: Other questions

Marko, thanks for the tips on how you solder SMD components.

As for servos (the type used in remote control cars and planes) as they are not like a traditional DC motor you don't need to use an H-bridge to reverse polarity to change directions. They are feed a pulse train that circuity inside the servo takes and determines the position to move the servo arm too.

As for push button and DCC control that is what I was thinking a local pushbutton(s) to control the turnout from a local control panel by an operator and DCC for those times when I have the throttle. The situation I was thinking of is where you have and engineer and conductor/brakeman at the opposite end of a train that is dropping off cars. The engineer would have the throttle so the brakeman would need a way to control the turnout locally with a push button.

Thanks for thinking about this.

 

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
Pygmalion

@Jim Re: Other questions

Jim, from your description, it appears that the same circuit could be used for the servo motors. However, a new sketch would have to be written. And since the DCC protocol can be very cumbersome, this could be quite a tough nut to crack.

As explained in the description of the turnout decoder, there are two free pins on the ATtiny85. These could be used for LEDs, debugging, jumper programming or push buttons. Not all at the same time, of course. Using two pushbuttons would require only relatively minor changes to the sketch.

Best regards,

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
nite

sourcing the PCB

If you're in the EU, aisler.net might be an interesting choice, they are Germany based. I had 21 board made for 10,14 EUR, incl VAT and shipping, that's under 50 cents a board. Took about a week

 

oderPCBs.png 

Reply 1
JAMES DEWAR

Kato turnout

As Kato operates their turnout motors by switching the direction of current using only 2 wires.

Would this device be device configurable to do this

Reply 0
nite

Shopping @ aliexpress

For second time in a few weeks I received an order of Attiny's, all were not-fuctional. So much for shopping in China. 

I literally have hundreds of orders from Aliexpress, mainly electronic components, and all was well. But lately the prices have gone up, shipment is very slow and specially the more complex components are prone to counterfeiting, falling through quality control, or re-use.

Just ordered a batch of Attiny's through Conrad, they were cheaper than Ali. Let's hope they work.

Just had to get this off my chest, sorry for abusing this place.

Reply 1
Pygmalion

@nite Re: sourcing the PCB

Hello,

thanks for the suggestion. Maybe I'll try Aisler next time.

I am so glad someone found my design useful!

More expensive parts, like chips, are usually riskier to buy at AE, and the price difference is not large anyway. However, I recently could not get ATtinys at Mouser/DigiKey/TME, so I bought them at AE. Fortunately, they are fine. On the other hand, the ATmega328 from AE are a bit suspicious...

Best regards,

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
Pygmalion

@James Re: Kato turnout

Hello James,

this is interesting. Apparently the Kato turnout has only one solenoid and uses a permanent magnet instead of a soft iron as the core. You can read about the difference in operation on my page here: https://www.pinteric.com/modeltrain_swit.html

If I got it right, current must be able to flow in both directions.  If so, I do not think the current turnout decoder design supports this type of turnout; I should add an H-bridge or another pair of MOSFETs. Maybe professional electrical engineers have an alternative solution?

Best regards,

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 0
Pygmalion
@nite

I made a new version of the circuit board and really wanted to order from Aisler. But I needed the boards as small as possible and Aisler does not support thicknesses other than 1.6 mm. So I panelised 15 boards per physical board and ordered them from JLCPCB. For a little more than 10 EUR, I received 75 boards 10 days after ordering. The last step was to cut the boards to size. The process is described in the following video:



@geoffb

I have redesigned and reprogrammed the project a bit. I see I can no longer edit the original post. Should I open a new thread or post the new version in this one? How can I make it clear that people should use the new version?

Best regards

Marko Pinteric

See my Model train nostalgia - How to repair Mehano(tehnika) items from 1960s to 1980s and transfer them into 21st century!

Reply 2
jeffshultz
Pygmalion wrote:
@nite

I have redesigned and reprogrammed the project a bit. I see I can no longer edit the original post. Should I open a new thread or post the new version in this one? How can I make it clear that people should use the new version?

Best regards


Marko, you should now be able to edit the original post. 

orange70.jpg
Jeff Shultz - MRH Technical Assistant
DCC Features Matrix/My blog index
Modeling a fictional GWI shortline combining three separate areas into one freelance-ish railroad.

Reply 0
Reply