How to Build an Arduino-Controlled Train

How to Build an Arduino-Controlled Train

I found a creative way to automate my morning coffee delivery using a model train and plenty of track.

Faced with challenges like navigating rooms, a staircase, and a tricky machine interface, read on to discover how I turned a basic Hornby train set into a fully fledged transportation system.

Oh and here's a video of the entire project start-to-finish.

Originally, I had built a small oval of track around my workshop to run some of my old Hornby model trains before realising this 00 gauge railway might have some potential as an actual transportation system.

 

The Plan

The kitchen coffee machine is located one floor down around 20m or 65 feet away, so to link it with my office upstairs, I’d build a scale 2km of track through four separate rooms with some sort of elevator system in the hallway to raise and lower the trains between floors.

The whole system would be autonomously controlled using Arduino microcontrollers and a finely-tuned system of sensors to make sure the trains were in the right place at the right time.

Even the coffee machine would be automatically triggered so that when trains pull up beneath it, coffee would seamlessly be made and poured without me having to do anything but wait.

 

Automation 

These trains run on 12v DC fed straight to the track and when the rails are powered, they effectively complete a circuit with the motor in the miniature locomotives meaning you can start and stop the train by simply switching on and cutting the power.

I followed this tutorial below to automate the trains using some infra-red sensors connected to an Arduino. When a train runs in front of a sensor, it kills the power and stops the train.

 

Building the office track 

The next step was to break out the power tools and build the first phase of the railway around my office.

I raised the track bed slightly to match the future train elevator in the hallway, creating a flat, smooth bridge for reliable, derailment-free coffee delivery.

I now had a test track to run some trains and see if an espresso cup could safely be delivered to the other side of the room.

We had some stability issues with the mug so we used blutack to keep it still. The train made it’s first successful delivery of some model plane paint, which would be a little less messy to clean up in the event of an accident.

 

Elevator

After experimenting with a few different ideas, I settled on a funicular railway inspired elevator that would have a movable section of track that travelled along rails attached to wooden supports.

To raise and lower this platform I’d have to build some sort of winch at the top of the stairs.

I used LEGO motors and gears to quickly build a precise, slow, and strong winch for safely raising and lowering the train. The setup had to be exact, aligning perfectly at both ends of the track and moving at the right speed.

There was a problem of how the track would get it’s power so the trains could move. As the elevator track only needed to be powered while docked, I decided to add brass contacts that would transfer current to the track as soon as the platform was in the right position.

I copied this at the bottom and everything seemed like it was starting to come together.

 

Building the downstairs section

Next, I built a suspended track bed all the way from the bottom of the stairs to the kitchen which, as woodwork isn’t my strongest skill, took another day or so to get right.

I made sure to rigorously test this section to make sure the track was smooth enough to run a fully loaded coffee train along it without derailing and plunging to its doom. Unfortunately, a few mugs were sacrificed to gain these important research findings.

 

Coding

For the top section, I’d have one Arduino controlling the track around my office and the elevator on the stairs.

For the lower section that takes the trains from the hallway to the kitchen, I’d use another Arduino to control the track and the coffee machine. This could be placed all the way over in the kitchen without the need for extra cables going all the way up the line; instead it would power the track from here.

 

Testing

This whole system should run almost any locomotive, but I’ve found these small 6 wheel tank engines to be the most reliable.

Next you put it on the track, and fire up the Arduinos.

Now the Arduino knows the train is on the way to the kitchen so the first thing it does is await its arrival at the top sensor on the lift.

When the train arrives, it stops at the sensor and the Arduino turns on the LEGO motor to lower the elevator.

Around three minutes later, the train finally arrives at the bottom where the elevator rails become live as soon as the brass contact pads connect.

This section of track is controlled by Arduino number 2 in the kitchen which is set up to simply wait for the train to arrive keeping a constant supply of power to the track.

The only sensor on this bit of track is right at the end so the locomotive can simply keep going until it hits this which should stop it in the right place and start the coffee machine with the servo.

Next the coffee is poured and after a short wait to make the machine has definitely stopped, the Arduino tells the train to start heading back up the line.

Now the train just needs to be stopped by hitting the sensor at the bottom of the lift which tells the LEGO motor to winch the elevator back up to the top.

After the train survives it’s perilous assent, the Arduino is told it has arrived by the top sensor, so it kills the LEGO motor and energises the track so the train can start on it’s final stretch of the journey around the office.

After a successful mission, the only thing left to do is enjoy my well travelled coffee and clean up the several mugs we lost along the way.

 

If you enjoyed this insight into one of many Project Air projects, please check out our articles page on the website for more!

Back to blog

Leave a comment