Lionel Train Switch Control With a Raspberry Pi

Lionel Train Switch Control With a Raspberry Pi

We wanted to control a Lionel train set with the Raspberry Pi.  As a first step, we wanted to try to control a track switch with the Raspberry Pi.  While we’re at it, let’s control the Pi over the web.  From literally anywhere in the world.  In the project below, we show you how to setup a Lionel train switch control with the Raspberry Pi, and how to control it from anywhere!

A Raspberry Pi Controlling a BIG Lionel Train Switch and Locomotive.

This tutorial will walk through how we setup the Pi, the Lionel train track switch, and the Arduberry.  In short, we used the Raspberry Pi to control an Arduino relay shield, and we used Weaved and Apache to the project from anywhere.

 

Project Components

ARduberry and the Raspberry Pi from the SideWe used the following components in our project (other than a Lionel Train Set):

Most of these products can be found in the Dexter Industries store.  Some, like the Arduino Relay Shield and Lionel Switch, can be found on Amazon.

Setting Up the Pi HardwareRaspberry Pi controlling a Lionel Train set Switch.

Setting up the parts is very straightforward.  We connected the Pi over the local home wifi network, powering it via USB with the Raspberry Pi Power Supply.  The Arduberry slides over the Raspbery Pi.  The Arduino Relay Shield fits directly into the Arduberry.

We setup the Pi on the wifi network and ran it headless (without a monitor or keyboard attached).  Once it was on the wifi network, we could login from any Mac or PC on the same network.

The Hardware setup for Controlling Lionel Train Switches with the Raspberry Pi

 

Wire up the Lionel Switch and the Arduberry

Lionel switch Wires are confusing.

Lionel trains run at about 20V AC.  That’s right, alternating current!  Make sure whatever Arduino relay shield you use can handle A/C.

Wiring up a Lionel control switch was a little confusing.  We could not find a good diagram of the switching mechanism.  The wire coming out of the switch had four conductors.  We knew the power came from the tracks themselves and after trying to pair each of the wires with each other, we found that the red and black wires were paired with each other and the green and yellow wires were paired with each other.

When  you connected one pair, and disconnected the other pair, the switch changed direction.  It would change direction back if the the other pair was closed.  For example, to get the switch to swing one way, we would connect the black and red, and disconnect the green and yellow.  To get it to swing back, we would connect the green and yellow, and disconnect the black and red.

Fortunately the Arduino shield we bought had a normally open, normally closed pair on it, so we could set the relay switch to normally open and normally closed.  Each pair from a single switch gets its own relay on the board.

Here’s the final wiring of the switch, into the Arduino relay.

Wiring the Switch to the Relay.  lionel train switch control with a raspberry pi.

Final wiring of the Lionel Switch to the Arduino Relay. Red and Yellow Go Together. Black and Red go together.

 

So, taking a step back, our system controls and communicates like this:  Raspberry Pi <–> Arduberry <–> Arduino Shield <–> Lionel Switch.

lionel train switch control with a raspberry pi

How things work: Controlling the Lionel Train with a Raspberry Pi

 

 

Weaved and the Raspberry Pi are a great combination.Setup Weaved on the Raspberry Pi

For many projects, controlling the Raspberry Pi over your home network provides all the access you need.  In this project, we wanted to use Weaved so we could eventually communicate with the Raspberry Pi from outside our home network.

Getting to your Raspberry Pi from outside can be a tricky proposition.  You need a port forwarding service, and sometimes you need to have a static IP assigned by your internet provider.

No more.  Weaved does an awesome job of making sure that your Pi can be accessed from anywhere in the world.  See more about setting up weaved here.  Here’s a step-by-step tutorial on setting up Weaved on the Raspberry Pi.

 

Setup Apache Webserver on the Raspberry Pi

To be able to monitor and control with a web page, the Raspberry Pi needs to have web server software.  The end goal is to have the Raspberry Pi serving up a web page which controls the switches.  Because we want to interact with the page, we’ll also install PHP.

First run an update on the Raspberry Pi.  (Your Raspberry Pi must be connected to the internet to do these steps).

sudo apt-get update
First, we’ll setup Apache webserver.  The Raspberry Pi foundation has a great tutorial we followed for setting up the Apache server here.
Run the following on the Pi:
sudo apt-get install apache2 -y
Next, we will setup the Pi to serve up PHP.  Setup PHP by running the following command on the Pi:
sudo apt-get install php5 libapache2-mod-php5 -y
If no errors popped up during the installation, we should have a webserver running on the Pi.  Now test that all is installed correctly.  Create a new file in the directory /var/www/ with the name “test.php”
Change permissions of the file test.php:
chmod 666 test.php

And we should now have a web page when you type the IP address for the Raspberry Pi into a browser.  In our case we have Apple’s Bonjour running on all machines, so in any browser on the local network we can just type in “raspberrypi.local” and the page “test.php” shows up in our browser.

Code

There are now three sets of code to consider for this project: the Arduberry code (in Arduino), the Raspberry Pi code (in Python) and finally the web page we’ll serve up and control from (in php).  All of the code can be found in our Arduberry repository on Github.  

To copy the code to your own Raspberry Pi, just type:

sudo git clone https://github.com/DexterInd/Arduberry/

First, let’s go over the Arduberry Code.  

The Arduberry is going to run in a loop, listening for commands from the Raspberry Pi.  The commands come over the serial line and turn the switch on and off.

In the video, we have an early version of the Arduberry code switching the tracks back and forth on a timer, rather than a serial command.

Next, let’s go over the Python code.

We’ve written in comments to explain what we’re doing.  Basically we’re running this code at startup, and then we’re going to constantly check the value of “test.txt” and see if it changed.  If it changed, we tell the Arduberry to flip the switch.  If it’s the same, we do nothing.

Finally, let’s look at the php website control code.

We have three pages that the browser will bounce back and forth on.  All three files will sit in the /var/www directory of your Raspberry Pi.  The primary purpose of these files is to rewrite “test.txt” with a 1 or 0.  Remember from above that the python code is reading this file and looking for a 1 or 0.

 

 

That’s pretty much it!  When installing the php code, you may need to run “chmod” on each file to open up the permissions.

Run it!

Here are a few videos of the project in action.  We threw in a great model railroad derailment as well!

 

Further Work

This is really just the start of this project.  If there’s enough interest, we plan to keep going and extending it.  Further work to be done includes:

  • Control the Lionel train engine.  In theory, we should be able to control the speed of the train with pulse-width-modulation (PWM).  If we use solid state relays, we could control the speed and direction of the train, not just the tracks.
  • More efficient way to wire up the switch.  If we found a better electrical diagram of the switch, we could reduce the number of relays needed to control a single switch.  Right now we have to use two relays.  If we could use just one relay per track switch, we could increase the number of relays controlled by 100%!
  • Safety.  You can see in the videos that we caused a derailment.  That was our human error!  Why not put a light sensor in there that wouldn’t allow the switch to switch while a train is present and blocking a light sensor?  You could easily hook this up with the Arduberry.
  • Expand!  You can see there’s a lot of space there, many more switches to be added, more trains to control, and more complexity to be added.