JonMcCain.Net
[top]

Using An Ardunio Uno To Control A Train

I wanted to put a model train up on the cabinets in my kitchen.  Due to limited space I wanted it to go back forth instead of a circle. The solution was to use an Ardunio Uno to control it.  It has sensors to detect when the train gets to the end of the track. A motor chip is used to control the power to the train and relays are used to control a track switcher.  It will randomly switch tracks to give a little variety. 

Here is where the train is going be to give you an idea of how things will look.

cabinet

The theory is simple.  A motor chip (LM293D) can control the direction.  This is dc train so we just need to flip positive and negative to go backwards or forward.  The ardunio can not provide enough current to drive the motor directly so the power for the train will come from the train transformer. Pulse width modulation (PWM) is used to control the speed so the train speeds up/slows down instead being full on or off. This also helps it to stay on the track.

ardunio ardunio2

motorchip transformer

Two relays will control the track switch.   When the train is at the right hand sensor it will randomly decide to flip the track or not.   To guarantee it does really switch sometimes, at every third time it switches regardless of the random number because Ardunio's don't really have a random number generator and the software implementation is not that great.

relays switch

origsensor Sensors at each end of the track will detect when the train is there to stop it, change direction and start it again. They were my biggest problem.  I wanted to make my own. The initial idea was to use an IR LED to send beam of light that would bounce off the train and get picked by an IR detector.  But sunlight created a problem because it also contains IR light so the detector was always on. 

Next I tried a more directional approach by making the IR LED point directly at the IR detector and the train breaking the beam would indicate it was there.   But sunlight could still interfere with it at the right angle.

sensor2 sensor2circuit

Eventually I gave in and just bought some sensors that worked on the same theory of the light bouncing back but they use frequency modulation to set the beam at a certain freqenncy so the detector can tell the difference between the natural light and the IR LED light it generated.

finalsensor



So here is the full scale test.

fullest

Here is the full circuit

traincircuit7web

Here is the Ardunio source code.

Date: 11/27/2018