SMA10 – Build a 17-Function DCC Decoder for about $5

geoffb's picture

My activities in Scale Model Animation often lead me to use DCC as a control method for sequencing. This could include building and street lights, even sparking welders and twinkling camp fires. Timing can be done with JMRI scripts (Like this: http://youtu.be/RXpvp5mIXvU?list=PL4dHfZjHc9t-pvctGQzSuOXBLG1M42xbS) or with programmatic control (Like this: http://youtu.be/zlcdJKXj_YI?list=PL4dHfZjHc9t-pvctGQzSuOXBLG1M42xbS). Control switches for such projects are a must. Using 4 or 6 function DCC decoders in this regard, can get costly, quickly. But with a little effort, and some low cost components one can build a DCC decoder supporting 17 independent functions for about $5.00, or less, depending what you have on hand.  This version has no CV’s, uses no DCC programming, can be set for any short address in CV1, and controls 17 Function Lines (which I am using to drive the green LEDs in the example below). Interested? Read on! (Latest Rev for this work can be found here: http://model-railroad-hobbyist.com/node/19446 )

Comments

geoffb's picture

Building the Decoder

This all starts with a $2.41 Arduino Pro Mini Board, a 6N137 optocoupler, a LM78L05 voltage regulator, miscellaneous resistors and capacitors, and a program and library modified for the job. The Pro Mini and other parts are mounted on a piece of perf board (available from many sources, even radio shack—google searches can be very helpful here). A diagram is included below as well as some key parts that have Digikey (http://www.digikey.com) numbers and prices for reference. Again, these are available from many different sources—electronics distributors, surplus dealers, ebay, Radio shack, and Fry’s Electronics. Assembly is pretty straightforward if you’ve ever soldered a small set of components before. You could also use a solderless breadboard (like Radio Shack 276-003) to assemble a decoder. The example decoder pictured above  has 17 green LEDs each with a 10K (10,000) ohm resistor in series connected to each “Function Output” pin. On the Arduino Pro Mini these are labeled 3-13 and A0-A5 and they directly correspond to standard DCC Decoder Functions F0 through F16. Each pin can directly drive at least 40ma at 5 volts, more than enough for several LEDs each.

The materials were all hand soldered.  You don’t need to mount any of the LEDs on the board.  I did it as a test and demo. You should cut off the surface mount LED Pin 13 indicator in the Pro Mini Board or its dropping resistor as it is both redundant and may interfere with the function assigned to that pin. I found that the circuit with the fast 6N137 optocoupler was very sensitive to noise on the DCC lines. My friend Gordon Hoffman suggested adding Capacitor C8 (270pf) and it solved the problem reliably. Other published circuits that I tried were far less reliable. Make sure you place C8 as close to the 6N137 as possible. I soldered the small C8 cap onto the 6N137 pins directly.

Partial Bill of Materials:

Arduino Pro Mini atmega328 5V 16M    (  from EBay:   http://tinyurl.com/kexh66b )   $2.41

(This price varies. I've seen it as low as $2.25. Search Ebay for "Pro Mini." Price often includes free shipping. It should specify an Arduino Pro Mini 5 Volt 16M Atmega328)

Digikey example parts (http://www.digikey.com):

160-1791-ND               OPTOCOUPLER HS LOGIC OUT 8-DIP            $ 0.73

LM78L05ACZXCT-ND  IC REG LDO 5V 0.1A TO92-3                           $0.44

493-5914-1-ND           CAP ALUM 220UF 25V 20% RADIAL                 $0.38

DF005M-ND                RECTIFIER BRIDGE 50V 1.5A 4-DIP                 $0.45

1N5819FSCT-ND          DIODE SCHOTTKY 40V 1A DO41                   $ 0.50

Other Resistors & caps as needed

17 Function Decoder Schematic (A4-A6 connections omitted)

The Arduino Pro Mini needs to be loaded with the decoder program which you can download from here (updated November 26, 2016):

/sites/model-railroad-hobbyist.com/files/users/geoffbfiles/new-dual-multifunction-decoderv5_4.zip

There are two folders in the zip file: NmraDcc and DecoderNmraDcc_4

If you have set up and installed the Arduino development code from http://arduino.cc/en/Main/Software  (Read on for details)

Then copy the NmraDcc folder to \My Documents\Arduino\libraries on your computer and copy DecoderNmraDcc_4 to \My Documents\Arduino

Then open the DecoderNmraDcc_4.ino sketch (program), set your decoder address, and you are good to go!

The original NmraDcc library was obtained from the “Model Railroading with Arduino” website (http://mrrwa.org/) which is made available free for personal use under a GNU general public license. This was a version written by Alex Shepherd from 2012. While incomplete, it gave me a great start to build this decoder. Many thanks for Alex’s work.

This is partially what it looks like:

#include <NmraDcc.h>

// Working 14 Function DCC Decoder  DccAckPin not needed

int tim_delay = 500;

#define numleds  17

byte ledpins [] = {0,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};

const int FunctionPin0 = 3;

const int FunctionPin1 = 4;

const int FunctionPin2 = 5;

const int FunctionPin3 = 6;

const int FunctionPin4 = 7;

const int FunctionPin5 = 8;

const int FunctionPin6 = 9;

const int FunctionPin7 = 10;

const int FunctionPin8 = 11;

const int FunctionPin9 = 12;

const int FunctionPin10 = 13;

const int FunctionPin11 = 14;     //A0

const int FunctionPin12 = 15;     //A1

const int FunctionPin13 = 16;     //A2

const int FunctionPin14 = 17;     //A3

const int FunctionPin15 = 18;     //A4

const int FunctionPin16 = 19;     //A5

 

NmraDcc  Dcc ;

DCC_MSG  Packet ;

 

#define This_Decoder_Address 17

 

extern uint8_t Decoder_Address = This_Decoder_Address;

struct CVPair

{

  uint16_t  CV;

  uint8_t   Value;

};

…and so on!

 

To set the decoder address, edit the line:

#define This_Decoder_Address 17

 

And change the “17” to whatever short DCC address you want the decoder to respond to.

While there are hooks in the code for CV and long address support, I haven’t verified or changed any of these additional hooks… yet!

You do not need to know to understand the program at all. Simply set the address and load it. You are always welcome to learn more, but it is completely usable as is.

 

You can get a step by step cookbook for loading the Pro Mini here:

http://model-railroad-hobbyist.com/node/17244

You will need a USB cable to load the program. You can get one here:

http://tinyurl.com/lydcv99

It can be used again and again for more decoders and future projects.

Once you get it assembled and programmed, hook it up to any DCC track bus and away you go! If you did everything right you can set your control for the decoder address and turn on and off 17 functions at will. You will note that as the decoder powers on, it will run through a test cycle torning all functions on, then all functions off—and only once. If you don’t want this, you can simply delete the test in the code (Lines 59-68) which are:

  for (int i=1; i<= numleds; i++) {

     digitalWrite(ledpins[i], HIGH);

     delay (tim_delay/10);

  }

  delay( tim_delay);

  for (int i=1; i<= numleds; i++) {

     digitalWrite(ledpins[i], LOW);

     delay (tim_delay/10);

  }

  delay( tim_delay);

 

This video will give you another look at the decoder:

While only implementing 17 on/off functions, it will eventually be possible to assign different working functions to each pin, from dimming, flashing, servo controlling, to most anything. The processor and memory in the Pro Mini is more capable than most but not all decoders on the market today. Getting the CV read/write capability working (next) should eventually lead to assigning complex functions to each pin—like “open the door, ramp up a light, and sound a sqeak in sequence” or “move to the next turntable track after rotating 180 degrees.” Because this is an Arduino, a “Function Key” could also read a pin instead of outputting to it. And these are the reasons why I started in on this project!

Construction Comments Addendum
1. Use only the library I provided in the Zip file, previous editions will not work.
2. The green LEDs used as Function indicators are especially bright even with very low current. With a 10K dropping resistor normal LEDs you might have may not light up. Try using a lower value resistor -- closer to 1K for more normal LEDs. (I get these from surplusgizmos.com listed as 3mm green LED).
3. To get the LEDs to fit side by side on .1inch centers, I used a sanding disk mounted in a moto tool to sand the sides down just a bit in parallel with the LED pins. They are plugged into an IC socket cut and soldered to the board-- it keeps tiem in a somewhat neat line.
4. If you don't want to solder this circuit, it can successfully be built on a solderless breadboard (see Dave Bodnar's writeup for an example: http://www.trainelectronics.com/DCC_Arduino/    He also adds a servo control example which you may enjoy!

Comments and appropriate suggestions are always encouraged.

Have Fun!  smiley
Best regards,
Geoff Bunza

pierre52's picture

Itching to learn more

Thanks for posting Geoff. I would love to get into this but it looks like a pretty steep learning curve

Peter 

The Redwood Sub

Wonderful Post

Hi Geoff,

Great post. 

I knew it was possible to decode DCC signals..hadn't thought of using the optocoupler to handle the DCC voltages.

I normally use Picaxe for the electronics/animation on my layout.

Happy Modeling, Bruce

Dave O's picture

Gosh ...

... it is like you've opened the door to a whole 'nother world!  Thanks for posting this ... might have to give it a go!  :)

This would have made a wonderful article ... with some follow-up "projects" could be a bit of a series ....  ;)

geoffb's picture

The Learning Curve

Pierre,

There is a learning curve, but BE FEARLESS! ...Well, maybe a little bit fearless. I've helped a dozen or so local modellers start using these cheap little controllers-- anyone can do it. Once you get by the program "loading" and if you haven't soldered much that's the next. But it opens up a new world for you as a modeller. And in this forum there are lots of people who can help too!

Have a go at it! and have fun!

Best Regards,

Geoff

 

geoffb's picture

Using your 'Axe

Bruce,

I do think the timer and interrupt capability on the PIC could handle the job, but you'll have to port the library that i used or write your own. This is why I use the Pro Mini-- it's cheap and there are many libraries already written, that gives a great starting point. I want to model and animate not write code-- I did enough of that professionally. smiley

Best Regards,

Geoff

geoffb's picture

New World Order for Dave

Dave,

This stuff opens up allot of possibilities for modelers in any scale, even beyond model railroading. Once you get the tools on your bench, I havee no doubt that you'll come up with ideas that never even crossed my mind! smiley

By the way this will not be the last version you'll see of this. As I said in the post above, this is just the start for more interesting "decoders" and many more projects. Tune in later!

Have at it Dave!

Best regards,

Geoff

 

Michael T.'s picture

Turnout control?

I saw mention of servo control being possible at some point. I know your goal is animation but does this mean this decoder would eventually be suitable for turnout control?

Michael

Michael

Original member of the "Gang of Six"

R.I.P. Verne Niner. The time I got to know you was way too short my friend.

"We all model the prototype, to suggest otherwise is ridiculous"

My Pike, https://mrhmag.com/blog/MichaelT

 

geoffb's picture

Turnout control and more possibilities

Hi Michael,

My goal with this report was to build a platform-- a starting point -- on which I was confident that more could be built. The next step for me is to fix the CV reading/writing capability in the library-- which is incomplete in this area.

Why? Because I want to create CV pairs for each of the 17 pins in the demo. The first CV would define the "function" of the pin, for example 1- Simple on/off 2- Dim based on value of second CV, 3- Servo control end to end based of 2 other CV values, 4- Blink, 5- Ramp up ramp down (like rotating lights, 6-- more to come!

There is also a much more interesting feature I want-- trigger inputs-- think chuff cam on steroids! smiley For example if a pin were to be switched low to ground (common) it could enable another function, or turn off another function. What I want this to do for me is to turn on/off sounds, and read sensors-- like distance sensors, proximity sensors, light sensors, etc. You might remember I have a great interest in animation. While these decoders can easily be attached to your track DCC system, I expect to have one or more DCC control buses (alternate tracks?) to control animation scenes. I you look at some of my other write ups and videos, you can see animations DCC controlled by a cheap Arduino Pro Mini "DCC Base stations" ( http://model-railroad-hobbyist.com/node/14762 ). These enhanced DCC decoders will be used there.

You may have to wait a bit for me to get this done and write it up, but you or others can take my work and build on it too. In fact, I'm hoping others join the fun!

Best Regards,

Geoff Bunza

 

Geoff

This is an outstanding example of a tutorial. Thanks.

In line with Michael's question, I take it that this module would have little issue to sink current for a turnout servo; I'm thinking it could adequately source it too? My Uno has the ability to do the latter.


>> Posts index


Journals/Blogs

Recent Blog posts: