Universal Railway Bus Project

is a Open set of formal rules allowing to combine multiple Arduino among themselves, plug external control consoles, including wireless ones, and connectiong any railway devices

Multiwindows app Train DUO


An interesting point, there are a lot of good materials on the Internet about the design on the basis of Arduino. Much smaller programs for Android using Bluetooth for control. And, apart from simple examples, there are practically no posts how to tie together Android and Arduino.

The problem in the rules of data transmission. For this purpose, a number of good protocols have been invented, for example the FIRMATA, which is included in the Arduino IDE collection of examples. But it is for this project that it is possible to make the protocol simpler and more flexible. In railway model, there is not a lot of data flow and the speed of passing the team is also not very important. Therefore, you can use a very simpliest protocol for data exchange. This is the URB Protocol developed by me.

The basic principles remain unchanged, only a simple, understandable code, no more complex than examples Arduino IDE.

Protocol 2, as well whole URB project, does not limit you in choosing equipment. You can use any Arduino boards and combine them with each other.

Introduction to the programming of the URB project

Any sketch of the project consists of four parts. The first part describes global variables, and, if necessary, their initial values. Declares required for this unit of the library. First part also mandatory to have the function of reloading a unit on an external command. There also overriding the Arduino pins using the #define compile component. In the second part, the modes of the Arduino pins are set, the used libraries are initialized and running commands are given to the your layout devices so they took the default position. The third part is the loop, and it is in it that all commands are executed. And the last part is the functions that will be discussed in detail later.

Your layout is quite complex in terms of wakeup control. The position of turnouts and signals, the order and logic of the power switching on of the lines, the state of the global settings at the power-up of the railway layout must be uniquely determined.

In this project, this problem is resolved in two ways. The first, obvious, in the void setup () part, you describe not only the purpose of the GPIO pins, but also their meaning. Then you, in accordance with them, bring the layout elements to the initial state. The second way is to use the RESET function. This function resets each URB by a common command. Then the same thing happens as in the first method. The reset command in the Protocol has the syntax "999z", and is executed every time the application is successfully connected via Bluetooth.

You can also force the layout lines at any time by pressing the DEFAULT button in the Application. This method is also demonstrating how do interactions between an Android application and Arduino. As soon as in the third part of the sketch the reset command is recognized, control is transferred to the reset function void(* resetFunc) (void) = 0; in the first part, after which Arduino successively passes the first and second parts of the sketch, and again returns to third part – void loop().

Structure URB sketch

Protocol 2.3

The URB project uses multiple URB units (or individual Arduino boards) simultaneously, and also receives and transmits data to and from the application on an Android device. Therefore, a standard is needed for data exchange.

For Arduino there is a very large number of protocols for data exchange. For example, Firmata or NMEA 0183. But they are all complicated for this project, in addition, if you use the library implementation code of the protocol, then you can not change anything in it, and this significantly limits the ability to connect various devices on the railway layout. In railway model, there is not a lot of data flow and the speed of passing the team is also not very important. Therefore, you can use a very simple protocol. It consists of four characters, the last character – "z" indicates the end of the command. Combination of the first three letters or numbers gives a lot of variants the commands.

For example, for switching turnout A command are "ja1z" and "ja0z". In the sketches of Arduino they are parsing by such a well-known block:

// start parsing switching turnouts command                    
if (inputString.charAt(0) =='j') {
 // select Turnout A
  if (inputString.charAt(1) =='a') { 
    if (inputString.charAt(2) =='1') { 
        // Code for switch Turnout to straight position
        Bluetooth.print("a1z"); // feedback command
    if (inputString.charAt(2) =='0') { 
        // Code for switch Turnout to branch position
        Bluetooth.print("a0z"); // feedback command

So, you can change this protocol and expand it yourself. It is therefore easy to expand. So, in Protocol version 2.3, turntable control commands are added, and these commands begin with the letter "t" (turntable). I will also gradually add new commands to this Protocol by your demand.

This type of command is parsing in the Part 3 of the sketch. It's based on the example of Serial Event from Arduino IDE, but is different only by the sign of the completion of the command is the symbol "z". For testing and adjusting your layout devices you can type these commands directly into the Serial Terminal of Arduino IDE. Full description of Protocol 2.3 you will read on Orange Book. For sketches and examples, see the Railworks page.

Orange Book



Feedback – a unique opportunity this project

Feedback easy possible to extend the use sensors to control trains. And gradually will be new features to the project. URB was originally designed for this possibility and the re-equipment of the layout Arduino hardware for the use of feedback is not required. Currently, two feedback options are supported: automatic train stop before deadlock or signal and displaying in the application the real position of junctions on the layout. The first possibility is called AWS and will expand in the future – soon script commands!

The video shows the use of this function in real conditions.




Next possibility – indicate turnouts positions

The motor switching of the turnout in railway modeling gives a lot of unobvious opportunities and gives rise to several problems. For example, the position of the turnout can be uniquely associated with traffic light signals and, as a result, get a ready signal system. But at the same time, most of the modellers make bulky and complex panels, and their upgrade will very complicity. This problem is further aggravated by the fact that the position of the arrows is not always known after the layout is turned ON.

All this was taken into account in the development of this project by changing the sketches of a particular URB, you can program any logic of the behavior of the signals. Also in the application there is a button DEFAULT, which sets turnouts to the position assigned user, the same happens when the layout is initialized when the power is turned on. Since there may be more than one player in the project, this button also helps to synchronize the position of the lines during the game. Without using feedback, the DEFAULT button is very useful.

Feedback OFF

This situation changes when the feedback mode is activated. The command unit URB always notifies the application about changing the positions of junctions. Thus, even if two Android phones are used, the position of all junctions will be online displayed on each of them.

Feedback ON

To use this function in a sketch of a communication unit, commands are used to send data to Android devices: Bluetooth.print("a1z"); Serial.print("a1z"); // feedback command to my apps.

In the Reverse Loop video, you can see the change in the position of the switch indicator on application when the real turnout switches automatically.

"... this is not for me! I just want to run the train!"

Ok, you have an option: download the any first sketch from chapters describing my applications and run it.

"... but I want traffic signals and switching turnouts without this stupid programming!"

For a small DONATION, I can coding sketches for you :)

I2C Bus. COMM and Local URB units.
Communication between units

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.

I2C Bus

The URB project uses exactly this method – combining several Arduino in a network. Control signals are sent to the COMM unit, and it routes the data to other local units. Moreover, each unit is the same, their specialization is provided by individual sketches, and the Local unit provides the based logic of the behavior railways devices. Therefore, in comparison with centralized solutions, sketches are much simpler. It also provides very great flexibility for the URB project. Thus, the all layout sketches are divided into the main sketch (COMM) and local skethes.

URB connections

Ok, we can transfer data over a Serial connection. But so we can connect only two devices to each other. Routing is needed to connect multiple Arduino units. This function is provided by the I2C bus, and for its implementation, it is enough to connect units with just two wires and use the Wire library.

The I2C bus allows you to transfer data to the specific address of the URB unit, but, unlike a Serial connection, it can transfer it only as bytes. That is, it is necessary to translate alphanumeric (String variables) commands into numbers (Byte variables). I have introduced restrictions on the transmission of only one byte on the bus, that is, any digits from 0 to 255, this is more than enough. The Protocol contains recommendations for translating into byte commands.



Extended URB topology

Extended topology

In the URB project, it is possible to combine Arduino with each other using a combination of serial and I2C data transmission channels. So in my layout there are two independent I2C buses connected by a serial interface (picture 2). More information see at URB Club.

URB unit

Free URB unit

It's just the way of the world, that without hardware, any program on Smart Phones and Tablets can not influence the reality. As you have already read about my applications, you know those it use the Arduino platform to control train models and layout devices. To connect details of Arduino with each other using a Breadboard and wires it is possible, but it is very inconvenient and unreliable. And to avoid this, I came up with a URB board.

This board uses Arduino NANO. Therefore, the dimensions of URBs are only 60x70 mm, which makes it possible to hide it even inside model building. This solution provides excellent flexibility, maintainability, simple mass production and low cost. The board is designed to avoid incorrect connection of Bluetooth modules and Arduino NANO.