Melaleuca station building banner
Last Updated Aug 21, 2011

Detection of trains using Light Dependent Resistors (LDR).

Introduction     

One of the biggest problems with all scales of model railways, is to be able to detect trains - whether for operating signals, points or accessories like level crossings. Lots of people use magnets and reed switches but this requires every loco to be fitted with a magnet. I’ve used complicated infra-red beams to operate my 2 reverse loops but they are hard to set up because you can’t see the beams to align, and you always face the problem of sunlight saturating the receiver so it doesn’t detect.

Now I think I’ve devised a really simple circuit using two Light Dependent Resistors (LDR). These only cost a couple of dollars (for those of you in Australia see Jaycar part RD3840 or Oatley electronics part 2D0054 - 5 for $3).

They are only about 5mm dia. as shown at left
I haven’t had much experience with them outside, so we’ll see over time.
Their resistance varies from about 300 ohm in direct sunlight to a couple of Megohms in the dark. Typical values for outside pointing upwards, are from 300 ohm in sun to 5k in deep shade and cloudy.


The secret is to use two of them, one between the rails and the other outside the width of the wagons, with both of them pointed straight up to the sky. Here's my test setup on a piece of veroboard.

They are connected in series across a voltage and the mid-point connection is always around ½ supply volts when there is no train present, irrespective of ambient light levels, as they both see the same light. When a train covers the one between the rails, its resistance increases so the voltage changes. Here’s the basic circuits.



Typically, in case 1, with no train the voltage is around 45-55% and with a train less than 25% of the supply volts. For case 2 the no train volts are again 45-55% and with a train, greater than 75%.
All independent of voltage and ambient light !
The circuit is not 100% reliable, but that’s not a problem for most of our applications. For instance, a leaf or building casting a shadow over the LDR outside the rails but not over the one between the rails will make it harder to detect the train.


Now we need a SIMPLE circuit to do something with them!
The simplest circuit, just provides a LOW output when a train is detected. This could be used as an input to any other circuit such as a point motor, level crossing lights or a signal.
NOTE this will only work for a 5V supply as it depends on the FET gate to source turn-on voltage of around 3V. D3 is to adjust for the FET turn-on voltage.
You may need 2 diodes in series for D3 for more positive detection.


Simple Signal.
As an extension, here’s a really simple circuit to operate a red and a green LED as a colour light signal. When the train covers the LDR the red LED lights immediately, when the train clears the LDR, the red LED slowly dims and the green one slowly brightens over a few seconds. The time is set by the capacitor. So you get red for the train detected, red+green = caution for a few seconds as it departs, and then green = track clear. You can choose C1 for the time required. The delay will depend on the FET type, but start with 0.1 uF and see how you go. Alternatively use a 100 uF and a 470k in parallel for about 5 sec. All diodes are typical 1A, 100V types (IN4001).


Using a 555 Timer
If you want a more precise signal operation with only red or green LEDs lit, we can use the ubiquitous 555 timer as a comparator. It provides up to 200 mA of current and can operate from 5 to 15V supply.

Note in this circuit, the LDR between the rails is in the positive side of the circuit. In this case the output, pin 3, goes HIGH when a train is present.
When the train covers the LDR between the rails, the red LED lights instantly. After the train leaves, it stays lit for a time determined by R2 and C1, then the green LED lights and red extinguishes. 10uF and 1M gives around 15 sec.
If you don’t want the time delay , delete R2, C1 and D1 (you’ll probably have to reduce R1 to about 3.9k)
If you make R1 a 10k pot you can adjust the level where the LEDs operate.


Using Picaxe
I wanted to operate a servo to raise a level crossing boom gate, so I thought of a servo motor. To drive this I use my favourite circuit component – a Picaxe 08M chip for both detection and servo control.
I’m using two of the LDR detector pairs – each one about 1m either side of the level crossing. When either detects a train, the boom lowers, taking about 1 sec. When both detectors are clear, a 10 sec timer starts and after the delay, the boom raises.


The picaxe program monitors both LDR detectors and provides the timer function and servo control. It also provides a flashing output on leg 6 which can drive alternate LEDs and a bell, with a few external components.
Pretty good with only the 3 external components ! More detectors can be connected via diodes if needed.