# Hooray I'm DCC-enabled!



## Shdwdrgn (Dec 23, 2014)

It's a small thing, but I finally have a loco going around my test circle under DCC commands. This makes me very happy to see a train running again. 

So I've been working on an Arduino-type base station, using code from DCC++ESP32 (which runs on an ESP32 and provides some wifi support). It seemed like it was working this week, so today at the train show I picked up a cheap SoundTraxx mobile decoder. Took an old Bachmann 0-6-0 and got some wires running from the engine to the tender and then soldered to the decoder. Nothing fancy, just the power pickup and motor leads. Then I had to figure out how to manually send DCC codes. Finally got everything working though and after some initial hesitation my loco is purring around the track dragging several heavy cars.

Eventually I will get another ESP32 onboard my 0-4-0 and get that set up to receive DCC commands as well. I had just decided that I better get a commercial decoder first so I could make sure my base station was working and start learning how to actually work with DCC. Hooray for my first baby steps!


----------



## Cycleops (Dec 6, 2014)

Good for you:appl::appl:


----------



## wvgca (Jan 21, 2013)

good deal .... and next is ?


----------



## Shdwdrgn (Dec 23, 2014)

Now that I have a small idea of how to work with DCC, I can get back to building the accessory decoder. Had trouble with the first computer chip I was trying to use (no supporting libraries yet) so I'm going to switch over to a regular arduino chip to get this up and running. I have a board that will control 16 servos, and want to see if these little ones are strong enough to flip my turnouts.

After I know what I'm doing with that, then I'll come back to building my own mobile decoder and getting a computer on the tracks again. And from there I'll expand the code so the loco can also send out its own DCC commands over wifi to control the turnouts and checking occupancy signals.

My test track is a simple loop with three short sidings and a combination of dual-gauge and standard tracks. The ultimate goal is to have two standard-gauge locos making the loop and swapping a couple cars, while a narrow-gauge loco also goes around the loop in between them and at random timings. Once all of that is working then I'll be ready to take it to a full-size layout and create even more complex routes.


----------



## mesenteria (Oct 29, 2015)

Well done, and I can 'hear' the excitement in your voice. Ain't DCC great??


----------



## CTValleyRR (Jul 26, 2014)

SG90 microservos are plenty strong enough to throw turnouts. I have 34 on my layout.


----------



## Shdwdrgn (Dec 23, 2014)

CTValleyRR said:


> SG90 microservos are plenty strong enough to throw turnouts. I have 34 on my layout.


I guess I didn't actually mention that, but yes, that's exactly the ones I'm working with as well. I paid a little more for one with metal gears, and it keeps jamming up, so I guess I'll stick with the cheap plastic.  I put in an order for five more of them yesterday, but I have two to play with now. Still struggling with the DCC++ accessory decoder code though, hopefully I can make some progress tonight when I get a circuit hooked up to the track.


----------



## Shdwdrgn (Dec 23, 2014)

Last week was painful on my brain. Spent the whole week trying to troubleshoot a basic arduino decoder only to figure out the the common pin 2 used for most arduinos to catch the interrupts from the opto-isolator chip doesn't seem to work the same on my board (an arduino pro-micro clone) and I may also have some bad opto chips. I was finally able to get a function decoder working using the second serial Rx line and interrupt 2 so I moved back to decoding accessory signals and ran into more problems. I think the Nmra code I was trying to us may be incomplete, but I found a different sketch that works perfectly so I'm up and running finally.

My next goal is to move this to a smaller package. I would like to use an ATtiny85 with a 16-servo controller board, but I've heard there are timing constraints that would prevent both DCC and I2C from working together on the same board. Hmm maybe I could connect two ATtiny's together with a low-speed serial line, convert the incoming DCC signals to a DCC++ style text string, and send that to a second ATtiny that runs the servo board? Sounds messy but I build a board with both chips sitting side-by-side and it would still be the same width as the servo board. Ideally I could design one small PCB that holds two surface-mount ATtinys, the opto-isolator chip, and a small voltage regulator circuit to draw power directly from the rails, and the whole thing should only be about an inch square and plug directly into the servo controller.

Ah well, lots to play around with, but maybe I can find another similar-sized chip that has built-in I2C and doesn't run into the same timing constraints. The ATtiny just seemed ideal because I really only need three pins -- one for the DCC input, and two for the I2C bus.


----------



## Shdwdrgn (Dec 23, 2014)

After figuring out the interrupt issues and spending time rewriting the code to work with the servo controller board, I realized I had yet another problem. I was never able to set any CV on my loco except for #3 and I brushed it off while I was working on the accessory decoder. Except that code also allows you to change settings using CVs, so for example I should be able to change the range of motion for each servo individually... but I couldn't. After about two weeks of banging my head against a wall I realized that I really do understand what *should* be happening, and debugging the DCC info led me to believe that my packets to program a CV on the mainline were not including the cab number, but it looked like the CV number was being repeated twice (thus explaining why I could program CV3 on the loco which was set as cab 3).

It turns out that while the DCC++ code I was using worked fine on the programming track (which everyone BUT myself was using), it had an error for sending CV codes on the mainline. Yep, the array being referenced was sending the CV number twice and skipping the cab number. Changed a couple of 1's to 0's, recompiled, and poof everybody is happy now.

So I finally have a fully functional accessory decoder and I can changed the CVs in my loco now. Hooray! The next step is to pick up some 1x2 lumber and frame out the bottom of my test track and then get the servos installed to operate my turnouts. Wheeeeee

After that, I'm debating if I want to jump into building an arduino for S88 sensors, or get a new ESP32 installed on my 0-4-0 and try to get it working as a loco decoder. My evening time is quite a bit more limited now that Spring is here, so many outdoor projects to squeeze into the schedule! However assuming there are no more complications both of the DCC projects should be fairly easy to get running.


----------



## MichaelE (Mar 7, 2018)

mesenteria said:


> Well done, and I can 'hear' the excitement in your voice. Ain't DCC great??


Can't wait to find out for myself!


----------



## Shdwdrgn (Dec 23, 2014)

MichaelE said:


> Can't wait to find out for myself!


I was worried at first that 1980's technology would really hold me back, but as I'm digging into it more what I'm finding is there are a lot of different technologies that all play their parts to work together for a larger system. This is great because you can use the pieces in their standard capacity and put together a working system pretty quickly (if you're not trying to debug something new), or you can write your own control system to really push everything to the next level. I'm still playing catch-up to all the things that have come out in the last 30 years but I'm really excited about the possibilities of bringing everything together in my own way some day.


----------



## Shdwdrgn (Dec 23, 2014)

So while perusing ebay the other day I stumbled across something called the arduino nano strong. It is built from the atmega328p but still includes a USB port. What really caught my eye was the particular layout of the header pins...










Yeah that looks to me like it is just calling for a bunch of servos! I found a listing on Amazon for a two-pack for $10, and they came in yesterday. So the first thing I needed is an optoisolator set up to pull DCC from the track, and since those header pins also provide a handy source of power and ground, I just wired up a small unit to plug in inline.










(Yes the original sales image has the header colors in the wrong locations! The black ground pins are supposed to be to the outside of the board.) Basically I just soldered the other components directly to the chip legs and heat-shrinked the whole bundle. After a bit of playing around I have it working fine with Geoff's 15-servo accessory decoder code (modified for 18 servos and no functions, LEDs, or anything else), but there are some caveats...


Pins 0-1 are dedicated to serial transmissions and do not seem to work reliably as I/O ports
The DCC input can ONLY be connected to pin 2 or 3. The atmega328p only has external interrupts on those pins.
Pins A6 and A7 are exposed through the headers, but seem to be dedicated analog pins (possibly input-only), so I might not be able to use them for servo outputs either
The power buss on this board is no where near robust enough to actually handle the current of a bunch of servos all running at once.
There is also a matter of the physical space. Because of the way servo connectors are made, you can only plug in a few of them side-by-side before they crowd each other. However I think I can fit in a row of six.

The biggest problem will be to power supply to the servos. I don't want to have to rework this board though, however I think I have an idea. There are connections in each of the three sets of headers that will be unusable for servos, so I was thinking I could attach my dedicated 5V power supply to one of the header rows in each of those three sets -- thus providing a much more local source for the servos. The board traces then should never have to carry the current for more than 6 servos at a time. I have a pack of 2-pin headers with wires on order to give this theory a try.

After tossing out the pins I can't use for servos, I still come up with 16 usable pins. This board is only 1-9/16 inch square, so that's packing quite a bit of functionality into a small space I think. Price-wise it's not bad either... $5 for the arduino, 50 cents for the 6n136 optoisolator, pennies for the 10k and 1.5k resistors, a 270pF capacitor, and a 1N4148 diode, plus a few bits of wire. All in all, probably around $6 in total cost to run 16 servos via DCC? If I could find a suitable surface-mount optoisolator this little circuit could be laid down on a small board and then sealed up in heat-shrink tubing and probably be no larger than the plug to connect it to the arduino.

One thing I did notice is that since this uses the SoftwareServo library, the servos do not stay locked in place like they would if they had a continuous signal. While I don't think a train passing by would be enough to move the servo, I do believe that over time the vibration of operating trains could potentially cause a servo to drift a bit and lose the lock on the turnout points. If all turnout positions were 'reset' each time a train approached, though, that would negate any possibility of that ever causing a problem.

So overall I'm really excited about this board. And of course the same setup could also be used to control LEDs and other things around the layout. So far there only seems to be a single seller carrying this board (DIYMore) so I would assume they created it, but it is probably only a matter of time before others start carrying it and the price comes down a little more.


----------



## wvgca (Jan 21, 2013)

there are about two dozen listings on fleabay for the strong ....
similar pricing so far, yes, it's good for hooking up servo's !


----------



## Shdwdrgn (Dec 23, 2014)

Ah so more people are offering the board already... I figured it would pick up quick.

I keep thinking about the idea of building a small optoisolator module specifically for DCC. If folks had a plug&play module readily available it seems that would take a lot of work out of trying to set up an arduino-based decoder. And so many people are looking for solutions where they don't have to solder components together. Think I'll at least check the datasheet for the 6N136/137 and see if I can find a compatible surface-mount part number.


----------



## wvgca (Jan 21, 2013)

a few years back i used the 6N137 [in thru pin style] for DCC pickup for a booster, didn't take much space there, in a 43A booster, lol


----------



## Shdwdrgn (Dec 23, 2014)

Agreed an 8-pin DIP doesn't take much physical space, however I had in mind a small board that could go inline between the arduino and the track as a simple cable. I did find an HCPL-0600 which appears to be the SOP8 version of the 6N137 and got a few on order. Maybe I'll finally break the ice and try out one of those PCB shops to knock out a few. Guess I'll have to make a new tip for my iron too, the old one is so blunt it would crush the poor smd parts. 

I'm still trying to figure out if there is any significant difference between the 6n136 and 6n137. It seems like most people prefer using the 137 even though the circuit for the 136 uses one less part, so maybe the 137 has better signal separation or something? Ah well.


----------



## Lemonhawk (Sep 24, 2013)

This thing is just a little smaller that the Nano on a breakout board (2" x 2"). Lot less height, however! the breakout board might have a little better regulator on it to provide 5v.


----------



## Shdwdrgn (Dec 23, 2014)

I don't know that any of them are going to have a regulator large enough to supply a bank of servos. I believe the regulator on this model is only good up to 500mA. The specs for the little SG90 calls for around 220mA to move it and up to around 650mA when the motor is stalled. I would want the board to be capable of handling at least 1/4 of the motors simultaneously so I'm not going to count on any onboard regulator.

I did find a calculator for PCB trace widths last night. The 5V trace on the boards appears to be 1/32" wide, so I rounded down to 0.030" which should mean it can carry about 1.57A. So if I run all six servos in a bank (assuming they don't stall) I only need 1.32A and the trace for that group should be fine. In actual practice I probably won't be throwing more than a pair of turnouts at one time and it should never be a concern -- but then again I could also go the extra-cautious route and solder a 16ga wire across the 5V and ground pins on the bottom side of the board, and there would never be any doubt.


----------

