Sensing Train Location

revised 12-15-06



The "eyes" of the switch control system allow the microcontroller's "brain" to know where the trains are located on the layout.  This insures that only one train is on the main line at a time and that the switches are set so that the train returns to the proper siding.


There are many ways to sense whether or not a train is at a particular spot on a layout.  In 2005 I wrote a three part series for LSOL (Garden Railway Sensors, Part I, Part II and Part III) that explored a number of ways of sensing a train's location.  I finished up that series with a detailed description of what I feel is the best choice for most of our outdoor train sensing needs.  This is device is called a pulsed infrared sensor.  I will briefly revisit it here and ask that you refer to the earlier articles for more detailed information.


Pulsed infrared sensors are made up of several components.  The most visible parts go on either side of a piece of track, the infrared emitter on one side and the infrared detector on the other.  When a train passes between them the beam of IR light is broken in much the same way that some automatic doors sense a customer's presence.  What really sets this sensor apart is that the devices operate with infrared light that is pulsed on and off at 38 kHz.  This means that the infrared emitter sends out bursts of IR 38,000 times each second and the detector only reacts if it detects IR pulsing at that frequency.  This is important because there are many sources of infrared light in and around our garden railroads.  These sources include fluorescent and other lighting devices but most of the extraneous IR comes from the sun.  Pulsed IR sensors are not completely immune from the effects of direct sunlight but they perform better than most other technologies.  By the way, you use 38 kHz pulsed IR every time you use a TV's remote control.


The IR detector that we will use is a PNA4602M, a device with only three connections.  One goes to +5 volts, one goes to ground and the output pin goes directly to an indicator, a transistor activated relay or, in our case, to the microcontroller.  When the detector "sees" 38 kHz IR the output pin shows 0 volts and is said to be "low".  When no IR is present it shows 5 volts, referred to as "high".  This swing between 0 and 5 volts is easily detected by the microcontroller so that appropriate decisions can be made.


The sensors were constructed by drilling 3/32" holes near the tops of 2" x 4" x 3/4" blocks of wood.  The emitter is glued into one and the detector into the other.  In this photo of one of my prototypes the detector is on the near block and the emitter is on the one on the far side of the track.



Wiring can be seen from the diagram below and the photo.  Note that there is a 0.1 mfd tantalum capacitor, the mustard colored device in the photo, between the positive and ground leads of the detector.  This helps to keep this very sensitive device from giving false readings.  The three IR LEDs are connected to one of the output pins of the PICAXE that is programmed to generate the 38 kHz pulses that the PNA4602M detectors can sense.  Each of the detector output pins go to an input pin on the PICAXE.



One of these sensor units is placed just before the end of each block so that the engine breaks the IR beam as it nears the end of the block.  In the photo below you can see the IR detector side of sensor number 2.  The emitter is behind the 0-4-0 locomotive.


Ambient Light Sensors

If a layout is indoors it is not necessary to use pulsed IR sensors as ambient infrared is less prevalent and more easily controlled.  In such a situation simple photo transistors that are sensitive to visible light can be used.  The phototransistors that I used are similar to ones that are available from The Electronic Goldmine (see: ).  These devices, shown below, are NPN transistors that allow current to flow between their emitter and detector when light falls on the lens on the face of the case.  A second NPN transistor is used to amplify this signal.



The schematic shows three phototransistors that are used to detect the position of trains on three blocks.  Note that the phototransistors' emitters are connected to the base of 2N2222 NPN transistors.  The 2N2222 amplifies the output of the phototransistor and also allows sensitivity to be adjusted by the variable resistor that is attached between the 2N2222's emitter and ground.  The output of the 2N2222 goes to the PICAXE but could just as easily connect to a relay or an indicator light.  If you are wondering about base lead that most transistors have, it is usually missing from phototransistors as light is what serves in place of the base lead.

In this photo you can see a phototransistor mounted in a piece of Plexiglas that has been cut to fit between the ties.  Its lens points straight up so that it is exposed to overhead light until a train passes over it.  The Plexiglas has been painted black so that it does not inadvertently illuminate the phototransistor with light from the sides of the track.  The box at the left is for an RJ-11 connector that allows the sensors to be connected to the controller with standard 6 conductor telephone cables.  These modular cables make setup a "snap"!



Ambient Light Sensors with LEDs

The ambient light sensors described above work well if your layout is in a place that is consistently bathed in fairly bright overhead light.  If you have a layout that is sometimes dimly lighted you may need to add a separate light source for each phototransistor sensor.


I did this when I made up a set of these sensors for a recent train show.  The Pittsburgh Garden Railway Society was invited to set up a layout and I wanted to automate some of the operation using this system.  Since I didn't know in advance what the brightness level might be in the convention hall I added a bright, white LED to each sensor.  As you can see from the photo the LED was mounted at the side and the beam was adjusted to shine directly onto the face of the phototransistor.  The setup worked well and I didn't have to be concerned about the overhead light level.  We also found that the angled light and its support were easily camouflaged by trees and other objects.


If you look carefully you should be able to see that the phototransistor is mounted in an angled hole that is aimed at the LED.



Although infrared emitters and phototransistors are available and would work well in this situation I like to be able to see the light so that aiming is simplified.  As you can see from the photo below there is little doubt when the beam from the LED hits the sensor!



Current Sensing

I have also experimented with devices that sense block occupancy by measuring the amount of current being drawn by motors or other devices that are present when a train is in a block.  While this kind of sensing can be done I have yet to be 100% satisfied with the results.  If and when I come up with a more reliable circuit I'll let you know.


To Be Continued...

We're getting close to the end.  We have looked at nearly all of the hardware components that are needed, block control, turnout control, diodes speed control and sensors.  What remains is the "brains" of the unit, the microcontroller.  Next time we'll pull it all together with a PICAXE based control board and the software to make it all work.


Stay tuned!