Monday, June 6, 2016

HO Trains and DCC++: Part 1

The Boy has rediscovered the model trains. Several years ago we started by resurrecting my 1990’s N-scale layout, which is 2’-x-4’ and could slide under the couch.  This was followed by  Dad’s post-war Lionel O-27, which was augmented by modern Fast Track and my 1970’s banjo crossing. The Fast Track was a game changer - the stuff works perfectly. It’s reliable, easy to join and doesn’t cause derailing. This was the first time the young-version Boy didn’t get frustrated while running trains. We added 21st century Thomas with a whistle. Finally we added an HO scale Thomas and Friends set. He was able to put together the track with this set himself. It also made several airplane trips in a carry-on back and forth to the Grandparents. But, then the XBox replaced the trains.

Most recently, the Boy pulled out the HO scale trains, built his own layout, and began running a Union Pacific (UP) diesel, boxcar and caboose. He’s using my 1980’s power supply, which was advanced at the time because it had a low-pass filter on the throttle to simulate gradual acceleration and deceleration of the train. Modern control, however, uses Digital Command and Control (DCC). The UP diesel has a DCC decoder. When we bought it, I figured I’d buy a command and base station soon after, but never found the opportune time.

Then last month the Boy has been talking about DCC nonstop and then he discovered locomotives with a Bluetooth interface. Time to do something about it. After researching DCC and reading the NMRA standard, I realized it could be implemented on an Arduino. Using my Google foo, lo and behold, I stumbled upon DCC++. I didn’t think it was the right time to invest in trains with dedicated Bluetooth interface and I have a couple Arduino Unos sitting around - this is the ticket.

Install DCC++

The DCC++ base station code can be downloaded from GitHub here. I used the standard Arduino IDE v1.6.5 to install it. The first time around compiling, it failed with the error:

Accessories.cpp:66:20: fatal error: EEPROM.h: 
No such file or directory #include <EEPROM.h>

Turns out in Arduino, all #includes no matter where they are used must be declared in the .ino file. I learned this interesting tidbit hereAfter adding #include <EEPROM.h> to the .ino file it works fine. I posted an issue to the GitHub site, so I imagine it'll be fixed. This is the first time I've contributed to someone else's code, although it is a pretty minor bug fix.

Prepare the Arduino Motor Shield

Per instructions here, I cut the indicated traces. The picture below shows the Vin trace cut - this disconnects the external power supply used to drive the motors through the motor shield from the Vin pin on the Arduino.
There are also a couple of jumpers to be installed. The final configuration is shown below.

Test the Uno

Before powering the motor shield, much less trying to control a train, I wanted to make sure I could talk to the DCC++ software on the Uno. Using Hyperterminal, I connected to the Arduino and sent the <s> status command. The response is shown in the picture.


Test the motor shield

Now that the software was working, I wanted to test the shield before using a train. I measured the voltage out when powered on. Using the <1> command turns the power on, which should be a 5-kHz square wave with almost 12-V amplitude (there might be some voltage drop in the driver chip). Note the LED’s by the terminal block are now also illuminated greenish-yellow. My digital volt meter is pretty decent at measuring root-mean-square (RMS) AC voltage and indicated 7.9 V for an expected 12/sqrt(2) = 8.5 V. (9/21/16: I was re-reading this and realized this statement is not correct. The RMS of a square wave is the amplitude. The sqrt(2) factor is for a sine wave. The low voltage reading on the voltmeter could be due it's frequency response. Or, it's possible there's a lot of loss in the driver. I need a scope to diagnose it.) That’ll do.

First run - throttle control

Now it was finally time to try it out. The DCC++ wiki gives an example command to move the train: <t 1 03 20 1>. This command tells locomotive “03,” which is the default encoder address, to move forward at speed 20. It worked. It worked without any fiddling. It’s nice when things just work. Though it is kind of anticlimactic. 

In Part 2, I’ll talk about our experience setting up iPad control.


1 comment:

  1. This is cool if you have decoders in your loco's. I have older loco,s without dcc and am thinking of using the arduino and motor shield for throttles on my N scale loco'. Back awhile ago, I built two throttles with momentem, but this seems cleaner and easier.

    ReplyDelete