An Intro to Model Railroading with Arduino

If you are looking for basic information and ideas on how to use Arduino boards on your model railroad layout, you’ve come to the right place.

I happen to model in N scale, but everything I write about in this blog can work in any scale from G to Z. Everything I’m doing is independent of the locomotive control system, so most techniques and technologies work equally well on DC or DCC layouts. This is for all modelers in all scales using any control system.

What is Arduino?

The Arduino Project is one of the best known, and by far best developed open source microcontroller board systems on the planet. In their words:

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It’s an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.

Microcontrollers are integrated circuit chips that have the basic logic and input/output capabilities that constitute a simple computer. Microcontroller boards place that chip with a few essential external components on a board with power and ways to plug things in. Your program (called a “sketch” in the Arduino world) gets loaded into the chip’s permanent memory and runs efficiently with little overhead between your code and the device.

Arduino Uno R3

Arduino Uno R3

Arduino boards come in several designs that are broadly useful for model railroading. I recommend that model railroaders stick to the 5 volt boards for general purpose layout applications because the vast majority of sensor and actuator technologies use 5 volt logic. This will keep your life easy, and these are usually the least expensive boards in the family.

The issue with the 3.3 volt boards is that 3.3 volt peripherals are less common than 5 volt ones. You can use a 3.3 volt board with a 5 volt peripheral, but you have to use a level shifting circuit between the board and the peripheral. Board-to-board serial communication between a 3.3 volt board and a 5 volt board would require level shifting.

That said, 3.3 volt boards can be used where low power matters and you are not attaching to 5 volt peripherals; lighting applications come to mind. I haven’t gotten my hands on one yet, but I think its worth looking at the new MKR1000, a new 3.3 volt board that includes WiFi. With WiFi solving your communication problem, this is a very attractive board for lighting and other 3.3 volt applications in far parts of the layout.

The 5 volt boards most broadly useful for model railroading are:

  • UNO R3—The original style and most popular design. The UNO is a great first board and a great foundation device for your layout. The UNO was specifically designed to accommodate plug-in, stackable peripheral boards called shields.
  • MEGA—This is the big cousin of the UNO, equipped with a microcontroller that has much more memory and supports many more pins than the UNO, but will also work with UNO shields. While many are drawn to the pin count, the real utility of the MEGA is the relatively massive amount of memory available for code and data storage and manipulation. The MEGA is the board of choice for high graphics applications such as a touchscreen control panel.
  • Micro—This is one of the smallest boards of the family, measuring a mere 48mm x 17mm, yet it still has the same basic capabilities as an UNO. The two main differences from the UNO are that the Micro does not use shields and the power handling capability of each I/O pin is limited to 20 mA.
  • Nano—A little larger than the Micro (0.73″ x 1.70″), this little board is virtually indistinguishable from an UNO in all material aspects. This board is officially “retired,” but remains readily available from Gravitech and many other sources. A few shields have been developed for it, including Ethernet and WiFi shields.
  • Mini—This is a smaller version of the Nano, measuring only 30mm x 18mm. It achieves its small size by omitting a USB interface, so an adapter is needed for programming. This board is a particularly good choice for applications where it needs to be hidden in a tiny space and you are unlikely to be reprogramming it frequently.
Uno with Ethernet Shield

Uno R3 with an Ethernet Shield

Shields are peripheral boards that can be plugged into the pins (pins are the connections, or I/O points, for attaching things to the board) of a microcontroller board. Multiple shields can be stacked on top of each other to add interfaces and other capabilities to the microcontroller. A wide variety of peripheral boards in shield format are available from a robust and competitive international market. “Development” shields are available that are like prototyping PC boards, allowing you to create a custom shield.

What can I do with Arduino Boards?

These little boards are computers that can do anything from reading a wide variety of sensors to running devices like servos and communicating with each other using a variety of technologies and protocols.  Here are some common applications for model railroading:

  • Turnout Control—A frequent motivation for modelers to explore microcontrollers is the desire to use common off-the-shelf micro servos to run turnouts. Servos require a microcontroller; however, the microcontroller is not limited to just controlling servos. Any kind of turnout motor commonly used in model railroading can be managed by a microcontroller, either directly or—where voltage or power consumption require—through a switching device such as a transistor or relay. The advantages are simplified wiring and the ability to programmatically drive other actions or devices in response to turnout actions and status. If using micro servos instead of stall motor units, the cost per turnout is significantly reduced.

    Servos 1, 2 and 9 mount in their final positions.

    Servos used as turnout motors.

  • Block Occupancy Detection—Detecting block occupancy is a sensor problem, and the Arduino world is all about reading and responding to sensors.
    • For DC layouts, the ACS712 sensor provides excellent detection at a cost of less than $2 per block on eBay and elsewhere. For most small layouts, the 5A version will work; big layouts might need the 20 or 30 Amp ones. You won’t need a special ADC; the 20mA resolution you get with the internal ADC (accessed through the analog pins) is enough to detect your locomotives. A trailing car or caboose (several on long trains) that draws 20 mA or more from the rails (use a resistor or some other load such as lighting) will allow you to detect trains occupying multiple blocks.

      ACS712 Board

    • On DCC layouts, the alternating wave form of DCC rail power allows the use of current transformers. Although the ACS712 sensor works down to around 10 – 12 mA with a high resolution ADC, that will not detect resistor wheelsets or the newest non-sound decoders in idle mode when draw is around 5mA with all lights and accessories off. The better current sensing solution for DCC is current transformers. These are very sensitive; even with the 10 bit ADC built into an UNO you can detect a 10k resistor wheel on the track when using CT sensors. Current transformers are directly attached to an analog pin on your Arduino, or an input on an external ADC, and read by your sketch.

      Current Transformer

      Open market parts are inexpensive at around $2 each. Digikey sells them, as does RR-CirKits. I’ve found them singly and in small kits on Amazon and eBay. Alternatively, you can interface an Arduino with off-the-shelf BOD systems based on current transformers (such as the RR-CirKits system) that produce a digital (on/off) DC output signal.

    • All Layout Systems – Other options include various types of optical sensing, magnetic sensing or anything where a train can cause a sensor to change state. Using Arduino technologies gives you the flexibility to use several different sensor types at the the same time.
  • RFID Detection—RFID is a common application in the broader Arduino universe, and it has been used on layouts to identify specific locomotives and rolling stock. The only limitation is the size of the sensor coils and tags; currently available parts are best suited to HO or larger scales. RFID coupled with block occupancy detection is a big step toward fully or partially automated train operations.
  • Lighting—Lighting is an application where microcontrollers shine.  For example, an Arduino can turn lights (both LED and incandescent) on and off at random, in response to  some other action or according to a programmed sequence or schedule. You can use a special capability of microcontrollers—PWM – pulse width modulation—to finely control the brightness of a light over time, creating fades and other effects such as a fire or welding simulation. Using RGB LEDS you can synthesize 16 million colors by manipulating the relative levels of Red, Green and Blue output by the LED.
  • Signals—A subset of lighting that has to be singled out because you can easily implement layout-wide signaling logic and protocols using the Arduino platform. With turnout status (and block occupancy data, if you have it) at your digital fingertips (your digits? I think that is an Arduino pun), implementing signal logic is a snap.Station and Signal 5
  • Mechanical Animations—Want to turn a windmill or water wheel, raise and lower a drawbridge, run a turntable, run carnival rides or animate a grade crossing gate? You can do that easily with Arduino technology.  For mechanical animation you have your choice of servos, linear actuators or nearly any type of DC motor.  Inductive loads such as DC motors generally require a transistor based switching/power management system. Stepper motors, which can be positioned precisely, can be run with the help of a Darlington array or similar control device.  Motor control peripherals are inexpensive and plentiful in the marketplace.
  • Fast Clock Operation—You can use an Arduino to create and display a fast clock. Why just display a fast clock when you can do more? Your entire Arduino-based system can respond to the fast clock appropriately with lights, animation and whatever else you can think of. Integrating a fast clock into your master controller, and having it send time signals to other boards in addition to showing you the “fast time” strikes me as a real enhancement of layout operation and functionality.
  • Control Panels—Instead of creating a rats nest of wiring for a complex control panel, how about home-running every panel object back to a single connection point and using program logic to read buttons and switches, run LEDS and tell layout objects what to do? If you are doing DC with multiple cabs, simplify your wiring and let your UNO or MEGA run power relays to switch cab control. Using a networking technology (Ethernet, WiFi, etc.) a control panel can control the entire installation through the network interface. Adding local sub-panels to a master control system is easy using these methods. You can also go a completely different direction from the usual control panel designs and create touchscreen controls.

    Test Loop Controls

    Test Loop Turnout Controls

  • Communications—Arduino boards can communicate with other boards, using a wide variety of methods and protocols. Board-to-board serial communication works well without special peripherals. For broader communication among many boards, use wired Ethernet, WiFi or other wireless technologies such as Bluetooth or XBee. Communication technologies allow you to assign tasks to boards  “geographically” (where on the layout) or “functionally” (task type), and have everything function as a coherent whole. Using networking technologies  you can communicate with pretty much anything else that uses the same protocols, even things out on the Internet (that said, for security reasons I’d never expose my private layout to the Internet).
  • DCC Applications—For those specifically interested in interfacing Arduino boards with DCC systems, the Model Railroading with Arduino project, an off-shoot of the Open DCC Project, is all about working on direct Arduino/DCC interfaces. It’s technically complex but straight forward to interface an Arduino with the DCC command bus. Check out this project using an Arduino Mini to create a 17 function DCC decoder. Projects like that are not for newbies, but once you are comfortable with the techniques and technologies involved it’s very doable.

One of my goals is to make my layout semi-autonomous, making it a dynamic environment within which I can operate trains. I can accomplish this with multiple boards running a selection of the applications listed above, communicating and working together in a cohesive system.

Is it Hard to Program an Arduino?

The IDE available on the Arduino website (Integrated Development Envronment — the software you use to write, compile and upload sketches to your boards) supports C++, an object-oriented super-set of the C language that has been the mainstay of professional development for 5 decades. You don’t have to write in an object oriented (OOP) style if you don’t want to — procedural C is fine. If you’ve ever programmed in C, C++, PHP, JavaScript or any similar structured language, you are good to go.

Arduino was designed as a teaching/learning tool so it is ideal for learning the basics if you’ve never programmed before.  Get an UNO starter set that includes parts and instructions for some basic projects—that will help you get started with both circuits and code. The Arduino website is an excellent resource for beginners, with plenty of information, tutorials and user forums for support.

Where can I buy Arduino boards?

Sparkfun and Adafruit are official distributors of Arduino products in the US, including some unique versions they have created.  You can find official goods at their websites,, Digikey, Jameco, Mouser and elsewhere.

On Amazon and EBay, you’ll find counterfeit copies, usually at ridiculously cheap prices. This is a touchy subject because the vendors of counterfeits are not doing anything original or making any contribution to the development costs. Arduino is an open-source platform, and they encourage experimentation and extension to the point that they provide the board design files so that you can modify them and manufacture your own if you wish. They like genuinely original variations on their designs. What they are less keen about is the counterfeits that are cheaply manufactured copies of the Arduino designs; these are often marked deceptively about the country of manufacture.

Official boards and the quality variants are well made. Solder joints are perfect and the parts excellent quality. I’ve yet to see an official product fail unless it has been abused (which happens to the best of us).

Having worked with a few knock-offs, I can say there is a quality difference.  I’ve had a couple of unofficial Nano’s spontaneously burn out on me. The solder joints are often atrocious. I’ve had heat dissipation issues with knock-off Ethernet shields. Most of the time the boards will work fine, but the trade-off for the low price is quality and occasional failure. The general rule in life—you get what you pay for—applies.

Getting Started

Get a starter kit with an UNO and materials for basic projects.  Here are a few examples:

If you already have an Arduino board, there are some great starter project kits with multiple parts and fun starter projects:

Once you are generally familiar with the Arduino platform and comfortable building circuits for use with the boards, explore this blog and other sites for basic ideas and techniques.

Most people want to start by controlling some turnouts from a control panel. Here are a few posts that you may find helpful:

From there you can move on to signals and lighting, and perhaps even some mechanical objects. You can quickly find where I’ve written about these and other subjects by using the tag cloud you’ll find in the left column of every page.

Good luck.  I think you’ll find Arduino and related electronics technologies worthy additions to your model railroading tool chest.