Railway sensors

The URB project manages the trains in DC mode. And this means that you can apply ANY sensor to Arduino, including such an exotic as a pressure sensor. However, most of the sensors are large enough, for example, an ultrasonic sensor is very difficult to incorporate into the layout imperceptibly. Also, there are some difficulties with the probability of triggering sensors based on reflection of visible and infrared spectra. In any case, you can experiment and get your own successful designs.

I mainly use Hall sensors, they provide guaranteed triggering and are very easy to install and configure. Also, I apply the infrared sensor together with the IR-LED in one housing. However, when using IR sensors, there are several problems: different pickup distance due to the color of the wagons (the more lighter the color, the longer distance of triggered) and a false signal of triggered at change of moving cars of a train in a zone of action of the gauge (gap zone triggered). It is also difficult to avoid the sensor's response to multiple reflections of infrared rays from the details of the layout and train and the influence of sunlight.

But in a situation where you need to get a constant signal while passing the entire train through the sensor zone, the infrared sensor is preferable, only you need to modify its design.

Hall Sensor

To automate the movement of trains you need sensors. I used Hall sensors as train detector triggers on the line. In comparison with other sensors, it has the advantage of a trigger "dot". The second element – the magnet –is attached to any metal part underside of a car or a locomotive (for example, to the fastening screw, etc.).

I recommend small cylinder neodymium rare earth magnets. By changing their number, you can adjust the distance between the magnet on the car and the sensor on the rails. I have reliable operation at distances of 1-5 mm. This allows you to correctly and accurately set the pickup location relative to the train and relative to the location on the layout, as well as easily adjust it by moving the magnets. You can place a magnet on any car or locomotive, at the beginning, middle or end of the train, thus adjusting the stopping place to within a centimeter. Hall sensors are small, if they are neatly put between the rails, then they are like real AWS inductor. To Arduino NANO it is possible to connect up to 18 Hall sensors. But this is a standalone solution, you can not transfer data to other layout's devices. URB solves this problem elegantly. You can connect the sensors to any URB and then transmit their signals to the communication station via the I2C bus.


In my opinion, it is wise to combine both types of sensors depending on the purpose. For example, IR sensors are more convenient for a railway crossing barriers, and Hall sensors for stopping at a dead end or at a station with automatic control (AWS and script commands of the URB project).

And one last point. For Arduino, the logical states of operation of any sensor do not matter, you can always invert them in the sketch code. For example, if you made my design with an IR sensor, you will notice that the sensor is triggered when there is no train (the logical state is HIGH), and when the train passes, the state of the sensor is off (LOW). A simple if (digitalRead(SENSOR)==LOW) {...} code will change the signal from the sensor to the opposite.


The IR-module

On this module I separated the infrared sensor emitter and receiver. These details deployed relative to the rails by 45 degrees and are located at a certain height above the rails. Also on the receiver is a black slit mask that removes stray radiation.

The height of the sensor and the IR-LED above the rails for different scales is selected experimentally based on the minimum horizontal continuous line of wagons in your collection.

This design allows you to get a guaranteed signal of the presence of a train at the sensor zone. The system works correctly at any speed of the train, any the number of cars or types of cars and any length of the train.


Hall Magnetic Sensor Module for Arduino

Servo Point Motor

By this mechanism, I am proud as designer! The task was to make such a reliable turnouts switch-mechanism, which will simply and quickly manufactured, while ensuring accuracy of moving the rod to a tenth of a millimeter.

Given the large error in the position of the lever of the cheap servo when stopping after rotation, and the need of convenient adjustment without tools at the installation site, the task seemed to me unsolvable. However, I came up with, and even much more. Shoulders slingshot, as well the mechanism, made of thin galvanized sheet metal, this material is available almost everywhere. So, by bending them with your finger you can adjust the stroke of the rod and, accordingly, the accuracy of the switch position. In addition, these shoulders act as a damper, parrying the errors of the mechanical transmission and securely fix the switch mechanism in the extreme positions. But that is not all. If the servo arm is stop to a position of 90 degrees, the mechanism is unlocked and you can switch the turnout manually. Sketches and examples in this project are focused specifically on this mechanism.

Placing point-motor


Unlike other known servo point designs, this design does not require space under the layout. That is, you can place such a mechanism on a table or floor next to the rails.Or, as in the classic Tortoise switch machine, hide the mechanism under the layout.

In the video "Railway modelling for teens", I used a modified version of my mechanism with the addition of a Hall sensor. In this modification, the point motor generates a turnout position signal, as when using limit switches. Only it is more reliable, since it does not have mechanical contacts.

Modified point-motor





The URB project allows you to monitoring and independently create any types of signals and algorithms for their operation. You can make the blinking light signaling for railway crossings and big semaphore traffic complexes ramified control systems trains. Even in comparison with a very well-designed proprietary signaling Z21 system – the URB project has the advantage of being open. Again, this is a very exciting process for you, you can independently invent and implement exactly what you need, and not to collect from the finished firm "cubes" approximately working solution. Even if you are projecting a MAGLEV trains layout, the URB project will allow you to create a futuristic signaling system.


I tried to make the most informative video about the production of signals for my layout. But you can use ready-made signals, and they most often use a three- or more-wire connection system. In this case, simply use the GND contact on the URB unit, you do not need to make any changes to the sketches. Also, you may need to change the resistance in the finished signals to a lower nominal, usually they are rated at 12V.


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 turnouts and the ease of programming Arduino all it is now easiest. Without any computers! Just turn layout power on and everything works!

For a greater reality of the brightness of the signals, I recommend individually to pick up the resistor – brightness and spectrum of LEDs of different manufacturers is very different. If you need to apply a LEDs array control system on one channel, you can overcome by the current limitations using the powerful PD outputs (see URB Documentation). Also very convenient the solution to use for connecting many signals via several very cheap boards with chip PCF8574.

When developing my signals, I took into account first of all the maximum manufacturability and the cost of their manufacture. This design assumes a mass production of signals by the modeler. In addition, the signals are very strong and maintainable.

URB signal system

Level crossing

This really working design was developed a two year ago, and reliably works to this day. The actuators of the barriers is independent, since each barrier is driven by a separate servo. Since barriers can be moved asynchronously and at different speeds, it's more like the real world in which it happens that way. Also in the module use classical flashing lights.


All crossing rail electronics made on the one autonomous URB unit with the possibility of connecting to the URB bus. The mechanism is made in such a way as to fit completely into the space under the crossing of the move and not to leave its dimensions.

To automatically lower the barriers when passing a train, the original algorithm given below and two conventional infrared sensors Arduino are used. The design of these sensors is modified and described in the Sensors chapter.


The sources of the train signal about the approach and the departure from the wide railway crossing area will be infrared sensors located at a distance from the crossing. I suggest that you will establish the distance between them experimentally.

The normal servo always rotates at its maximum speed. For different models of servo drives speed is different, there are fast and slow servos, but the speed of rotation is always constant. That is, we can not make the rotation faster than the installed by the servo manufacturer with the help of electronic control (though we can increase the speed mechanically using the reduction gear). But we can lower this speed by rotating the servo shaft to a small angle with a delay, and repeat this process again and again until the servo turns to the desired position.

For this I propose to use the alternate library for Arduino VarSpeedServo, very similar in function to the standard Servo library. You will read instruction how install the third-party library in the Arduino IDE environment on the author's page. As a result, you get another setting for controlling the rotation of the servo. Changing the command myservo.write (angle, speed, true); the second parameter you can set the speed from very slow: 10-50, to maximum: 255.

Just in case, I will explain, under all the processes when using Arduino it is easier to manage with the help of variables. And servo control is no exception. In the sketch for the motor-point for the junction, it is more convenient to specify directly the angle of rotation of the servo, but in other cases it is better to use the type variables INT (integer). Variables can be calculated mathematically when the program is running. Among other things, this allows create realistic behavior of moving objects on the layout, for example, when the position of the descending barrier is closer to stopping at the lowest point, it can be slowed down or a small rebound can be realized when the barrier reaches the extreme positions.

Algorithm and sketch of railway crossing

I have already described the use of Arduino sensors for use in railway electronics and problems with their interaction with trains. To overcome these problems, several methods should be used at once: physicals and algorithmics.

Here I use infrared sensors my module with a separated emitter and receiver deployed relative to the rails by 45 degrees and spaced apart in height. Thus, I remove problems with different reflective ability of painting cars and false sensor triggering when changing moving cars of a train in the sensor operating zone. That is, there is an inverse algorithm, unlike the usual one – while the train does not exist we have a sensor active, when the train crosses the sensor, and until it completely leaves its area of operation, the sensor is blocked. Therefore, the setting of the variable resistor on IR sensor housing should be set to the minimum position for its activation.

It is obvious that the minimum Trigger distance from the sensor zone before crossing point should be such that when the train approaches the maximum speed the barrier had time to descend. This distance should be established experimentally. But Arduino gives us a lot of extra features, for example, after triggering the sensor, we can turn on the blinking alarm light at crossing , and with a certain delay after that we lower the barrier, in this case the length of the Trigger distance needs to be increased.

The original algorithm for automate moving barriers uses two variables for each sensor, one of which acts as a latch. As a result, the time independence is ensured and the code becomes very simple.

if (latch_s1 && latch_s2 && !trigger_s1 && !trigger_s2) {
latch_s1 = false;
latch_s2 = false;
The algorithm correctly reacts to the stop of the train in the sensor area and even the train maneuvers at the rail crossing.

URB signal system

It's design based of the simplest Auhagen 41582 Level crossing kit. Since there are many similar sets, you may use any. And of course, this module work both in automatic mode and in manual controlled mode.


The mechanism is made in such a way as to fit completely into the space under the crossing of the move and not to leave its dimensions. This mechanism converts the translational motion of the thrust from the servo to the rotation of the barrier and at the same time has a damper. The horizontal angle of the thrust vector can be set by you in a wide range.

This level crossing module has advanced settings: you can raise barriers synchronously, or with time-sharing – for example the barrier starts to go down (or go up) on one side of the crossing, and on the other side the same thing happens, but with a half-second delay. Also you independently regulate the speed of lifting or lowering barriers on both sides of the crossing. All light effects are regulated – the frequency of flashing of the signals, the sequence and delay of the light signaling. You can for even joke to raise one barrier and lower the other.

Blinking signals are collected on red LEDs of a two-cylindrical shape. Anodes and cathodes of these LEDs are cross-connected. As a result, with the opposite change in the state of the two outputs of Arduino from high to low levels, only one of the two LEDs will light.

Crossing plan Crossing Sketch


To configure the module, I made a separate sketch. By sending protocol commands 2 through the computer's serial terminal, you can adjust the angles of the barriers, their behavior and speed.

Adjustments Crossing Sketch

Layout Lighting


Lighting adds a realism to the model railway layout. But simple light bulbs are boring, it is better if every window of all houses will individually turn on the light. Even better, if there are fade-in effects, or flashing, like a broken fluorescent tube. Also, street lights should use dimming and station buildings should also be included in lights groups. This means we need a lot of control channels and it's expensive and complica-a-a-a-ted.

And here not! With the URB it's easy, and even more so, you can add a random algorithm for lighting windows, you can also still put an ambient light sensor and your railway world will react to begin the night. You can control this and the real buttons on the control of the layout, and from the phone using my application. And you will periodically change lighting rules by simply uploading a new sketch! My layout on assembled on a shelf, and there are 48 light channels, and this is far no limit.

Connecting LED Lighting

Each URB has 6 skrew outputs D2-D7 with a current of up to 500 mA (7 if you do not use servos). Outputs D3, D5 and D6 in the middle can be operated in PWM mode and you will use them for dimming. All pins have a voltage of 5 volts, so you can easily calculate the maximum number of LEDs possible to connect to one pin. For example, if you use cylindrically LED of white light, then its current with a serial current-limiting resistor of 150 Ohm is about 25 mA, then their total numbers will be 20 per channel. Numbers more powerful SMD LEDs, like 3528, assembling will be less per channel. Pay attention to the non-standard connection scheme with a common plus. Just in case instead of LEDs it is possible to include usual bulb lamps, only at them very much big a current.

ULN2003 DIP-16 is a cheap and widely available chip. In my experience, part of the copies of this chip (depending on the manufacturer) starts to get very hot at currents of more than 350 mA per channel. But even if it broke, you can easily replace it.

Starting with the version of URB 2.6 it is possible to connect a 12 V load. You can connect 12V LED-tapes direct to the pins.

Local URB control and effects

It is not difficult to control such a number of channels if you adhere to the main rule: all LEDs from a particular building (for example, a house) should be connected to the nearest URB. Do not use long wires! Everything will work, but you will get confused when you start to write sketches. If you have a multi-window structure, or a houses group, use PCF8574. Thanks to this rules, most of the management of the lighting channels is performed by a local URB. Only the general command comes from the communication station.

After such a long introduction, I'll explain why there are so many channels. In the real world, people turn the light on and off randomly. The simplest way to simulate this is to introduce delays, for example at station: first lights are switched on in the cafeteria and the cashier's area, after the light is on the entire first floor, then on the second and finally the platform lighting turns on.

You can see how it works on the general video of the project. The light on the platform can also flash several times, and only after then constantly glow. That is, you can do it programmatically yourself, and then how convenient it is for you to change the behavior of the layout. And of course, you can apply fade-in fade-out effects, it is especially suitable for street lights. I installed on my layout a separate URB with a photodetector, and now when the room gets dark, if the power of the layout is turned on, he turns on the evening lights. The Arduino platform also allows you to use the function random() for this.

Blinking and Fading effects

I've been looking for ready-made sketches on the Internet for a long time and as a result I was forced to reinvent the wheel. In most sketches implementing the illumination effects there is a problem – they use, often implicitly at the library level, the DELAY command. This results in data loss on I2C bus and other synchronization difficulties when executing the Arduino code. In addition, the realistic fade-in effect in my opinion can be realized only by applying trigonometric mathematical formulas. Therefore I will give here two variants of effects. The first is very good for mutually flickering lights on the railway crossing and the realization of several flashes for the effect of incorporating fluorescent lamps. The second for the ignition of street lights and the like.

The sketch is given for the test platform published below. You can control and adjustment the effects directly from the serial terminal Arduino IDE. Then the code for this sketch can be directly transferred to the local URB on layout. I remind you that on URB 2 FINAL there are only five high-current outputs with PWM control, on which such effects operate: PD3, PD5, PD6, PD9 and PD10.

Lighting test stuff


Blink and Fade Effects


By sending a command "la1z/la0z" Protocol command through the Serial Terminal on computer, a flashing mode is activated. The commands "lb1z/lb0z" start fading effects. Also, you can use wireless control using the Arduino Train applications sending the same commands to the URB unit.

Unexpected using of 12V LED stripes

Devices under developing

Friends! I started this campaign to bring back the fun and simplicity of railroad modeling. I'm doing this project alone and on this site only my original content is posted. For the content of the site and purchase of details for testing new ideas, I need funds. Or comrades-in-arms!
Please support my project informationally – share them with your friends.
Subscribe to my YouTube channel

Or you can make a donation here.

Now I am developing a wireless URB unit. Also, Protocol 2.3 supports turntable management, and with your help I can make video about it with a working prototype. And accordingly, all the instructions and the code for the sketches for you. An even more interesting ideas for you is presented in the next chapter and in the URB Club section.

I really appreciate your help in promoting and financially supporting the project, please help me implement and test new opportunities for controlling your railway.