Introduction: Model Railroad Automatic Tunnel Lights

About: Started playing with electronics to add extra character to my model train layout. The hobby keeps growing.

This is my favourite circuit board. My model railroad layout (still in progress) has a number of tunnels and while probably not prototypical, I wanted to have tunnel lights that turned on as the train approached the tunnel. My first impulse was to buy an electronic kit with parts and leds, which I did. It turned out to be an Arduino kit but I had no idea what an Arduino was. I did find out. And that led to an adventure of learning some electronics. At least enough to do tunnel lights! And without an Arduino.

This is at least my third version of the tunnel lights circuit board. The basic design I discovered in one of the projects of the book Electronic Circuits for the Evil Genius 2E. This is a great learning book! I also discovered using integrated circuit chips, specifically the CD4011 quad input NAND gates.

Step 1: The Circuit Schematic

There are three signal inputs to the tunnel lights circuit. Two are LDR inputs (light dependent resistors) and one is an optional obstacle detector circuit board. The input signals of these devices are logically assessed by NAND gate inputs of the CD4023 (triple input NAND Gates).

There is one green/red common anode LED (which will be used on the display panel indicating a train is occupying a specific tunnel or approaching the tunnel). The green will indicate a clear tunnel and the red will indicate an occupied tunnel. When the red led is on, the tunnel lights will also be on.

When any of the three inputs detects a signal condition the NAND gate output will be HIGH. The only condition when the first NAND gate output is LOW is the single condition when all inputs are HIGH (all detectors at default condition).

The circuit includes a P-CH mosfet which is used to protect the circuit from mis-wired power and ground. This can easily happen when wiring the circuit board under the layout table. In previous versions of the board I used a diode in the circuit to protect the circuit from switching the ground and power wires, but the diode consumed .7 volts of the 5 volts available. The mosfet does not drop any voltage and still protects the circuit if you get the wires wrong.

The HIGH output of the first NAND gate passes through a diode to the next NAND gate and is also connected to a resistor/capacitor time delay circuit. This circuit maintains the HIGH input to the second NAND gate for 4 or 5 seconds depending on the value of the resistor and the capacitor. This delay prevents the tunnel lights from flashing on and off when the LDR is exposed to light between cars passing over and also seems a reasonable amount of time as the delay will give the last car time to enter the tunnel or exit the tunnel.

Inside the tunnel the obstacle detector will keep the circuit activated as it also monitors the passing of the cars. These detector circuits can be adjusted to spot cars just a few inches away and also not be triggered by the opposite wall of the tunnel.

If you choose not to connect the obstacle detector inside the tunnel (short tunnel or difficult) just connect the VCC to output on the 3 pin obstacle detector terminal and this will maintain a HIGH signal on that NAND gate input.

Two NAND Gates are used to allow a place for the RC circuit to be implemented. The capacitor is powered up when the first NAND gate is HIGH. This signal is the input to the second NAND gate. When the first NAND gate goes LOW (all clear) the capacitor keeps the signal to the second NAND gate HIGH while it slowly discharges through the 1 10m resistor. The diode prevents the capacitor from discharging as a sink through the output of NAND gate one.

Since all three inputs of the second NAND gate are tied together, when the input is HIGH output will be LOW and when the input is LOW, output will be HIGH.

When the output is HIGH from the second NAND Gate, the Q1 transistor is turned on and this turns on the green led of the three wire red/green led. Q2 is also turned on but this just serves to keep Q4 off. When the output is LOW, Q2 is turned off which causes Q4 to turn on (and also Q1 is turned off). This turns off the green led, turns on the red led and also turns on the tunnel light leds.

Step 2: Tunnel Light Images

The first image above shows a train entering the tunnel with the overhead LED turned on.

The second image shows an LDR embedded in the track and ballast. When the engine and cars travel over the LDR they cast enough of a shadow to trigger the tunnel LEDs to turn on. There is an LED at each end of the tunnel.

Step 3: NAND Gate Voltage Divider

The LDR's individually create a voltage divider circuit for each of the inputs to the NAND gates. Resistance values of the LDR's increase as the amount of light decreases.

The NAND gates logically determine that input voltages of 1/2 or greater when compared to the source voltage are considered as a HIGH value and input voltages less that 1/2 of source voltage are considered a LOW signal.

In the schematic, the LDRs are connect to input voltage and the signal voltage is taken as the voltage after the LDR. The voltage divider is then made up of a 10k resistor and also a variable 20k potentiometer. The potentiometer is used to allow for control of the input signal value. With varying light conditions the LDR may have a normal value of 2k - 5k ohms or, if in a darker location of the layout it may be 10k - 15k. Adding the potentiometer helps to control the default light condition.

The default condition (no train in or approaching a tunnel) has low resistance values for the LDRs (generally 2k - 5k ohms) which means the inputs to the NAND gates are considered HIGH. The voltage drop after the LDR (assuming 5v input and 5k on the LDR and a combined 15k for the resistor and potentiometer) will be 1.25v leaving 3.75v as input to the NAND gate. When the resistance of an LDR is increased because it is covered or shaded, the INPUT of the NAND gate goes low.

When the train passes over the LDR in the track, the resistance of the LDR will increase to 20k or more (depending on lighting conditions) and the output voltage (or input to the NAND gate) will drop to about 2.14v which is less than 1/2 source voltage which therefore changes the input from a HIGH signal to a LOW signal.

Step 4: Supplies

1 - 1uf capacitor

1 - 4148 signal diode

5 - 2p connectors

2 - 3p connectors

1 - IRF9540N P-ch mosfet (or SOT-23 IRLML6402)

3 - 2n3904 transistors

2 - GL5516 LDR ( or similar)

2 - 100 ohm resistors

2 - 150 ohm resistors

1 - 220 ohm resistor

2 - 1k resistors

2 - 10k resistors

2 - 20k variable potentiometers

1 - 50k resistor

1 - 1 - 10m resistor

1 - CD4023 IC (dual triple input NAND Gates)

1 - 14 pin socket

1 - obstacle avoidance detector ( like this )

On my circuit board I have used an IRLM6402 P-ch mosfet on a little SOT-23 board. I have found the SOT-23 p-ch mosfets to be cheaper than the T0-92 form factor. Either one will work in the circuit board as the pinouts are the same.

This is all still a work in progress and I think some resistor values or some improvements can still be made!

Step 5: The PCB Board

My first working versions of the circuit board were done on a breadboard. When the concept was proven to work I then hand soldered the entire circuit, which can be very time consuming and generally I always wired something wrong. My current working circuit board, which is now version 3 and includes the triple NAND gates (earlier versions used the CD4011 dual NAND gate inputs), and as shown in the video, is a printed circuit board with output files generated by Kicad which is my circuit modelling software.


I have used this site for ordering the PCB's: https://jlcpcb.com/

Here in Canada the cost for 5 boards is less than $3. Shipping tends to be the most expensive component. I will usually order 4 or 5 different circuit boards. (The second and more circuit boards are about double the price of the first 5). Typical shipping costs (by mail to Canada for various reasons) is about $20. Having the circuit board pre built so I just have to solder in the components is a great time saver!

Here is a link to the Gerber Files that you can upload to jlcpcb or any of the other PCB prototype manufacturers.