Introduction: Giving Thomas the Train the Ability to Drive at Night

Picture of Giving Thomas the Train the Ability to Drive at Night

This instructable will teach you how to create a system of lights to warn waiting passengers when a train is approaching and also how to get a message to appear on a laptop when the train is at the station. A sound for when the train passes the station will be generated along with a series of flashing LED lights. All of this can be done using a Raspberry Pi.

Step 1: Supplies

Picture of Supplies

One computer

MatLab 2016 or newer

Download the Raspberry Pi package

3D Printer

3D printed Train Station to house the raspberry pi

Raspberry Pi with the following components:

2 Infrared Sensors

5 LED lights of your choice of color

11 Wires

2 Resistors

200-300 Ohm resistors

USB Chord to connect computer to the raspberry pi

Step 2: Design the Circuitry for Desired Outputs and Inputs

Picture of Design the Circuitry for Desired Outputs and Inputs

Using basic circuitry, the lights and infrared emitter and receiver should be hooked up to the Raspberry Pi. The Red LED lights are hooked up to ground and then are connected to GPIO Pins 4,5,6 and 7. The Infrared Receiver is connected to GPIO Pin 21 and the Infrared Emitter is connected to the 5V pin.

Step 3: Develop Coding to Satisfy Desirable Outcome

Picture of Develop Coding to Satisfy Desirable Outcome

The most important lines of the code are lines 12 and 16 which launch the dialogue boxes. Line 18, the if statement, is reading if the infra red sensors have an obstruction between them and if they are obstructed then that means the train is passing, the horn will sound and the lights will turn on. If the statement is false nothing will happen because the train is not approaching.

Code: %% Header

%Microcontroller Project-Night Train

%John Brown,Trent Payne,Karsten Parker;Section 9

%October 3, 2017

%Project Description: Design a microcontroller that takes two inputs and

%produces two outputs to help improve aspects of a model train setup

%Solution Method:Use various resources and Matlab to improve aspects of the

%model train setup.

%% Setup-First Input/Output

while true

a=0;%initializes a

while readDigitalPin(rpi,21) == 1

a=1;%stops light code from running before question dialog code

question=('The train is stopping at the station. Do you wish to sound the horn?');

question_title=('Train Horn');

resp=questdlg(question,question_title,'yes','no','no');%pops up question dialog box with two options and a default answer

tf=strcmp(resp,'yes');%compares character array length of response to character array yes.

if tf==1%if resp='yes'

[Y,FS]=audioread('train_horn.m4a');%takes audio file and converts it into sample data,y, and sampling rate,FS.

sound(Y,FS)%sound command takes sampled data and sampling rate and generates sound

msgbox('The train horn is sounding!')



else%if resp='no', tf will be logical 0 since no and yes char arrays are different lengths

msgbox('The train horn was not sounded!')





while readDigitalPin(rpi,21) == 1 && a==1 %starts while loop while switch is flipped on and question dialog box has run

%This first code segment turns on the lights in order.









%This second code segment turns the lights off in order.









end%end while loop


Step 4: Listen for the Train to Come Around the Corner, and Watch As the Lights Warn You to Step Back

Picture of Listen for the Train to Come Around the Corner, and Watch As the Lights Warn You to Step Back

As the train approaches and crosses the infrared sensors, the lights will go off, setting off the train horn to arrive passengers to step away from the edge; however, there will also be a dialogue box that pops up asking the train conductor, "The train is approaching the station, is the train stopping?", then a second reads "The train horn is sounding", and if the horn is not pulled, a third dialogue box will say, "the horn was not sounded."

Step 5: Final Setup

Picture of Final Setup

To complete the project, the whole system should be combined into the blue train station which was 3D Printed for aesthetics. The train station symbolizes where the passengers will be when the train arrives. Now they will be safe thanks to the Night Train warning system.


Swansong (author)2017-10-11

That would be a fun mod for a train set :)