Multiwindows app Train DUO

Arduino Train DUO 5

App with advanced settings and features. The foremost program from collection of Arduino Train. 24 speed control. Implements ALL FEATURES of the URB project.

You can use several Android devices for manage your trains and layout. Appeal for few inertial control modes, manage 16 turnouts, 3-way turnout, double-slip switch and 12 channels for lighting and mechanisms. Native support sensors.

Modellers like a complex systems. If you look at any of your layouts or rail lines, then it's always a lot of ways and swithes. But when the modeler sees Arduino sketch of 400 lines for his layout, then this amount of code scares him. My American friend Dan, was so scared saw the code of Arduino that he abandoned railway modelism and began to manufacture toy furniture:)

In fact, the amount of code Arduino directly corresponds to the complexity of your track-plan and layout. But the good news is that what the more URB units you use in your layout, the simpler and shorter the code of sketches. If you want to immediately try to apply the URB project to your complex layout, then you have to read many materials from URB Club section. But, in my experience and Dan's experience, it's better to do it gradually.

I've been thinking for a whole year how to simplify your entry-point into this system, and do not turn the project into a rigid design in which you can not do anything yourself. And I managed to make the code modular. Simmetrical example given in this chapter are almost symmetrical. Therefore, two sketches for URB units are very easy to compare with each other, since peripheral devices are connected to units in the same way. This is an easy entry-point for understanding the project, but if you find it difficult to understand it, look at Learn or Junior Lite pages.

The DUO application performs the functions of complete control of a model railway layout of any complexity and therefore, first, I will briefly introduce you to its capabilities.


  Loco cab experience


  • Classic and Inertia Drive modes
  • Smooth thrust (24 speeds) and custom adjustment of the throttle your locos or part railpaths on layout
  • Cab interior UI
  • Lite, middle, heavy and off inertia modes
  • Version 5.0 full supports Protocol 2.3.1
  • Feedback between your railway and remote control app Train DUO
  • Online indicate turnouts positions
  • Special buttons for extended automate and interlocking moving trains
  • Sensors toggle (disable/enable layout sensors)
  • Extended app settings
  • Crossing and Turntable controls
  • Automated Traffic Control (10 scripts)


Switching turnouts

App DUO UI. Switches

The horizontal scroll contains 16 switches (Switch A … Switch P) for conventional turnouts and special switches for the three-position junction and the double-slip junction. For convenience, turnouts are grouped into four, marked with color marks.

Feedback indicator
Feedback switch

The URB system has two modes of online indication of turnouts status. Both of these modes are supported by the DUO application. To activate the feedback indication on the LAYOUT MANAGER tab, there is a FEEDBACK toggle. In this case, on the main LOCO CONTROL tab, the inscription FEEDBACK ON will light up.
There tab is now also a button to reset the position of all turnouts to the default state, it button was located in previous versions of the application on the main tab.

Extended train control

Extended control Train control

The application has the word DUO in the name, which means that you can use it on two Android devices at the same time. In this way, two people can control one railway layout. Possible, and most often used, the second method is separate control of two different segments of the common railway line (for example, separate control of the external and internal circles of rails). To switch between these segments, DRIVER SELECTOR is used.

The version of Protocol 2.3 added an inertial system for driving locomotives to the URB project and 24 speed control.
This led to a change in the state of the reverse lever, now it has only two positions and, by default, the direction of movement is assigned to the forward. At the same time, the lever has support feature of changes direction when passing the reverse loop.

The inertial selector has four positions. When selector off, the main traction lever looks like a classic control. If you select different inertial modes, the traction lever is automatically set to a central position and you control trains in acceleration and braking modes (I really like to use my new mode, as in a real train). You can change these modes during train control. For fun, to braking process of the locomotive I added a braking indicator, like in the real cab.

A CUSTOM button has been transformed into a switch CROSSING, for example, now you can control the railway crossing from the main menu.

Layout manager

Extended control

In the third version of the application, the way to control layouts devices is changed. All previous functions are preserved, but the channels controls switches are moved to the horizontal scroll.

Added turntable control. The first two buttons rotate the turntable to the next position clockwise and counterclockwise. The next button rotate the turntable 180 degrees, this is done to turn the locomotive with one cab (steam locomotive). The last switch turns on or off the voltage on the rails of the entire depots yard. For detailed commands for managing the turntable, see Protocol 2.3.

The functions of AWS and the interactive reverse function working only when the SENSOR switch is on.

For examples of use of the capabilities of this section, see the pages of the RAILWORKS and URB Club.

How use Bluetooth

The reason of using Bluetooth in URB project described on the Railworks page. According to reviews of modellers using my applications, there is often a confusing situation with the connection of several Bluetooth modules. The problem is not in the connection itself, it works very reliably, but in naming matching the multitude of devices paired with the phone with Bluetooth. Therefore, initially in my applications, the MAC-address of the connected device is displayed. In addition, on start each video about my applications, it begins with a video instruction on pairing Bluetooth module HC-06.

Since the project can use several Android devices, here I post the step-by-step instructions on how to connect in this situation.

Pairing a new HC-06

Power ON HC-05

Power ON module HC-06

Search new BT devices

Search new BT devices

Search new BT devices

Select HC-06

Search new BT devices

Enter pairing code

Search new BT devices

Repeat for other HC-06

Connect HC-06 to the App

Search new BT devices

Push Bluetooth Icon

Search new BT devices

Choose HC-06 you need

Search new BT devices

Connect established

If you try to connect to the HC-06 module that is turned off or already connected to another phone, you will receive an error message. So just repeat the procedure with the another module.

Search new BT devices

Wrong selected BT device

Search new BT devices

Repeated pressing to Icon breaks connection

More convenient trick

Another way to create a clear match between several identical bluetooth modules is set the individual name of each of them.

This is a very simple procedure. You only need to load the sketch into the URB unit and come up with a unique name. After changing the module name, turn off and then turn on the URB unit again. The name of the module in the Bluetooth network will changed.

More information about HC Bluetooth modules see here.

Set unique name to HC-06

The effect of connecting a USB cable together a Serial RX TX pins

In all the previous examples, I used a software serial port that can be connected to any GPIO pins of Arduino. It's solution add the independence of the Bluetooth channel from the USB. Thus it was possible without disabling Bluetooth upload the sketch from your computer via USB to Arduino, which was very convenient for experiments with sketches reprogramming. But in this example, we need two control channels, so if you forget to disconnect the second Bluetooth module from the Arduino, then when you upload the sketch from the computer, you may will get an error or endless uploading in to Arduino.

When you upload the sketch into Arduino via USB, the data from the computer goes to the same pins RX and TX as are used by second Bluetooth module. Therefore, if you upload the sketch, unplug SECOND Bluetooth module wires RXD and TXD.

I'll tell you a little secret: Arduino UNO and Arduino NANO use the same microcontroller Atmega 328, which means that there is no difference between them, even at the output GPIO pins. That is, they are exactly the same, but Arduino NANO board is smaller size.

Problem between Serial and USB

Power supply

The electric power in URB project divided into two branches, one for moving trains, the second for electronics and devices on the layout. Thus, with short circuits on rails and other troubles with trains, the layout control will not be affected. When using Arduino NANO (UNO, MEGA) obvious choice voltage for electronics is 5 volts. L298 allows you to use for your locomotives the required supply voltage (12 or 18V), and in the sketch you can change the code PWM to adjust the characteristics of the dynamics.

In the following examples, we will create an extensive network of many URB units and a large number of railway peripherals connected to them. Because of this, the load current on 5V can become significant, so we need more powerful power sources compared with the charger for phone. I highly recommend a 5V power supply with a current of at least 2 amps.

The ideal solution can be a computer power supply, it immediately gives out voltges both 5 and 12V with protection. Also included in the set of railways are usually 9-12V power supplies. Also you can apply two standard power supplies, to 5V and 12V, by combining their negative wires together.

Power for big projects

Symmetrical track plan

Track-plan for symmetrical example

After several successful experiments railway modeler will faces the problem of lack of free I/O pins (GPIO) on Arduino. And many people try to solve this problem directly, change board UNO or NANO to bigger Arduino (DUE, Mega). It seems to me wrong, the problem still remains. The best way – to unite the microcontrollers to the network, allow scale the number of pins of the microcontroller to almost endlessly. The I2C bus is a good fit for this. It has native support with the Wire library in the Arduino, and has addressing in contrast to the serial connection solution. Convenient commutation for the I2C bus is built into the URB unit, besides this unit allows you to standardize connection methods. As a result, the electrical circuit diagram becomes unnecessary – just look at the description of the conclusions in the title of the sketch.

As you can see, I use in this experiment three URB units. Observe the following principle of placing URB units on the layout, the units should be located as close to the peripheral devices of the layout. In this case, these are servos of Point-motors. In the examples for the app Train DUO, I will use the servos to switch turnouts. You can change this code to control any type of turnouts drive.

In the header of the sketches, I set the pins for the servos and the driver-motor. Of the functions, I only need control of locomotive traction and switch junction in the sketch for the COMM unit. For data transfer, I use the libraries Software Serial and Wire on the COMM unit and one the library Wire in local URBs.

Since April, the URB Project, by default, uses the 122 Hz PWM frequency by pins D9 and D10. This means that timer 1 with divider 256 will be used. There is a problem with the standard library SERVO from Arduino IDE. If you an attempt is made to use an Servo.h and PWM on a URB unit (that is, simultaneously connect both the motor-driver and servo drives to one unit), the train control ceases to function. The Servo library blocks these PWM pins.

There are two solutions to this problem:
1. Do not connect servos and accordingly do not use the servo library on units with the motor driver connected (this example).
2. Replace the servo library with an alternative one, namely with ServoTimer2. Instructions and description of this library can be read here.

Since the feature of the project is auto-disconnection of servos, you will be interested in the code that implements this function. Also here, the reset options for the rail lines in the default position described above are applied. As you saw the code is not complicated and for local units sketches is the same.

This example proves the universality of URB units.

Compatible apps:

  Arduino Train Junior Lite

  Arduino Train Junior PRO

  Arduino Train DUO




Servo HG-90

Servo – 4 pieces

URB unit

URB unit – 3 pieces

Universal Railway Bus unit is a convenient and reliable connector, replacing the breadboard and a bunch of wires.

Adding isolated lines

Track-plan for symmetrical example

One train is good, but several trains are even better. Since my project works in the DC mode, it is enough to interrupt the rails in several places and get more interesting control modes. This simple solution, as you will see in the following examples, allows for flexibility to surpass even DCC control. But most importantly, Arduino in my project independently turns on and off lines depending on the position of junctions and the logic you set.

In there part of experiment, I use the simplest algorithm – if two turnouts (A—B or C—D) allow movement the train along a line, then the relay turns on the current to it, otherwise the line is de-energized. The code that implements this algorithm in my project is called a Logic Block.

I connected the relay in many ways. Relay one just transfers the voltage to line 1 or to line 2. In this case, always one of the lines will be active. Relay 2 allows both lines (3-4) to be de-energized.

Algorithm for symmetrical example

To compose algorithms for the layout, we use selection operators and Boolean Algebra. Before writing a sketch, it is convenient to make a table of states of junctions, lines (and, accordingly, relays). You can also add semaphore signals to this example by simply adding commands for this to local sketching.

And now, I just add the relay logic to the previous Symmetrical sketches. Done!




Insulated rail joiners

Insulated rail joiners

Rail joiners are small clips used to join two sections of track mechanically. They come in metal (nickel silver) which also connects the rails electrically, and plastic which are insulated to isolate the two sections of track electrically.

Arduino relay module

Relay module – 2 pieces

A Relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate the switch and provide electrical isolation between two circuits.

Signal System

Railway signals are rarely found on layout, and their availability is a sign of professionalism. Not this the modelers do not want to install them, but that not a trivial task – the algorithm for switching many signals needs to corrected for each railway line. So you need a large computer with software or branded digital sets from manufacturers. Or soldering their own boards on logic chips.

But with the Arduino everything becomes much better. Convenient direct connection of signals and sensors to the URB connectors provides easy installation. Together with the availability of information on the position of all junctions and the ease of programming Arduino all it is now easiest. Without any computers! Just turn layout power on and everything works!

Elementary 1 track-plan

In fact, programming automatic switching of light signals is a fun. I mean, get a peep of how this luminous color "magic" works itself after the your sketch has been filled and running. In this chapter I give a simple example, but even it shows ways to automate the movement of trains. Next, in pages RAILWORKS and URB Club examples will be even more cool!

Compatible apps:

  Arduino Train Junior Lite
* without Indicate

  Arduino Train Junior PRO
* without Indicate

  Arduino Train DUO



Youtube Episode 5 (ZIP archive)

Track plan of the winner of the 2020 competition

Contest 2020 track-plan

These are sketches for the Track plan of the winner of the competition. In this project, only the basic functions of the URB control system are implemented. Free pins of units of URB can be used to add traffic lights and other features of URB project.


Sketches (ZIP archive)



This sketches was programmed for Protocol 2.3 and URB 2 FINAL and SE.


Imre track-plan

My friend Imre from Hungary sent me his project. It turned out an excellent video about the adaptation of the URB control system for a specific railway layout.

This example consists of two parts. The first is the planning of the electrical circuit, the second is the implementation.


Imre Layout sketches (ZIP archive)



This sketches was programmed for Protocol 2.1 and URB 2.122.



URB #2

Local URB2

URB #3

Local URB3

URB #4

Local URB4


Imre Indicate Panel

The Indicate Panel of the state of railway lines is the final stage of building the layout. And there are many options for creating such structures.

It is very easy to make such a system for URB project, as it is integrated into the system. Since the project was invented for quick and effective implementation, I also came up with an interesting design for this device. The bottom line is that you just need to print it on a printer. And then connecting it to the nearest URB unit.

If you have difficulty creating your panel design, then I can design such a panel for you.

Send me message.


Sketches for Indicate Panel (ZIP archive)