Trains.com

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Arduino in model railroading

13169 views
34 replies
1 rating 2 rating 3 rating 4 rating 5 rating
  • Member since
    March 2014
  • 16 posts
Arduino in model railroading
Posted by Walt - S on Monday, October 26, 2015 3:04 PM

I did a search for "Arduino" and all I got was some video links to a DCC controller.

I'm really surprised that there is not more interest in using this versatile and inexpensive device in model railroading.  I have a small N scale switching layout, and never have the need to run more than 1 train at a time, so I built a throttle that used the Arduino's PWM (Pulse Width Modulation) output to directly feed the tracks.  This is essentially the same waveform that a DCC decoder supplies to the motor.  No, I don't have sound or lighting control, but I also spent less than $50 and I have far better low-speed control than I ever had with DC.  I programmed momentum and braking into the Arduino, but I took it out since I prefer to have tight control over the locomotive.

I'm in the middle of using an Arduino with a stepper motor to control a turntable.  Initially I will just have a "run" and "jog" function in each direction, but when I can get a detection circuit I'm happy with, I plan to have the turntable automatically position at the selected track.

Is anyone else making use of the Arduino?  If anyone wants my sketches, I'll be happy to share them.

 

Walt

  • Member since
    January 2008
  • 100 posts
Posted by jrcBoze on Monday, October 26, 2015 5:39 PM

I developed a system for signaling and have signaled about 1/3 - 1/2 of my railroad. Of course, I'm not by any means the first one to do so. You can read about my system at:  "warrena623.wordpress.com", including sketches (small programs) and wiring details. One guy I know of has had an article waiting for publication for quite a while, same kind of idea. There is also a group in Southern California doing Arduino applications to DCC, etc, with a Yahoo group called the 'arduini'. Then there is Dr. Geoff Bunza's blog on MRH ezine (Model Railroad Hobbyist).

It does seem a rather natural application of very flexible technology to model railroading.

jrc

  • Member since
    October 2006
  • From: Western, MA
  • 8,571 posts
Posted by richg1998 on Monday, October 26, 2015 5:46 PM

As was said, Being discussed in the MRH forums since about 2012. They are a bit more advanced there then here.

Search for Arduino mrh.

 

Rich

 

If you ever fall over in public, pick yourself up and say “sorry it’s been a while since I inhabited a body.” And just walk away.

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Monday, October 26, 2015 8:31 PM

 There's actually quite a bit. NMRA DCC library, Loconet library, and a CMRI library.

So you can make a generic DCC accessory decoder, a Loconet device, or a CMRI node out of them.

                     --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    September 2003
  • 10,582 posts
Posted by mlehman on Monday, October 26, 2015 9:11 PM

There's a fellow doing some work with Arduinos over in the Layouts forum on his On30 logging line.

I'm sure there would be interest in a TT stepper drive controller. I've wondered about how to do that cheaply myself since stepper motors now appear regularly in surplus shops and vendor catalogs. I have a nice NYRS drive on my big TT, but paid a nice sum for it, too.

My wife is a programmer and the hot little unit in town here is the RaspberyPi because of the CS and EE programs at UIUC. Supposedly, there's a build with it in JMRI that lets it run the layout, etc via Decoder/Panel Pro so you can use it as a dedicated computer controled via remoate over the network instead of needing a laptop attached to the layout itself

That's probably a bigger set of tasks than an Arduino could handle, but there's certainly some interest here from what I've seen in these new types of micro computers.

Mike Lehman

Urbana, IL

  • Member since
    September 2003
  • 10,582 posts
Posted by mlehman on Monday, October 26, 2015 9:16 PM

rrinker
So you can make a generic DCC accessory decoder...

That would be a very useful application, especially if it could drive multiple devices, such as serving as a swicth machine controller. Serve that up with some Macros adressable in the DCC formats used by different mfgs and you have something very useful.

But even a remote for a single turnout could be useful if the net cost was comparable to commerical DCC accessory products.

Mike Lehman

Urbana, IL

  • Member since
    February 2007
  • From: Christiana, TN
  • 2,134 posts
Posted by CSX Robert on Monday, October 26, 2015 9:45 PM

Walt - S
Is anyone else making use of the Arduino?

I've got a setup that I used at Christmas that consists of an Arduino, a motor shield, PIR motion sensor, and infrared receiver with a remote.  I have an N-scale loop of track that I place IN the tree and an HO scale loop that I place on the ground around the tree.  The system has two modes of operation - manual and automatic - which I can switch between using the remote.  In manual mode I can control the trains using the remote - volume up and down for faster and slower for one train and channel up and down for faster and slower for the other.  In automatic mode, when the motion sensor was activated, the controller would automatically start one train, then wait a few seconds and start the other.  After a short period of time it would stop one and then the other.  Using PWM to drive the motor shield, the starts and stops were never sudden but always gradually ramped up and down.

  • Member since
    September 2003
  • 10,582 posts
Posted by mlehman on Monday, October 26, 2015 11:26 PM

mlehman
the hot little unit in town here is the RaspberyPi because of the CS and EE programs at UIUC. Supposedly, there's a build with it in JMRI that lets it run the layout, etc via Decoder/Panel Pro so you can use it as a dedicated computer controled via remoate over the network instead of needing a laptop attached to the layout itself

This just in...my wife found the RaspberryPi and case she thought she had sitting around that a friend gave her when he left town. BNIB. Now if my buddy who is on the JMRI list can only figure out which files I need to set this up with JMRI. Supposedly, the whole thing runs off an SD card, but that's about as much as I know about it. Will start me own thread when that happens. Just wanted folks to know there's usually a couple of pots bubbling on the electronic stove around here, even though I'm an e-school washout (by choice).

Mike Lehman

Urbana, IL

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Tuesday, October 27, 2015 7:16 AM

 Arduino Nano is a chip-size unit effectively equivalent to the Uno - the Uno is easier to connect stuff to, so is better for development, but if you want to produce a few finished devices to install around your layout, the Nano is the one to use. They are under $3 on eBay. You need to add a few parts for the DCC interface (another couple of dollars tops) and then it can directly drive 4 servos for turnouts PLUS a bunch of LEDs for signals. You aren't going to get a commercial DCC stationary decoder for that price.

 And they are stupidly simple to work with. After a few tutorials I am almost having to unlearn some of what I know, lots of prior programming experience may not actually be a plus when learning to program Arduino.

               --Randy


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    June 2004
  • From: From Golden, CO living in Puyallup (Seattle), WA
  • 751 posts
Posted by Renegade1c on Wednesday, October 28, 2015 2:46 PM

mlehman

 

 
mlehman
the hot little unit in town here is the RaspberyPi because of the CS and EE programs at UIUC. Supposedly, there's a build with it in JMRI that lets it run the layout, etc via Decoder/Panel Pro so you can use it as a dedicated computer controled via remoate over the network instead of needing a laptop attached to the layout itself

 

This just in...my wife found the RaspberryPi and case she thought she had sitting around that a friend gave her when he left town. BNIB. Now if my buddy who is on the JMRI list can only figure out which files I need to set this up with JMRI. Supposedly, the whole thing runs off an SD card, but that's about as much as I know about it. Will start me own thread when that happens. Just wanted folks to know there's usually a couple of pots bubbling on the electronic stove around here, even though I'm an e-school washout (by choice).

 

I used a RaspberryPi with JMRI to control the signals on my layout (now disassembled due to a move). I have not yet got it to run in headless mode but was able to boot when I powered up the layout and be able to run the logic used to control the signals. I interfaced it with Loconet via a LocoBuffer USB. I also run my Speedometer off of the Raspberry Pi to speed match locomotives.


Colorado Front Range Railroad: 
http://www.coloradofrontrangerr.com/

flag

  • Member since
    September 2003
  • 10,582 posts
Posted by mlehman on Wednesday, October 28, 2015 3:03 PM

Well, that's a start. No signals on the narrowgauge, even one as busy as mine, but if it can keep track of that it should be able to deal with what I need. I need a WiThrottle server, a webserver, and turnout tables (and macros if I ever get that figured out.)

Mike Lehman

Urbana, IL

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Wednesday, October 28, 2015 3:12 PM

rrinker
After a few tutorials I am almost having to unlearn some of what I know, lots of prior programming experience may not actually be a plus when learning to program Arduino

any examples?

greg - Philadelphia & Reading / Reading

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Wednesday, October 28, 2015 5:23 PM

 Stuff like configuring the pins for different types of output - MUCH simpler than PIC (probably simpler than native Atmel too, but I never used them).

 Not really more than you get switching back and forth between multiple programming languages - some are 0 based for arrays and position indexing, some are 1 based. Some use If..Then..Else constrcuts, some use End, others use End If or EndIf to end an If block. Maybe not a common thing, but I am always working on different projects, wiringt PowerShell scripts one day. T-SQL another, VB the next. Then weeks with no coding of any sort. Nature of the beast working as a consultant.

                        --Randy


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    December 2001
  • 3,139 posts
Posted by chutton01 on Friday, October 30, 2015 11:12 AM

I first learned about Arduino's in an old-school RMC about layout animation, maybe late 2013, early 2014?  Shortly thereafter, I started stumbling across a wave of YouTube videos about using Arduinos, such that one handyman channel included a humorous mocking statement "Blowing out your lawn sprinkler system - now 100% Arduino free".


I haven't implemented anything with one yet, but obviously useful for things like crossing gate controllers (as this guy demonstrates) and traffic light sequencers (indeed, considering the incredible crappy real-world traffic light controllers on Long Island which routinuely fail/fall out of synch, perhaps they should replace those with Arduinos - it could only help).

  • Member since
    October 2015
  • 8 posts
Posted by MDLaFond on Friday, October 30, 2015 2:07 PM

While I don't have a turntable, I have created a 3 chime horn using the Mozzi sound synthesizer (an Arduino library), and am in the process of developing a system of turnout activation via ifrared (old DVD player remote).  I'm happy to share the train horn code, but just finished (with bug!) the turnout code.  

Tags: Arduino
  • Member since
    March 2014
  • 16 posts
Posted by Walt - S on Friday, October 30, 2015 3:08 PM

I'm encouraged by the response to this thread!

I have completed a breadboard version of a turntable controller.  The Arduino part of it has two pushbuttons, fast and slow, for each direction of rotation.  I'll publish the sketch and wiring diageams here in a few days, once i'm confident it is working properly.

I am using a stepper motor and timing belt from an old printer, and I turned an 8"disc with a groove around the perimeter for the belt, to drive the turntable shaft. The stepper motor already has a pulley that is grooved to match the belt, and just friction is enough to drive the large disk on the turntable.

On the train control side, as I said, I use the PWM output from the Arduino, connected to the track as in a DC configuration (through a motor controller shield).  One improvement I'd like to add would be to have the same position of the throttle knob result in the same speed from every locomotive.  Does DCC actually achieve this?  I was thinking, if there was some way to use the back EMF from the motor, that might provide an input to the Arduino that would be proportional to speed.  Any ideas?

 

Walt

 

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Friday, October 30, 2015 5:20 PM

 Yes, DCC does that, if you configure speed tables in each loco so that the same throttle positin results int he same speed for each loco. Your BEMF idea for DC would work, but you would have to have the same sort of speed table configured in your throttle (and everyth throttle, if there will be more than one) so that for each loco you associate a certain amount of BEMF with a specific speed - no two locos will ever be the same, they will either have different brands of motors or different gear ratios, or both. So you need to develop a table for each loco, so that throttle position X equals speed Y, and then when you adjust the throttle, the Arduino controller can increase or decrease the track voltage as necessary to maintain that specific speed. That's how it happens with DCC< except that it all takes place in the loco, with the decoder adjusting the motor PWM to keep the speed at the desired setting. There is no analog voltage involved with DCC. The track voltage is constant, and the drive from the decoder to the motor is PWM.

                                    --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    March 2014
  • 16 posts
Posted by Walt - S on Saturday, October 31, 2015 8:21 AM
Thanks, Randy. That makes sense. BTW, I'm applying the PWM signal to the track, not DC. I realize I'd have to use a selector switch for each individual loco - isn't that what you have to do on a DCC throttle? My dilemma now is how to measure the back EMF, and I've found a couple of Internet sites that come close to explaining it. It looks like I will have to abandon my motor controller shield in favor of a home-made H bridge in order to make that happen. I just love the fact that model railroading allows me to combine three of my favorite hobbies - railroading, electronics and computers! I'm so glad I got back into model railroading after a 25 year hiatus! So much has changed! Very exciting!!! Walt
  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Saturday, October 31, 2015 10:30 AM

 You could always build your own DCC system

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

If you google "arduino model railroad" you'll find tons of sites with projects of all sorts. There's a section on the Arduino forms for model railway control. There's also a Yahoo group on using Arduino in model railroading. In addition to looking at the web page hits on Google for arduino model railroad, switch to the video results - there are numerous videos on YouTube with demos of Arduino-controlled layouts.

 The major difference between switching between locos with DCC and with a system witht he config information in the throttle is that it makes each throttle more complex - and you need to find a way to synchronize the speed tables in each throttle. That's fine on an experimental basis where you only have one controller but on a more practical basis with multiple throttles in use it can be interesting. With DCC that information is stored in the decoder in the individual loco. so the throttle doesn't have to know about any of that, the throttle just says run at speed step 20 and the decoders makes the loco move at whatever speed it has been programmed to move at speed step 20. Storage of all this data might be an issue with Arduino, even ones with 32MB of program storage - for a large fleet you might need to use a CF card and read data from there for the selected loco (probably too slow for real time, but if you load only 1 loco at a time into the program memory, you can store hundreds on the CF but have fast response for the one being driven at any given time.). The other thing to keep in mind is possibly compressing the speed data - DCC decoders only have 29 data points, most decoders do interpolation so the speed tables can still work when used with the much finer 128 speed step mode (which is really about 125 steps, some are reserved). That uses 28 bytes, plus there are forward and reverse trim values that adjust the whole curve to compensate for differences in forward and reverse motion - gear lash, side rods on steam locos, etc). A true 128 step curve, or 256 (although 256 discrete pulse widths may be excessive, as in no noticable difference between speed 230 and speed 231 - 128 in DCC seems to be a fine compromise between memory and signal encoding and actual speed changes in the running loco) would of course require 128 bytes, plus two trim bytes, or 256, per loco. It would certainly be possible to encode such a table rather than use raw data, which could greatly reduce the storage requirements.

           --Randy

 


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    September 2003
  • 10,582 posts
Posted by mlehman on Saturday, October 31, 2015 10:33 AM

Walt - S
I just love the fact that model railroading allows me to combine three of my favorite hobbies - railroading, electronics and computers! I'm so glad I got back into model railroading after a 25 year hiatus! So much has changed! Very exciting!!! Walt

Walt,

There are people who grump about all the cool whiz-bang electronics (not all, some just prefer DC) as nothing but useless glitz. But I think your reaction is one that a lot of folks share. People worry about the health of the hobby, but it's new developments like DCC, computer-driven programs like JMRI, and plain ol' fashioned hacking like you're doing which broadens the hobby's appeal to younger folks, whether they're 15 or 55.

Mike Lehman

Urbana, IL

  • Member since
    March 2014
  • 16 posts
Posted by Walt - S on Saturday, October 31, 2015 1:02 PM

Mike said:

"...hacking like you're doing which broadens the hobby's appeal to younger folks, whether they're 15 or 55."

 

I'm 71 :)

Walt

  • Member since
    February 2002
  • From: Reading, PA
  • 30,002 posts
Posted by rrinker on Saturday, October 31, 2015 1:08 PM

 There's a cool Arduino project on Instructibles, basically a self balancing ride on like a Segway except you sit on it instead of stand, it uses the motors from an old motorized wheelchair, guy welded up a frame for the seat and used some high current motor driver board controlled by an Arduino to balance it and make it go when you lean, and you steer with a stick.

 Guy's 81!

I hope I'll still be playing around with this stuff when I'm 81. The whole phenomenon is just amazing. I'm just a little too old, having started long before there was an IBM PC and the closest thing to a microcontroller was the Intel 8048. I have some old magazines where Imsai advertised a single board machine based on the 8048 and in the add they had model trains shown as part of the "what you can control with this" angle. Usually the current crop of technology is the playground of the young, who grew up with that level of technology always around them. But in a way I think this had jaded them. Neither of my boysd, in their early 20's, is the least bit impressed when I show them my first computer, which I built from a kit and uses a hex keypad and LED displays to program. They're too abstracted from the hardware - I still remember most of the 91 instructions that old CPU had as its entire instruction set. Modern CPUs probably have more than 91 instructions just to add two numbers together. Programming a current Intel CPU in machine language is an exercise in futility. Even modern microcontrollers - you have C or C-like languages, or variations of BASIC (I have one for PIC chips), you don't program them by hand assembling programs like I did for my first computer. Things like Arduinos at least get you back in touch with the hardware - in high level machines running a modern OS< be it Windows, Linux, or OSX, far abstract the programmer from the hardware, everything is virtualized and accessed via some black box of a driver, they are deliberately made to NOT allow direct hardware access. Arduino and PIC and other microcontrollers are completely the opposite.

                   --Randy


Modeling the Reading Railroad in the 1950's

 

Visit my web site at www.readingeastpenn.com for construction updates, DCC Info, and more.

  • Member since
    July 2006
  • From: west coast
  • 7,667 posts
Posted by rrebell on Saturday, October 31, 2015 5:04 PM

Remember TI's being the most advanced and then the Atari's, my first and last attempt at programing.

  • Member since
    October 2013
  • 9 posts
Posted by martan3d on Saturday, October 31, 2015 9:10 PM

Can anyone point to a good bit/byte/packet level description of DCC?  I've read the nmra boiler plate and done many google searches but it's fairly dry out there.  I understand there is a basic packet and extended more or less but I'm not clear on things like the bi-directional components, etc.  I've got some hardware and code working that basically spit out the DCC stream in hex format but other than the address, I'm not sure what I'm looking at...

  • Member since
    June 2009
  • From: QLD, Australia
  • 1,111 posts
Posted by tbdanny on Tuesday, November 3, 2015 2:09 AM
Walt,

I discovered Arduinos about a year ago, and I realised I could use them to help with operations on my layout. The Bradford Valley Lumber Co is a small model railroad, designed for a single operator, with only one train running at a time.

To this end, I decided to use Arduino controllers to automate some of the tasks that would be done by others. I started with two 'operations controllers', one under each section of the layout. The one on the 'Camp A' (i.e. company headquarters) end provides a random choice as to whether the next train dispatched is from the schedule, or an extra with some empty log cars. This Arduino also drives two LED 10-segment bar graphs, with a shift register IC providing the required outputs. These graphs count up over a set period of time, to simulate loading and unloading at various sidings. The Arduino on the other side of the layout just drives a single LED timer at the moment. However, when I install water towers, these Arduinos will drive the animation for those.

I also built a wireless DCC system with two Arduinos and Xbee wireless modules. Now that that's done, I'm working on an automated staging system that ties into the DCC system, to automatically allocate and select tracks for trains entering and leaving staging.

The Location: Forests of the Pacific Northwest, Oregon
The Year: 1948
The Scale: On30
The Blog: http://bvlcorr.tumblr.com

  • Member since
    October 2013
  • 9 posts
Posted by martan3d on Tuesday, November 3, 2015 8:57 AM

Can you possibly elaborate on the wireless implementation?  I'm interested in doing something similar.

 

Martin

 

  • Member since
    January 2008
  • 100 posts
Posted by jrcBoze on Tuesday, November 3, 2015 9:23 AM

rrinker

"I'm just a little too old, having started long before there was an IBM PC and the closest thing to a microcontroller was the Intel 8048....

                   --Randy"

Would you believe punching cards for Fortran in 1971? No such thing as a 'personal computer' or even a desktop. The most advanced 'desktop' was a Wang programmable calculator with nixie tubes.

jrc

 

  • Member since
    July 2009
  • From: lavale, md
  • 4,678 posts
Posted by gregc on Tuesday, November 3, 2015 11:03 AM

martan3d
Can anyone point to a good bit/byte/packet level description of DCC?

there's a bunch of info on the NCE-DCC yahoo group

It looks like there's code for testing under the Files tabs in that group, including some code for a controller un Misc_NCE_Info.

Bear in mind, NCE uses PIC processors.  Mark Gurries web page has lots of info.

greg - Philadelphia & Reading / Reading

  • Member since
    June 2007
  • 8,892 posts
Posted by riogrande5761 on Tuesday, November 3, 2015 2:00 PM

jrcBoze
Would you believe punching cards for Fortran in 1971? No such thing as a 'personal computer' or even a desktop.

jrc

Yes, except for me I was learning Fortran IV around 1978/79 at California State University Sacramento (CSUS) otherwise known as Slack Stat, er Sac State.  My first class in Comp Sci was punching Holerith cars and running a stack through the card reader and waiting for the output to be put in a bin.  Not long after, the cats meow was to get a Heathkit terminal and old style modem that you plugged the telephone handset into and wrote your code using a text editor and then submit the job without the punch cards!  I found that coding was too tedious for me so I switch to chemistry and then geology, which is what I earned my B.S. and later M.S. degree's in.  But eventually I went back to IT (MCSE) to put food on the table and model trains on the tracks, but not before I took an assembly language class and wrote a two pass assembler - woo that was tedious.

Rio Grande.  The Action Road  - Focus 1977-1983

  • Member since
    June 2009
  • From: QLD, Australia
  • 1,111 posts
Posted by tbdanny on Wednesday, November 4, 2015 2:41 AM

martan3d

Can you possibly elaborate on the wireless implementation?  I'm interested in doing something similar.

 

Martin

I did a write-up here a few months ago, which shows how I put the whole thing together. 

I used a pair of XBee modules to cover the wireless aspect.  These are essentially transparent to the Arduino - as far as the Arduino is concerned, it's sending and receiving across a serial link.  The software aspect of the transmission was handled by the EasyTransfer library (http://www.billporter.info/2011/05/30/easytransfer-arduino-library/). 

Essentially, the controller reads the input from the rotary encoder, function button, etc. and puts it into a data structure. This structure is then put out over the serial link via the EasyTransfer library.  The XBee takes this data from the serial link, and sends it to the receiving XBee in the base station.  When the data is received by the base station, it's a reverse of this process.  The struct is read in, and the base station generates the DCC packets, allocates the locomotives, etc. accordingly.

The automatic staging system also uses the XBees - a spare one I had after experimenting with the various antennas.  The base station transmits the address of the loco to the staging controller, using the same XBee on which it receives from the controller.  As there's only one loco running at the time, it's safe for the staging system to 'assume' that the last engine allocated is the one entering staging.

The Location: Forests of the Pacific Northwest, Oregon
The Year: 1948
The Scale: On30
The Blog: http://bvlcorr.tumblr.com

Subscriber & Member Login

Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!

Users Online

There are no community member online

Search the Community

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Model Railroader Newsletter See all
Sign up for our FREE e-newsletter and get model railroad news in your inbox!