Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Model Railway Automation using Raspberry Pi and Arduino

This is part of my projects on building an indoor (OO Scale) and outdoor (G-Scale) model railway.

This will look at the technology behind model railway computer control and automation and some of the tools that can be used. This is primarily looking at using a Raspberry Pi or Arduino, controlling an analog train using sensors to detect the position of the train.

Outdoor G-Scale Model Railway

This video introduces my G-Scale outdoor model railway. It's work in progress, but it is now fully working with some automation as well as a web based IoT train control option. Using the web based version I can control the trains using my mobile phone or laptop.

Please Subscribe to my channel if you find these videos useful.

Model Railway Automation - part 1 - Power Supplies

In this first part to the video I cover power supplies and the need to convert form the AC (Alternating Current) power supply in many model railway transformers to DC (Direct Current) needed to control the DC motors.

Converting from AC to DC

The image below shows a plug-in transformer and a model train controller. These are typical for analog model railways (in my experience with both OO / HO scale and Garden Scale outdoor railways), in that the plug-in transformer outputs an AC voltage. This is different to most other plug-in power supplies which convert the voltage to DC within the power supply. For model railways the controller is responsible for converting the AC to DC which is required to control the DC motors inside the model train.

Hornby OO model train transformer and controller

The AC waveform changes polarity from positive to negative. This is shown in the top of the diagram below. This first needs to be converted to a DC voltage using a bridge rectifier which gives the second waveform. It can then be smoothed using a capacitor to create the third rectifier. If this is being used to power a microprocessor then it may need to go through an additional voltage regulator to ensure a consistant voltage.

Waveforms of an AC sine wave, DC after rectification and DC voltage after capacitor smoothing

A bridge rectifier is an arrangement of 4 diodes (eg. 1N4001) configured so that the output is always in the same direction giving a DC voltage.

Bridge rectifier made using 1N4001 diodes

Rather than constructing this from four diodes it is usually easier to buy a bridge rectifier integrated into a single component. The SKB2/02 or KBP series bridge rectifier is shown in the video, although there are other bridge rectifiers depending upon the amount of current required.

Model Railway Automation - part 2 - Changing the direction of the voltage and train using an H-Bridge

Please Subscribe and click the bell icon to be notified of my future videos.

To change the direction of a DC motor uses in a model train then the polarity of the supply needs to be reversed. For automation this is normally achieved using a H-Bridge which can swap the polarity to the track / motor without needing a negative power supply. The H-Bridge works by having 4 internal switches (usually MOSFets within an integrated circuit). When the switches are all off then there is no power to the track. When diagnally opposite switches are turned on then the polarity is in one direction, when the other pair of diagnally opposite switches are turned on then the polarity is reversed. This is best illustrated through the following diagrams.

In this diagram the switches are all off so there is no power to the motor.

H-bridge integrated circuit controller for a model railway / robotics controlled by Raspberry Pi / Arduino - Motor in off position

In this diagram one pair of diagnally opposite switches are on so there is power to the motor in one direction.

H-bridge integrated circuit controller for a model railway / robotics controlled by Raspberry Pi / Arduino - Motor in forward direction

In this diagram the opposite pair of diagnally opposite switches are on providing power in the opposite direction.

H-bridge integrated circuit controller for a model railway / robotics controlled by Raspberry Pi / Arduino - Motor in reverse direction

It is important that the switches that are in-line with each other (on the left or right) are never turned on together as that would create a short circuit. When a H-Bridge integrated circuit is used then this protection is included within the package.

For model railways then my preferred H-Bridge is the L298N motor controller IC. This is available as an inexpensive motor controller board which is easy to connect to the Raspberry Pi or Arduino. An example board is shown in the photo below.

L298N H-Bridge Motor Controller Board for DC motors (model railway or robotics) controlled by Raspberry Pi or Arduino

Base board for motor controller board

To make it easier to connect the Raspberry Pi to the motor controller I have created a board to mount them together. I designed the board in FreeCAD and printed on my 3D printer.

Raspberry Pi with L298N motor controller board

The board has mounting holes one one side for a Raspberry Pi, with our without a pimoroni Pibow case (the photo above shows with a PiBow coupe). The other space has mounting holes for 2 L298N h-bridge motor controller boards which can allow up to 4 motors to be controlled. Alternatively as shown, it can have one motor controller board and one mini-breadboard. The connections needed are one pair of GPIO ports per motor (one for forward and one for reverse) and a ground connection. The motor controller can be powered from an external supply (eg. 12V DC supply) in which case the power jumper can be left in place and it will take power for the board from that. If not then that can be removed and a 5V power connection be taken from the Raspberry Pi.

FreeCAD CAD image of motor controller base board for Raspberry Pi with L298N

You may need to widen the holes using a drill depending upon the size of your mounting screws. A 3.2mm drill is a good size if using M3 screws.

Model Railway Automation - part 3 - Speed control using Pulse Width Modulation (PWM)

Pulse width modulation is a technique to create a variable voltage using a digital output. It is often used on both the Arduino and Raspberry Pi to provide an equivelant analog voltage output.

The waveform below shows how different width pulses can be used to create a varing effective voltage when used to switch as 12V power supply to control a model train.

Waveform showing PWM output compared with switched 12V analog power supply for model railway train control

The source code to use PWM on the Raspberry Pi (using Python GPIO zero) and on the Arduino (using in-built pwm pins) is included below.

Model Railway Automation - part 4 - Train Detection with a Reed Switch

This episode looks at using reed swith and magnet to automate the model train so that it automatically stops at the station. The reed switch is a switch which is activated when a suitable magnet comes into range. Placing the reed switch in between the track and a magnet on the locomotive means that the Raspberry Pi can detect the train approaching the station and slow it down and stop it at the correct position.

Choosing a Reed Switch

There are different types of reed switches and magnets suitable for different situations. The following guide is a quick guide to some different types of reed switches and magnets.

Here is a list of the reed switches and magnets that I used for the video. These are all from CPC Farnell which is the UK retail branch of Farnell / Element 14. I don't get any monetization from these, but they are the one supplier that I used that has a good selection of different types that I could buy and try. I've also included a cheaper version of the mountable reed switch which is much cheaper than the 240V version and makes that an even better option for model railway control.

Model Railway Automation - part 5 - IoT model railway control with Raspberry Pi and web browser

The final episode in this series covers using a Raspberry Pi and IoT to control the train using a web browser. This allows the train speed to be controlled using a mobile phone and a web browser. The program uses Python Bottle which acts as a web server.

The wiring for the Raspberry Pi to the L289N motor controller is shown below. This shows motors to represent the trains, which are actually wired through connections to the track for the model railway.

Wiring diagram for Raspberry Pi and L289N motor controller for model railway automation

The software for the model railway controls has now moved to GitHub. See the following link, which includes details of how to install the code.

The interface is under development. It is designed for a mobile phone in portrait mode with the ability to control two trains.

Please Subscribe and click the bell icon to be notified of my future videos.

Related projects

Also see:

Future projects

For the latest updates please:
Subscribe to the PenguinTutor YouTube Channel
and
Follow @penguintutor on Twitter

Previous Garden Model Railway
Garden Model Railway
Next Points controller
Points controller