Multiwindows app Train DUO

Arduino Train DUO

Multi-window app with advanced settings and features. The foremost program from collection of Arduino Train. Implements all features of the URB project.

You can use several Android devices for manage your trains and layout. Appeal for few control modes, manage 15 turnout switches and 12 light channels and moving gismos.

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. The lines of the 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 the Junior Lite page.

Bonus — everyone who bought the DUO application gets free access to the URB club for one week.

Free access  Much more than the Android App


  • Smooth thrust (18 speeds) and adjustment of the throttle your locos
  • Version 2.0 full supports Protocol 2.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
  • Save settings


Participate Design for Families

This App is participate in the Designed for Families program on Google Play. You can share this app from Google Play with up to 5 family members using Google Play Family Library.


Fits to your scale

The URB project has many unique and very convenient features, one of them is the independent adjustment of the traction characteristics of your locomotives.

The DC motors of the majority of locomotives, including those with DCC decoders, are controlled by the PWM method. In short, unlike a regulated transformer that changes its output voltage, in the PWM method the voltage always corresponds to the input voltage, but the number of pulses and their duration per unit of time varies. This method allows to reduce the size of the electronic DC motor controller to a small chip, and such a chip is installed, including in the DCC decoder. For the world of Arduino this method of management is native, you do not need to study the principles and subtleties of PWM work. There is a simple code that implements this method in one line: analogWrite(pin, value).

Thrust loco diagramm

In the headings of a sketch I entered an array named speedArray#. The numbers listed in it correspond to the output level of PWM. By changing these numbers you can change the characteristics of the locomotive, making it, for example, very quick, or on the contrary to achieve a smooth acceleration or braking at low speeds. By default, it is configured almost linearly: byte speedArrayA [] = {40, 50, 60, 70, 80, 90, 100, 110, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};

This experiment gives you the opportunity to adjust the control system to suit your scale and the characteristics of your locomotives. The order of the experiment is very simple: first, increasing the lower limit, get a starting to your locomotive from the spot, and then distribute the remaining range to get the dynamics you want. But this is not all the possibilities, since we can perform any mathematical and logical operations on the array, it is not difficult to programmatically introduce inertial motion and many other variants!

Compatible apps:

  Arduino Train Junior Lite
* only 9 speeds

  Arduino Train Junior PRO
* only 9 speeds

  Arduino Train DUO

Library snippets

Writing a new code for each project is rather boring, therefore in the project you have ready-made code elements are used – snippets and functions. Moreover in Arduino IDE, a sketch consisting of several files is supported. When compiled, they are combined into a common firmware to uploadind to Arduino. In this example, all the files are easier to unite into one sketch, which I did. The following example is quite large, so I'll use this feature of Arduino IDE in it.

Adjustment thrust sketch
Electrical circuit
Circuit for Arduino NANO

This scheme is almost the same as the example for Junior PRO. But in this experiment, the power to the Arduino is fed directly from the computer via the USB cable. That is, you can change the digits of the array, then load the sketch and immediately test the changes in the dynamics of your locomotive from the DUO application, without making any re-switching with wires and cables. And repeat it again and again until the result of locomotive move dynamics suits you.

After that, you can replug USB to power charger and return to dual control of your railway from Arduino Train DUO.

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 two 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 Communication station. For data transfer, I use the libraries Software Serial and Wire on the Communication station and one the library Wire in the local URB.

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.
2. Replace the servo library with an alternative one, namely with ServoTimer2. Instructions and description of this library can be read here. This and the following examples use this library for the COMM URB.

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.

This example proves the universality of URB units. In this scheme, it is enough to change the sketches in units to opposite ones and rearrange the bluetooth module to other unit. As a result, without any changes in the wiring and connections, the whole system will work correctly.

Compatible apps:

  Arduino Train Junior Lite

  Arduino Train Junior PRO

  Arduino Train DUO

Symmetrical sketch URB (ZIP archive)


Servo HG-90

Servo – 4 pieces

Arduino relay module

URB unit – 2 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 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.

Also, in this example, you can verify the universality of Arduino pins – I will connect the dual relays to different pins of the URB units, and the sketches code will remain unchanged.

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). Similarly, you may semaphore signals are programmed.

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

Isolated lines sketch URB (ZIP archive)


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 the 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

In this example, the railroad yard is controlled by a URB to which two servos, a dual relay and two signals are connected. So, you need one URB unit.

In the sketch code, you can see how the feedback works to indicate the position of turnouts (this option will only work in app Train DUO with setting Feedback ON).

I use two-wire connection of signals. If you have a three-wire signal with a ground contact, then the sketch does not need any changes, just connect the GND contact to the GND of the URB.

Signal System sketch (ZIP archive)



The site will gradually appear new examples adapted to your requests. If you have a desire to see your track plan on the site, then send me message.

Any sketches or circuits presented on this site is not a dogma. I am sure that your sketches will be of interest to other project participants. Please share your successes with other modellers. I remind the owners Arduino Train DUO that you have free entry to the Club within 15 days after purchase.

Project documentation is available to advanced project participants in the URB Club.

Free URB 2.7 unit

URB unit

As you see, that asembling of the circuit by connection wires over the breadboard is a very inconvenient task. As the circuit becomes more complicated and the components added, problems are added with the correctness and reliability of connecting the wires and blocks. Therefore, I suggest you make a free URB unit that solves not only these problems but also adds new tremendous functions. You can collect a similar scheme on the breadboard, but URB is much more convenient!