JMRI® connects to...
Arduino Use with JMRI
Supported Hardware
Devices, command stations, networks, and protocols:
Applications
By the community of JMRI.org:
Tools
JMRI tools for working with your layout:
Layout Automation
Use JMRI to automate parts of your layout and operations:

JMRI Help:

Contents Index
Glossary FAQ

Donate to JMRI.org

Hardware Support: Using Arduinos with JMRI

Benson Valley Railway

Picture Credit: http://www.motorhomesites.org.uk [ archived ]

Arduino micro controllers are special purpose computers that, with appropriate sensors and other electronics, can be used to emulate DCC command stations and decoders, control turnout machines, light buildings and scenery, and animate just about anything on your layout -- all under control of JMRI.

Introduction to Using Arduinos with JMRI

Using arduinos on your layout is no harder than you want it to be. You can get a block occupancy system up and operating and communicating with your sensors and panels in JMRI in just a couple of hours. You can also design a custom layout animation that is triggered by Logix or a Jython script and take days to work out all the kinks. You can even use an arduino as your DCC command station with DCC-EX EX-CommandStation. It's up to you.

Software to develop code on your arduino is free online at arduino.cc.

There are many ways that arduinos can be used on your railroad with JMRI:

JMRI provides support to use an arduino as a DCC command station ( DCC++) or as a DCC decoder (by setting up a decoder definition file. You can also use arduinos to control one or more auxiliary devices on your railroad by connecting the arduino to your computer running JMRI either through the tracks (in which case it will receive commands as any other decoder) or through an auxiliary network such as C/MRI or Loconet in which case they will communicate with JMRI by following the rules of that protocol. Sections below will help you get started.

Additional Reference Material

You can find information about typical projects in a variety of places on the web. Some to look at include:

New blogs and videos are being created almost daily describing additional uses for arduinos on the railroad. With the steady introduction of new sensors and controllers to connect to the pins on an arduino, there is hardly an electronically controllable item that cannot be controlled via JMRI.

[Go to top of page]

Connecting to JMRI

Arduinos communicate with your Windows, Mac, or Linux computer in a variety of ways depending on the function they are performing:

The following sections provide hints for using some of the different communication approaches.

Emulating a CMRI node

One of the simplest ways to have your arduino communicate with JMRI is to install software that allows it to emulate a CMRI node. [See the CMRI help page for information about using CMRI nodes with JMRI.] On the arduino, install one of the arduino C/MRI libraries and include it in your sketch (for example, the library by Michael Adams available on Github ( see also Stephen Brackstone's website [ archived ] for an example of its use). See also below for a set of steps to get up and operating with an arduino as CMRI node with JMRI.

Some notes of interest when programming your arduino as a CMRI node:

See also the section below for a Step-by-Step example of creating an arduino-based CMRI Node.

[Go to top of page]

Serial communication

Arduinos can be directly connected to a USB port and use serial communications via a Jython script executed in JMRI. An example is included in the JMRI Jython directory. Geoff Bunza describes a serial communication script for collecting sensor data from an arduino. It is not necessary to configure a JMRI system connection to use direct serial communications.

[Go to top of page]

Using multiple arduinos via RS-485

RS485 Daisy Chain Although JMRI permits more than one CMRI connection to be configured for a layout, it is often convenient to connect multiple arduinos on a single CMRInet connection. This eliminates the need to have individual USB cables going from each arduino to separate USB ports on your computer. Instead, multiple arduinos can be connected via an RS-485 adapter to an RS-485 two wire cable in a "daisy chain" (each arduino connected to the next), ending in an RS-485-to-USB adapter to connect to the JMRI computer as shown in this diagram:

Connect each arduino to an RS-485 communication board via the serial pins on your arduino. There are multiple sources for the RS485 communication board that range in price from a few dollars to upwards of $30. You will also need a single USB-RS485 converter, also available from multiple sources.

See Stephen Brackstone again [ archived ] for a good description of how to do this setup.

Then include a RS-485 library in your arduino sketch (for example, this one by Michael Adams). Make sure each arduino is assigned a different CMRI node number. You do not need any additional software on your computer. Simply tell JMRI what USB port the converter is connected to and all arduinos on the RS485 network will be seen by JMRI.

[Go to top of page]

Using JMRI Features and Tools with Arduinos

Arduinos can be connected to almost any electronic component used on a model railroad, including turnout motors, turnout relays, led lighting, and a wide variety of sensors. You can, for example, define a sensor within JMRI that is identified with a CMRI input bit and a turnout identified with a CMRI output bit, both of which are processed by an arduino for controlling a turnout motor. Or, you can identify those sensors and turnouts as "internal" (JMRI objects not associated with hardware on your layout) and use a script to process the data flow back and forth to the arduino.

Here are some examples with links to explanatory articles and/or arduino code that you can learn from:

The possibilities are nearly endless.

[Go to top of page]

Step-by-Step Example Using Arduino as a CMRI Node

This example will walk through main steps of using an arduino to monitor a block occupancy sensor and and JMRI using that sensor to change the color of the track of the occupied section on a layout panel. This is not meant to be a tutorial on either arduinos or JMRI, but rather to provide a guide for someone generally familiar with both but just trying to use them together. Much more can be found by checking some of the references listed in the Introduction above.

[Go to top of page]

CBUS® is a registered trade mark of Dr Michael Bolton