DCC Control Variety
I have been using both DCC decoders and independent LEDs, motors, servos and sound modules together in animations for years, like and:
What enables the close coordination of all of these is an Arduino based DCC generator combined with the general purpose capabilities of Arduino control. This article describes the newest version I use, based on a low cost add-on or daughter board that attaches to an Arduino Pro Mini. This is similar to the decoder series I have presented earlier in SMA20 New Low Cost 17 Channel DCC Decoders with PC Boards & Dual Motor, LED, & Servo Control ( https://forum.mrhmag.com/post/sma20-low-cost-17-channel-dcc-decoders-ver-6-01-with-soundtriggered-soundstepperdual-motorled-and-12201920 ). These boards are included for the modeler too. This pair can be configured in a large number ways, and like the decoders, a pre-configured set of options is described and made available for the modeler to load into their own Pro Mini. Then, you are good to go. These pre-configured examples include a simple, autonomous DCC controller with speed and 6 function control, a break-in test version for point to point running with slow down and reversals at each end point, a “trolley line control” with independent stops and reversals, and an absolute stopping block for DC and DCC locos including those equipped with keep-alive features. Absolute stopping blocks can be used as safety or guard block to prevent your fine locos careening into the abyss beyond lift-uts and open bridges on your layout.
I call this module a DCC Function Generator. The combination can also be used as a general purpose interface board for an Arduino Pro Mini, to ease interface tasks, or directly drive 2 DC motors or a stepper motor.
What is a DCC Function Generator?
A DCC Function Generator (DFG) is a combination DCC base station and throttle rolled into one unit. Normally, I do not use it for manual control, even though I will show how it can operate as a basic substitute, perhaps for a display or test track. Like the Arduino Pro Mini – based decoders I use ( https://forum.mrhmag.com/post/sma20-low-cost-17-channel-dcc-decoders-ver-6-01-with-soundtriggered-soundstepperdual-motorled-and-12201920 ) the DCC Function Generator is also built using the ubiquitous Pro Mini. Also like the motor control version of the decoder, the DFG uses the Texas Instruments SN754410 Dual H-Bridge chip to drive the DCC signals. This is limited to a mere 1 Amp but other H-Bridge chips and/or modules could also be substituted if higher amperage is desired, like http://www.ebay.com/itm/170926726867. I started with this design because I built animated scenes that used commercial DCC decoders, and had the need to control other lights, motors, and sounds in addition. I’ll give some examples and provide the sketches used to set up the Pro Mini later. I do wish to make it clear this is, and was, never meant to be used as a substitute for a complete DCC command station. In my view it has utility and value beyond that. Similar to the Pro Mini DCC decoder series, the DCC Function Generator’s code or sketches are built on the groundbreaking work of another modeler, D.E. Goodman-Wilson, who created the CmdrArduino DCC library ( http://railstars.com/software/cmdrarduino/ ), which my work uses. While the library does not account for every operation a base station might want, for my purposes it works quite well, and I am grateful for Don’s contribution.
Configurations
One might think that such a little board serves one purpose and should be built one way. Well …no! You can certainly build it with all components and use it to drive its own DCC bus. You can also use it as a Pro Mini bread board or prototyping aide, a single or dual motor driver, a stepper motor driver, a DCC Function Generator and a single DC motor driver. Consider the small set of 3 pads in a row next to the +PWR- connections on the board top. If you “solder blob” the middle pad to the one marked 12V, then the DCC and the MOT out from the H-Bridge driver will swing +/- 12Volts. If you “solder blob” the middle pad to the one marked 5V, then the DCC and the MOT out from the H-Bridge driver will swing +/- 5Volts. “Solder blob-ing” means to load the 2 adjacent pads with a small blob of solder, and add enough to bridge the tiny gap connecting the 2 pads – a cheap way to make a semi-permanent option switch or jumper! Connect the appropriate blobs before attaching the Pro Mini board. It will be easier that way. You can also leave off the two 22uf SMD capacitors if you feel you don’t need the extra filtering (they will be hidden under the Pro Mini board once attached). I always include them.
Building the Board
This little unit is composed of 2 small boards, sandwiched together in a mother/daughter board arrangement. The daughter board or driver board can be soldered or socketed to the Pro Mini controller. There is provision for an on-board, 5 Volt regulator. I prefer to use the PSU3-5 shown in the parts list. You connect 12 Volts - 15 Volts to the PWR+/- connections on the driver board. Input/output connections D8, D11, D12, D13, D14/A0, D15/A1, D16/A2, and D17/A3 are brought out to a separate set of pin headers for connection convenience, but you may also solder directly to the pin holes in the board. A0-A3 designate those pins that can also be used for Analog inputs (like for use with a resistive potentiometer as a speed controller). Each input/output pin is labeled and situated next to a +5 Volt pin and a ground pin for ease in connecting to switches, potentiometers, servos, sensors, and LEDs. The board combination is small enough to hide in an HO scale model structure, building, or scene.
The PC Board can be ordered from this board fabricator: oshpark.com
The boards are 1.60x1.19 inch (40.64x30.15 mm) 2 layer boards. OSHPark offers a public service fabricating very high quality, low cost PC boards in low quantities. Download the (Eagle PCB layout) board files (.brd) from here: SMA22_Download.zip
Set up an account at OSHPark (really easy) and upload the board file(s) you want to fabricate and specify the quantity (always in multiples of 3—their rules) either of these will cost $ 9.45 for 3 (3.15 per board). They accept Paypal and will ship international. I have no vested interest in OSHPark – I’m just a satisfied customer. Please feel free to use whatever fabricator you know. I know of no source for fully built boards. That could change in the future.
The bare boards are shipped “panel-ized” so break or cut them apart. Follow the component diagrams below and solder the components to the board. I add the pins and/or sockets, last. Use a low temperature soldering iron or a low wattage soldering iron for these.
Daughter / Driver Board
Driver Board Schematic
Board Top
Board Bottom
Top Component Placement (Note: This Board is Equipped with Strip Sockets for the Pro Mini
Bottom Component Placement
Example Materials
Arduino Pro Mini atmega328 5V 16MHz (from EBay:
http://tinyurl.com/kexh66b ) $1.95
(This price varies. Search Ebay for "Pro Mini." Price often includes free shipping.) These can also be obtained from
many other sources. Problems have been reported using these with Windows 10 drivers. If you don't want to mess with the anomalies of the Chinese knock offs, try these likely genuine FTDI cables instead
Arduino Pro Mini USB FTDI TTL Programming Cable (Ebay:
http://www.ebay.com/itm/111241824248)
Please note some programming cables sold have scrambled pin outs, and color coding is wrong! Please be careful. The correct configuration is:
Pin Wire Signal on Signal on# Color Cable Pro Mini Label1 Black GND GND or BLK
2 Brown CTS
3 Red +5 Volts VCC
4 Orange TXD
5 Yellow RXD
6 Green RTS DTR or GRN
This is the standard coloring and order that FTDI used. It is clear that some cables were manufactured incorrectly. I would be cautious of the wire colors too! I have found that red for +5 Volts and Black for ground is commonly used. If you have a voltmeter, plug your cable into your computer, but not into the Arduino Pro Mini and measure the voltage from the black wire to red wire-- it should be +5 volts. The set these 2 pins into the Pin 1 and Pin 3 socket as shown above.
Digikey example parts (
http://www.digikey.com):
478-8312-1-ND CAP TANT 22UF 25V 10% 2312 SMD 1.22
296-9911-5-ND IC HALF-H DRVR QUAD 16-DIP 2.43
LM78L05ACZXCT-ND IC REG LDO 5V 0.1A TO92-3 0.44
497-5838-1-ND L4931 IC REG LDO 5V 0.25A TO92-3 0.37
497-15682-5-ND L7805 IC REG LDO 5V 1.5A TO220 0.58
PSIP-80 PEEL-A-WAY(R) MACHINE PIN SOCKET STRIP 2.50/50 socket pins
These socket strips will accept DIP packages, or 0.020 wire – like the Tichy phosphor bronze wire.
MTS-5 SPDT ON-OFF-ON MINI TOGGLE SWITCH 1.25
PTW-10K 10K BOURNS POT, PRE-WIRED 2.75
Between the diagram and the pictures, assembly should be straightforward. Use a low wattage (15-40 Watt) or temperature controlled soldering iron and take your time.
Switches (for function control) and variable resistors (potentiometers) are connected to the appropriate header pins (or holes if you don’t want to use pins) according to these diagrams:
The switch may also be a normally open pushbutton.
Realities of DCC Control: Some Additional Comments
The modeler using the DFG is exposed to the “nits and grits” of DCC in a way that commercial products normally shield the mere mortal modeler. Several things must be remembered:
***** DCC commands are normally repeated over and over again to decoders on the DCC bus (tracks). In using the CmdrArduino library, it is still very much incumbent on you setting up the DFG to assure that commands are sent out in a timely fashion. You cannot assume that every command will reach the decoder, the first time. Typically you can’t assume a decoder has memory to retain its speed and function states.
***** As a DCC modeler at this level, you will find that different decoders do not quite behave as consistently as you might like. I have noticed that some decoders act on the transition of a function from off to on, some behave differently when the function is repeated “on.” Some even have a slightly different behavior when a function is switched from on to off.
***** Simply sending a single speed packet does not necessarily start a loco moving, particularly one with sound, or one with momentum effects turned on. Likewise, immediately reversing the direction of a locomotive sometimes has unpredictable effects on decoders – including just coming to a halt! You should take a look at the trolley/shuttle example and note the delays inserted.
***** You should note that each of the examples provided have been tested, and they work as described. So you should get some utility using them as a starting point.
***** For the intrepid modeler, note that the “set_function(function_number,0/1)” has been provided to turn a function numbered from 0-12 on or off (1 or 0). Also, one sets the appropriate speed for whatever decoder is addressed by the variable locoAdr by setting dcc_speed to a number from 0 to 127 or 0 to -127. Avoid one (1) which will send an estop to the decoder. All these simply set up the command set to be retransmitted over the DCC bus. You provoke the transmission by using dcc_cmd_rpt (time in milliseconds up to 32767). This is also how you insert delays before changing functions or speeds
***** Setting locoAdr to zero (0) sends a “broadcast message to all decoders.” I do not know how all decoders will respond to broadcast messages, although it is part of the NMRA DCC standard. If you have lived in a DCC cave for the last 20 years or so, you may not know that there are inconsistencies in the way certain decoders (commercial decoders) implement the NMRA specification (or don’t).
***** This library does not implement speed stretching for DC motored locos on DCC tracks.
***** There are more features in the library that you can explore. It can address more than one decoder address serially, it can generate accessory decoder switch commands. I will leave all the other “features” for the readers (your) exploration. To my knowledge, active development has stopped on the Cmdr Arduino library. I have made some minor changes that I have included in the accompanying download. With its limitations, it is still a usable, featured, free library and so far, the best one I have found.
Give Your DCC Function Generator (DFG) Its Personality!
Since this is a bit different and likely new to most modelers, I am making an attempt to simplify DFG usage by providing a starter set of examples. You can download the daughter board file (.brd) and all the sketches—provided as examples with a version of the CmdrArduino library, which I have fixed some minor bugs). Load the entire CmdrArduino library folder into your …\Arduino\libraries folder in your …\My Documents\ folder (on Windows).
Build the DFG hardware variant of you want, and then look here for information on setting up and loading the DFG sketch (program) of your choosing. With that said, a step by step cookbook for loading the Pro Mini sketch, oriented to the modeler, can be found here:
Starting from Scratch with an Arduino Pro Mini (or Moteino):
http://mrhpub.com/2014-11-nov/land/#99
The “Arduino Pro Mini Controller” used here is one of a set of small processors (computers) on boards, set up to allow easy use by people who want to use them as a tool, rather than focus on the details of the technology. Arduinos were originally conceived as a teaching tool in Italy. All aspects of their design and construction are available for public use with no restrictions. These are manufactured and distributed by many companies worldwide, some for as little as $2 (quantity one) including shipping! With such a cheap and powerful tool, a series of tested, working, and packaged instructions (called Sketches or programs) to set up these versatile DCC Function Generators with many practical functions are made available.
I have pointed out before that I am not a proponent of asking modelers to learn how to program-- just learn how to use these new tools. You can stop there or take it as far as you want. These are used in high schools and colleges all over the world to teach basic concepts. I am not trying to do that here, merely trying to show others that these little guys can add a great deal to your modeling enjoyment.
If you really want to learn more, a good start for climbing the Arduino Learning curve would be the tutorials at: http://arduino.cc/en/Tutorial/HomePage
and there are allot of topics there for searching and browsing or
http://arduino-info.wikispaces.com/TUTORIALS
or http://arduino.cc/en/Guide/ArduinoProMini
Pre-Configured Examples
You can download the board file (.brd) and DFG sketches here:
SMA22_Download.zip
DCC_FTN_Gen_ESTOP_SW
This version is used to protect and open /up bridge or a lift-out
A double insulated section of track (block) is switched over to a DCC ESTOP
stopping ALL DCC equipped locos, even those equipped with keep-alive
D8 has a double pole double throw relay to switch in DCC Function Generator Control
This pin goes low (connects to Ground (GND) when active (see diagram below on the left)
D17 has a normally open switch connected to GND, This switch is connected to Ground
when the bridge is open or the lift out is missing, this activated the ESTOP Block
default locoAdr= not relevant
DCC_FTN_Gen_Basic
This version provides basic speed and function control using switches for Functions F0-F4,ESTOP, & Direction
Speed is controlled by a variable resistor (potentiometer)
D8,D11-D15 Have switches connected SPST to GND controlling F0-F4
D17 has a normally open pushbutton connected to GND for an Emergency Stop ESTOP and may be omitted
A2 / D16 Has the slider of a 10K potentiometer connected, either other pot end to VCC and GND
this is the speed control
Direction is controlled by Switch connected to D15
default locoAdr= 3
DCC_FTN_Gen_Back_Forth_Stops
This version is used to operate a trolley or shuttle on its own independent track with stops and reversals
Each of the following inputs are typically connected to a position sensor or short block sensor which goes low
when detecting the presence of the loco/trolley/shuttle (like an optical sensor):
D11 sense forward motion, stop, & go in reverse
D12 sense reverse motion, stop, & go forward
D13 intermediate short stop switch / sensor
D14 intermediate long stop switch / sensor
If you don’t need the intermediate stops, then don’t connect D13 or D14 to anything
If you want multiple long or short stops, connect multiple switches/sensors together (in parallel)
A switch or pushbutton is connected to D17 to act as an emergency stop ESTOP and may also be omitted
default locoAdr= 3
DCC_FTN_Gen_Back_Forth_on_PB
This version is used for simple testing and demos – with each pushbutton press it sends a loco
forward with the speed set by a variable resistor (potentiometer) for approximately 12 seconds,
stops and reverses travel for 12 seconds and stops
Functions F0-F5 are set by switches connected to D8, and D11-D15
A2 / D16 Has the slider of a 10K potentiometer connected, either other pot end to VCC and GND
this is the speed control
default locoAdr= 3
DCC_FTN_Gen_Decoder_Test
This version is used to test functions F0-F12
All functions are turned on in sequence with a half second delay between each,
and then turned off in sequence; after a 2 second delay, the test repeats
default decoder (locoAdr)= 24
DCC_FTN_Gen_Template
This example is provided as a teaching tool and starting point for you to see what capabilities are available
You should pay close attention to the heavily commented loop() section (duplicated below)
and use this as a starting point for your own experimentation
This version provides basic speed and function control using switches for Functions F0-F4,ESTOP, & Direction
Speed is controlled by a variable resistor (potentiometer)
D8,D11-D15 Have switches connected SPST to GND controlling F0-F4
D17 has a normally open pushbutton connected to GND for an Emergency Stop ESTOP and may be omitted
A2 / D16 Has the slider of a 10K potentiometer connected, either other pot end to VCC and GND
this is the speed control
Direction is controlled by Switch connected to D15
locoAdr= 3
DCC_FTN_Gen_Template loop() section example:
void loop() {
//
// YOU CAN SET UP YOUR DCC COMMANDS HERE
// WHICH WILL BE REPEATED OVER AND OVER IN A LOOP
//
// F0-F12 ARE SET TO OFF (0) AT THE START, AND DCC SPEED IS SET TO 1
// IF YOU WANT TO USE AN EMERGENCY STOP (ESTOP) BUTTON INCLUDE THE NEXT LINES:
//
if (digitalRead(estop_pin)==LOW) {
dps.eStop(); // ESTOP Power Cut Off
// Global Broadcast
dcc_cmd_rpt ( 30000 ); // stop everything for 30 seconds
}
// END OF ESTOP SECTION
//
// THIS NEXT LINES CAN BE USED TO SET DCC LOCO SPEED (OF THE SELECTED locoAdr)
// READING A POTENTIOMETER CONNECTED TO Analog pin 2 == D16
dcc_speed = (analogRead (speed_pin)/16)+1 ;
dcc_cmd_rpt ( 1 );
// THE PREVIOUS LINE: dcc_cmd_rpt ( 1 );
// DOES 2 THINGS - IT SEND OUT DCC SPPED COMMAND AND ALSO REFRESHES FUNCTIONS F0-F12
// AT PRESENT THE LIBRARY CAN ONLY HANDLE F0 - F12
//
// THE FOLLOWING LINE READS A POSSIBLE SWITCH CONNECTED TO P5 (D15)
// AND SETS THE DIRECTION ASSUMING THAT THE AALOG READ AVOVE SET THE SPEED
if (digitalRead (p5) == LOW ) dcc_speed = - dcc_speed; // reverse direction
//
// THE FOLLOWING LINES SHOW HOW FUNCTIONS ARE SET BASED ON READING SWITCHES
//
set_function (0, digitalRead (p0) & 0x01) ;
set_function (1, digitalRead (p1) & 0x01) ;
set_function (2, digitalRead (p2) & 0x01) ;
set_function (3, digitalRead (p3) & 0x01) ;
set_function (4, digitalRead (p4) & 0x01) ;
// THE FOLLOWING LINES SHOW HOW FUNCTIONS ARE SET BASED SKETCH VARIABLES OR CONSTANTS
//
set_function (5, 0) ; // TURN OFF FUNCTION 5 (F5)
set_function (6, 1) ; // TURN ON FUNCTION 6 (F6)
set_function (7, F_VARIABLE); // SET FUNCTION 7 (F7) TO WHATEVER IN IN F_VARIABLE
dcc_cmd_rpt ( 1000 ); // UPDATE ALL DCC COMMANDS & WAIT 1 SECOND
//
//
//
// END OF YOUR DCC COMMAND LOOP
//
}
Appropriate comments and suggestions are always welcome.
If hope you can use this, and I hope you share your work with other modelers. Have fun!
Best regards,
Geoff Bunza