I see that the Arduino can be used to control lighting, motors, and sound. I am not familiar enough with this item to know if it is usable for an electrically ignorant individual on a model train layout. Anyone know of protocols or programing that can be used to control lighting, motors and sound on a model train layout or how the information can be found?
Replies sorted oldest to newest
What cant you do with Arduino?
These little programmable boards are being used to control robots. That means attaching a motor driver board (a.k.a. a "shield") and driving motors, lights, sounds, sensors, you name it. They can be controlled through RF, bluetooth, wifi, or programmed to operate autonomously. Absolutely no reason why any of that could not work with model train.
You can spend months sifting through all the Arduino projects and stuff on the internet but as far as I know, no one has applied any of this to O gauge trains.
That means you would have to do all the research, hardware assembly, and coding yourself.
And while your at it, research the Raspberry Pi. Its a full-fledged computer that is about the size of a credit card, with its own operating system. It has video and audio outputs and you can even attach a camera to it. It has programmable pin-outs for LEDs and PWM for motors (with appropriate motor driver board attachment). Using wifi and remote desktop software, the Pi can be controlled by any other computer or tablet through a network connection.
Again, lots of documentation out there. You will have to be the one to learn it an apply it to your application.
I just bought an Arduino robot and a Raspberry Pi for my daughter for her birthday. We will be learning all this stuff together.
Go here: http://www.adafruit.com/products/170 for a good Arduino starter kit (it comes with a book)
The site also offers lots documentation, project information, and helpful support.
I've started to experiment with the Arduino Nano and the JMRI open source control program to potentially control my O ga layout. At this point I have a couple bench circuits working with a simple control panel drawn up in Panel Pro. These two sites got me started:
http://www.utrainia.com/tag/cmri
Here's another site geared toward learning electronics including Arduino, Raspberry Pi and others. Lots of electronic parts, tutorials and sample code here. They have lots of beginner kits and stuff to get you started as well. SparkFun Electronics
I've used the Arduino as the centerpiece of a complete automated control system; the Arduino reads insulated third rail sensors and RFID readers, controls layout signals, accesories and turnouts, sends commands with the DCS and Legacy command bases, and interfaces with JMRI running on a PC to provide a touch-screen layout interface.
There aren't any out-of-the-box applications for the electrically ignorant… but I was completely electrically ignorant before I started! Nearly everything I've learned about electronics has been through these projects. I started with a very system using a Basic Stamp and a loop on the living room floor, and worked up to the installation I have today.
Professor C.
Do you have any suggestions as to what to start with to learn the system?
I also would be interested. I am presently trying to learn enough about CTI Electronics for running my signal system. I have only invested time and no funds so far. If Arduino would be better now is the time. So far CTI is actually appealing since there is no reason to mfg hardware or write software code.
mikeg
Mike, you might begin with one of the Arduino starter kits. I haven't used one, but a lot of companies make them (including the 'official' starter kit). Start with simple experiments like rigging a pushbutton to light an LED.
Outside of those, the building blocks are basic digital electronics and basic C++ programming. I don't know of a good intro to digital electronics, but the Arduino website has a reference page documenting most of the C++ and Arduino extensions you use in programming. There are lots of C++ resources on the web, but most of it is oriented towards computers and may not be as helpful for Arduino.
In my case, one of the first things I did was hook up the Arduino to the TMCC command base, which required me to learn about serial interfaces and converting voltages from what microcontrollers output to what RS-232 devices expect. Then I wrote some programs to control simple locomotive functions like a horn from the Arduino.
If you're interested in interfacing the Arduino to TMCC/Legacy, consider an Arduino Mega.
The Mega has 4 hardware serial ports, meaning you can dedicate one to talking to a serial device like the Command Base while one or more are connected to your computer. If you are using a regular Arduino you may have to use a software serial port which is more complex and limiting.
Professor,
Thanks. I will talk to my local electronic shop about the starter kit and will work on lighting first. Then the mega stuff.
I am diving in as soon as possible.
Professor,
Thanks. I will talk to my local electronic shop about the starter kit and will work on lighting first. Then the mega stuff.
I am diving in as soon as possible.
There is small book by one of the Arduino founders (Massimo Banzi) that does a real good job of explaining the Arduino in simple layman's terms. Very easy read and easy to understand. It's titled "Getting Started with Arduino". I think I got mine at Radio Shack. I think for someone new to all this it would almost be a recommended read. That might be a good thing to start with and then go to one of the kits.
Make sure the kit has a manual also. I got the SparkFun Inventor's Kit for Arduino last winter to get going. They had a special sale and it was 60% off and free shiping. The sale is what pushed me over the edge to get started. The kit I got has a good manual with plenty of illustrations and lots of parts and example experiments. It includes everything you need to get started. Pre-written code is available for all the experiments as well as other tutorials and information on their website. This could be the case with other kits from other sites as well, haven't really looked at many of those?
Professor Chaos and the others are much more advanced than I am, I'm only just past the getting started point. It is very interesting and a lot of fun though!
Do a search on the Appalachian & Ohio discussion forum for "Arduino". Bob from that group (who posts here occasionally as riogrande491 I believe) has utilized the Arduino in any number of projects. I think he does something with programming for his real job, so he would have an advantage with that aspect of using the chip.
Jim
I have utilized the Arduino for several projects on my layout. I picked the Arduino for
several reasons. The Arduino is open-source which means that the hardware is reasonably priced ($12 - $15 on ebay direct from China with free shipping) and development software is free. I receive them in about 7 days. Some include a USB cable for programming. Once programmed the program is retained even when power is removed. The Arduino has 14 digital input/output pins. Plugin interface boards (called shields) are available. I have used two different shields so far. One of these shields contains relays that can handle AC power for trains. Another shield contains a 9 X 14 LED array.
I use the Arduino + relay shield to control a Marx elevated train. It stops the train at
the station and leaves it in neutral and the train makes station anouncements. It then
cycles it into reverse for a very short burst and then into neutral where it pauses
until the train sounds the horn. It then cycles into forward and leaves the station.
After a delay the cycle is then repeated.
Another use is to blink three red lights on top of a bridge. The previous electronic
circuits had a tendency to get into sync and I wanted them to be randum. I programmed the Arduino to flash three outputs at slightly different rates.
The Arduino + LED array is mounted in an old Revel Blimp Kit and makes a digital
display. This will be hung from the ceiling and fly over the layout. The display is
programmed with scrolling messages. The program to make the LED matrix scroll messages is available on the internet so all I had to do was type in my message.
The best venue I have found to learn the Arduino is Jeremy Blum's series of YouTube videos - here. He makes it easy to understand and covers both programming and basic electronics. Give them a try, starting with Tutorial No.1, and you will like it.
Enjoy!
Alex
Mike,
Another great source of info for the Arduino is a book titled "Arduino Cookbook". Is is pretty hefty and is full of example applications with the associated code. The coding is fairly easy to learn given all the examples of how to do a specific things in the book. As someone mentioned previously there are lots of free programs available on the internet.
I'd be really surprised if there aren't some train related projects already on the net.
There is just a ton of stuff that you can use the arduino for on a train layout.
I bought an Arduino 2 years ago to fool with while I was recovering from some surgery. It was great fun learning it, especially for led lighting, relays and photo sensors. The parts are generally quite cheap. Some of the shields can get up to $30 or $40. ( and that is old info). One of the really neat shields provides wireless capability..
Ed
For those of you who are paying attention to this item: There is another device called the Raspberry Pi which does the same type of thing. It too has tutorials and programing to learn.
I have picked up an Arduino beginners kit and have started the led lighting tutorial. This will come in handy for lighting on the layout.
And dont forget the BeagleBone.
About the same size and price as the RaspberryPi but with more pin-outs for more experiments.
I have a lot of great ideas where I can use a programmable controller on the Choo Choo Barn layout but a have a few concerns. First is reliability. My display operates 7 hours a day, 7 days a week for 9 months. Will an Arduino hold up under this pressure? What happens when there's a brown-out or a total power failure while it is operating? One of my ideas incorporates stepper motors, servos and MPEGs for sounds. Will an Arduino with add ons handle such a thing? I need a contoller that will run a series of commands, working the motors, servos and sounds and then rest for a while and start all over again.
I fear that my brain is getting too old to learn how to write code but I imagine that once I've done one sequence of events, I can use this is a building block for many more exciting animated scenes. I can build the hardware, the animated parts, but I'm no a coder.
Anybody out there willing to tutor me?
YES...YES...YES...
There is no reason that the Arduino can't run continuously for long periods of time. You can either use a UPS for brownouts or do an external watchdog reset.
There are stepper motor boards that are very cheap for the Arduino, and it has a number of PWM outputs that you can control RC servos directly with, very handy. MP3 sound would be an add-on, they also exist, though I haven't actually fooled with them.
Hey gunrunnerjohn, was the third YES in answer to my "tutoring" question? I've bought the "Dummies" books but they didn't have in them what I wanted, too general.
Well, I'm not sure I'm wanting to get into trying to teach someone programming, so make that YES...YES...UHH anyone?
Hey gunrunnerjohn, was the third YES in answer to my "tutoring" question? I've bought the "Dummies" books but they didn't have in them what I wanted, too general.
I think if you select a project, and do some code that you think is in the right direction, then post it, then I'm pretty sure that folks will be unable to resist redirecting you.
Makes it interesting to all.
If you're interested in interfacing the Arduino to TMCC/Legacy, consider an Arduino Mega.
The Mega has 4 hardware serial ports, meaning you can dedicate one to talking to a serial device like the Command Base while one or more are connected to your computer. If you are using a regular Arduino you may have to use a software serial port which is more complex and limiting.
I have had fun using the Arduino to control accessories, including a Choo-Choo barn replica carnival ride.
I did most of my learning by using examples from thee internet.
Tom -- I emailed you off line.
Hi Randy - let's see if I can answer some of your questions!
Easy things first: Check this article about serial voltages. The output from the Arduino serial pins is 5V TTL; you need to invert and regulate the voltage when communicating with an RS-232 device like a computer or the Legacy base. A basic MAX232 circuit does this as shown in the Sparkfun tutorial.
I have two RFID readers hooked up, though you really only need one. The RFID reader identifies the train when it first enters the mainline, and it is subsequently tracked by occupancy sensors. I use the ID-20 reader available from Sparkfun; each one is connected directly to a serial port of the Mega. Note that you can attach different input and output devices to the same serial port if at the same baud. For example, I have one serial port where the input reads an RFID reader and the output sends commands to the Legacy base. But for 10-12 readers you would need some sort of multiplexing scheme.
I put the RFID readers in a trackside structure, or behind a billboard, as shown in the photos. RFID tags are stuck inside of a plastic tender, or the shell of a diesel. Steam locomotives with die-cast tenders have a particular piece of rolling stock associated with them to identify them to the system.
Under the track / under a locomotive doesn't work well because of all the metal around - not to mention the difficulty of burying the reader.
Attachments
Thanks Professor. So are you saying that you only use a single RFID reader on your entire layout - and always start your trains running from a specific set of known locations? And from that point on, since "you" (your Arduino or whatever, as programmed by you) are throwing all of the switches...you basically just need isolated rail triggers to know when that train has reached a given point on the layout. I.e. since you know that Train 1 was at point A (where the RFID reader is located), then that must still be Train 1 at point X -- since *something* is there and based on your control programming, it couldn't be anything other than Train 1?
I must be missing something, because if that was the case -- a known starting configuration -- then you wouldn't even need a single RFID reader...you could just keep track of train movements based on assumptions...???
That can't be right ;-) So how are you keeping track of so many locomotives on your layout all running around at once, without having a bunch of RFID readers? With conventional control, it was easy to just turn the center rail power off and on...but now we need to know *which* loco is at any given point so we know which serial command to send to tell it to stop and start. I was just ASSuming that you were using multiple RFID readers.
For sensing occupancy, I didn't want to connect the third rail directly to the Arduino. In part, because for TMCC you can't connect the earth ground (which is what the Legacy base is using for serial communication) to the outside rail common. There may be a way around this, but I had already developed a circuit to convert a noisy AC signal into a clean 5V input.
The basic sensor circuit is shown below, though I have altered some of the resistor values from the diagram. The signal from the track goes through an AC optocoupler, and then the 'debouncing' circuit uses a resistor/capacitor to smooth the signal. A Schmitt trigger then ensures the input sees a clean transition. Same arrangement you would see if you looked up switch debouncing circuits, but much longer debounce time than for a typical switch.
It might be possible to handle all that debouncing entirely in software, but when I started I was using a microcontroller with very limited memory and power, so I wanted to do as much in hardware as possible.
I designed a circuit board to consolidate all the occupancy signals into a single stream of pulses that can be read by the Arduino's SPI interface. All it does is feed the outputs of the Schmitt triggers into four shift registers which are daisy chained together. Same idea if you look for examples of how to expand Arduino inputs with shift registers. So in the end, the Arduino just loads in four bytes over the SPI interface, which represent the states of 32 occupancy sensors.
Hope that is helpful!
Attachments
Thanks Professor. So are you saying that you only use a single RFID reader on your entire layout - and always start your trains running from a specific set of known locations? And from that point on, since "you" (your Arduino or whatever, as programmed by you) are throwing all of the switches...you basically just need isolated rail triggers to know when that train has reached a given point on the layout. I.e. since you know that Train 1 was at point A (where the RFID reader is located), then that must still be Train 1 at point X -- since *something* is there and based on your control programming, it couldn't be anything other than Train 1?
I must be missing something, because if that was the case -- a known starting configuration -- then you wouldn't even need a single RFID reader...you could just keep track of train movements based on assumptions...???
That can't be right ;-) So how are you keeping track of so many locomotives on your layout all running around at once, without having a bunch of RFID readers? With conventional control, it was easy to just turn the center rail power off and on...but now we need to know *which* loco is at any given point so we know which serial command to send to tell it to stop and start. I was just ASSuming that you were using multiple RFID readers.
You have the basic idea! On my system, a train must 'enter' the layout at a defined point - where one of the RFID readers is. Once it passes the RFID reader, the system tracks it by occupancy. Trains are assumed to only go forward; since I designed the system primarily for automatic running this is not a major limitation.
The way I have things set up, it is not actually necessary for a train to start on a siding. Layout blocks only become 'active' once an automatically controlled train enters them. So, I can stack a few trains behind each other on the mainline, and have them pass by the RFID reader (thereby 'entering' the system) one by one.
Of course, one has to make sure the trains that have already entered the layout don't progress into the inactive blocks before they're vacated!
For conventional control, RFID would not be necessary. I am controlling the trains with TMCC and DCS, so the system has to know the ID# of the locomotive to issue the proper commands. Hence each locomotive has one or more tags associated with it, either attached to the locomotive itself, or to a particular piece of rolling stock associated with that locomotive.
Very interesting, and it certainly gives me food for thought. That sounds much simpler than populating the layout with RFID sensors.
In lieu of grounding an insulated rail section to trigger occupancy, I might look at one of the commercial IR sensors out there. They will close a relay and stay latched anywhere from .5 to 3 seconds after proximity is no longer detected - so no bounce. Or use an insulated rail section to close a relay, and figure out a way to keep it from chattering... I presume any of these relays could be set up to deliver the appropriate high or low signal to any of the Arduinos many digital inputs.
If anyone else has done this, I would love to hear about it.
Thanks Professor!
Randy
If it is 3 rail,I am just curious why you dont use a relay and an insulated rail. The relay contacts are open or closed,easy to adapt for binary logic. A capacitor across the coil would eliminate "bounce". Not a criticism just a question.
Dale H
For sensing occupancy, I didn't want to connect the third rail directly to the Arduino. In part, because for TMCC you can't connect the earth ground (which is what the Legacy base is using for serial communication) to the outside rail common.
I've used the Arduino as the centerpiece of a complete automated control system; the Arduino reads insulated third rail sensors and RFID readers, controls layout signals, accesories and turnouts, sends commands with the DCS and Legacy command bases, and interfaces with JMRI running on a PC to provide a touch-screen layout interface.
There aren't any out-of-the-box applications for the electrically ignorant… but I was completely electrically ignorant before I started! Nearly everything I've learned about electronics has been through these projects. I started with a very system using a Basic Stamp and a loop on the living room floor, and worked up to the installation I have today.
That's very clever and interesting, Dale -- thanks for sharing. (Also, Mr. Webmaster, when reading Dale's earlier post, I learned how to embed a link in a description, so I won't paste raw links again!)
I love the way you have set up delays for the long-long-short-long horn blast -- so creative!
Years ago I had a conventional layout that used similar relay logic as you are using for your SuperStreets -- basically it kept multiple motorized units running on a single loop without slamming into each other, much as you have done. It worked, but was pretty herky-jerky.
I also designed a relay circuit that allowed four trains on a large loop with two sidings. Two trains would sit in opposite sidings, while the other two trains could circle around until they entered the siding that their counterpart had just vacated. Then they stopped, and the two trains that had been sitting would do the same thing -- in the opposite direction just for fun. So four trains, with two running at any given time. Each of the two sidings required five relays and several insulated rail sections, and I connected them together so no train would be released until all trains were in sidings. So ten relays total, and theoretically I could have added any number of additional sidings for two more trains at a cost of five relays each. It was a lot of fun and worked flawlessly.
But when I look at what Prof. Chaos has accomplished, it is in a completely new realm. Trains taking seemingly random paths, often (but not always) running in opposite directions, three trains at once, and never colliding or getting into a standoff with an opposing train. As much as I love everything electromechanical (I also collect antique pinball machines) and postwar, I am fascinated and enamored with microcontrollers and software. TMCC may be technically outdated, but I love the relative simplicity. It does everything I could want, and I can get my head around it -- so I'm excited to dive in.
Thanks for sharing the information, and for your thoughts.
Randy
If it is 3 rail,I am just curious why you dont use a relay and an insulated rail. The relay contacts are open or closed,easy to adapt for binary logic. A capacitor across the coil would eliminate "bounce". Not a criticism just a question.
Dale H
Relays would work, but wouldn't you still need some debouncing of the output for digital processing - don't the relay contacts bounce like switch contacts?
So on the input side it seems like the choice is between 32 relays, + 32 rectifiers + 32 capacitors to dechatter, vs. 8 quad optocoupler ICs + 4 SIP resistor arrays to limit current. The latter is more compact and easy to assemble (once you're making PCBs).
The Dark Side is strong, Dale… embrace the power of the Solid State.
Relay contacts most certainly do require debounceing, either in hardware of software.
Randy, believe it or not it wasn't that difficult… I started small with a two-sensor system running on a Basic Stamp on the living room floor and worked up from there. There are lots of resources for learning digital electronics, and it was mostly a matter of adapting and combining bits and pieces I found here and there.
For designing the boards, I used a program called DipTrace, and generated Gerber files that you can send to a board house to be fabricated. It was a lot of fun! I'm not going to embark on the board-selling business, but I have put the Diptrace files and the Gerber files in my Dropbox public folder if you want to take a look: try this link and look in the AC sensor folder.
You will also find my Arduino code ("TrainControl") there. (You will need to install the libraries in the libraries.zip file). Unfortunately the system has grown very complicated so it is probably hard to understand; it has manual and automatic train running, control of layout signals via two different circuit interfaces, TMCC and DCS interfaces, monitoring of layout switch and accessory status, stored data for my layout configuration and locomotive collection; a JMRI interface, and now even a wireless remote control for my gantry crane.
It started out as just automatic train routing… depending on the operating mode, trains always seek the most permissive path, or follow the current turnout path unless it is a red signal. There is also a 'reservations' system where trains are assigned a priority, and higher-priority trains can grab a block ahead of the current path unless another train absolutely needs it. The idea was to make freight trains sit and wait for a passenger train (contrary to current Amtrak practice!). A train can also be designated manual control, in which case the system will attempt to route automatic trains around an operator-controlled train. The automatic logic is a complicated mess and I am not sure I even understand it any more!
I use JMRI to provide a touch-screen interface to the system, but all the logic is handled by the Arduino. The JMRI interface is one of the more complicated parts of the system. I emulate the C/MRI protocol to pass data back and forth between the Arduino and JMRI, and have some scripts running on JMRI to generate TMCC commands that are passed to the Arduino for transmission to the Legacy base. There is a screenshot of the JMRI panel in the Dropbox folder.
The issue with not connecting earth ground to outside rail has to do with how the TMCC signal is generated. Dale Manquen is the expert on this, but as I understand it earth ground and the outside rail are the two halves of the antenna for transmitting commands to locomotives. Connect them and you will short the TMCC signal.
So I wanted the system (which needs an earth ground reference for serial communication with the TMCC base) electrically isolated from the layout. Also isolated from spikes and whatnot that might occur during derailments or from solenoid-powered accessories, which might not be appreciated by the digital electronics.
Not really. but I am not sure since I do not have the Arduino and its requirements. Again logic controllers can be programmed to ignore this bounce as you call it.. Relays, unlike solid state electronics take a bit of time to throw,they are not instantaneous. With the capacitor they do not really bounce,or should not at least. AC relays are also wound not to bounce.
Dale, almost any relay will have contact bounce. It won't be apparent in the electromechanical way you use them, but it sure will as input to a microprocessor that can process discrete input states in a microsecond or two. Debouncing is a fact of life when you program a microprocessor.
As you say, the canned logic controllers have that built-in, but we're talking about programming something that costs $8-10 to do the task, not something that costs hundreds of dollars.
Randy, believe it or not it wasn't that difficult…
John
My understanding is that the contacts and blades are formed in a slight arc. Some relays are different. When it makes contact,there is actually a slight wiping action, so no bounce really occurs. This maintains constant contact as long as the input voltage is there. Second there is a throw in voltage and drop out voltage. This is usually designed for a smooth operation to prolong contact and blade life. Relays would not last so long if they bounced.
Dale, trust me, virtually all relays have a period of uncertainty of the contact. You are free not to believe it, but I've interfaced many micro's to relay circuits, and debouncing was ALWAYS required.
Here is the Arduino controlling a scrolling LED display as mentioned earlier in this thread.
https://www.youtube.com/watch?v=pugB0VJUIas