Model Train Tunnel Circuit

djsfantasi

Joined Apr 11, 2010
9,163
I would approach the problem in a totally different way.

Assuming that each track through the tunnel is continuous (no diverging routes within the tunnel, which includes passing tracks or sidings). Then once the sensors at the portal on one end of the tunnel are activated, you toggle a flip flop. Once the sensor at the other end is activated, you toggle the flip flop again. One state is tunnel occupied/turn tunnel lights on.

You can OR the state of the two sensors and feed it to the CLK input of a T flip flop (or a 4013 configured as one)

The problem is that in one case, you want to trigger on the leading edge and in the other case you need to trigger on the trailing edge. One could invert one signal, but then it won't work for trains going in the opposite direction. So you still need two flip flops - one for each direction. And OR their outputs to the tunnel light control circuitry.

I need to do a timing diagram before I try a schematic. This also assumes you are protected against cornfield meets using DCC. However, in that case the tunnel lights going out unexpectedly might provide enough warning to avoid the inevitable. A"feature".

I concur with the earlier poster who opined that reading a schematic based on pinouts versus logic symbols, is too much work for me.
 

Thread Starter

bobinbd

Joined May 28, 2016
23
tunnel-circuit-gates.png
I would approach the problem in a totally different way.

Assuming that each track through the tunnel is continuous (no diverging routes within the tunnel, which includes passing tracks or sidings). Then once the sensors at the portal on one end of the tunnel are activated, you toggle a flip flop. Once the sensor at the other end is activated, you toggle the flip flop again. One state is tunnel occupied/turn tunnel lights on.

You can OR the state of the two sensors and feed it to the CLK input of a T flip flop (or a 4013 configured as one)

The problem is that in one case, you want to trigger on the leading edge and in the other case you need to trigger on the trailing edge. One could invert one signal, but then it won't work for trains going in the opposite direction. So you still need two flip flops - one for each direction. And OR their outputs to the tunnel light control circuitry.

I need to do a timing diagram before I try a schematic. This also assumes you are protected against cornfield meets using DCC. However, in that case the tunnel lights going out unexpectedly might provide enough warning to avoid the inevitable. A"feature".

I concur with the earlier poster who opined that reading a schematic based on pinouts versus logic symbols, is too much work for me.
I am attaching an updated version of the schematic with the gates in place rather than the components. Note that I had to use a custom shape for the 4 input NOR gate because CircuitLab doesn't have a 4 input NOR gate.

One little quirk you have to think about with the trains is this. When first approaching the tunnel (either direction) the first sensor will turn on the tunnel lights, however, when exiting the tunnel, the next sensor does NOT turn off the tunnel lights. Not immediately. The turn off signal has to come after the last car has gone past the sensor. So there is different behaviour for the sensor depending on whether it was the first trigger or the second trigger.

So how I handled it was this way:
First sensor is triggered, sets its flip flop to High, complement to Low and the NAND gate is High (the NAND gate will go low when the last car has passed the sensor and entered the tunnel).
Second sensor is triggered when the engine comes out of the tunnel, and this sets its flip flop to High, complement to Low and the NAND gate is High.

The first tripped sensor will have its NAND gate go low first. It has too! If there are still cars in the tunnel, we will have three low signals, the first NAND gate and the two complements of the flip flops. The NOR gate needs all of them to be low to generate the high signal for the Reset. This will happen when the last car clears the second sensor that was set when the train started exiting the tunnel.

This part does work well, but the reset signal is a little flakey. It only works if it goes through the voltage divider, which Albert Hall says shouldn't work.

I suspect there might be a better way to configure the set and reset signals. Maybe I can configure the J and K inputs or use the clock input somehow. Or maybe use a D flip flop (which had the same problem of only one flip flop resetting). Or maybe a 555 timer configured as a flip flop.

If you can follow my logic of what happens (with direction of events being irrelevant) then perhaps a better solution comes to mind. This is the first time I ever used a flip flop so I am quite open to suggestions!
 

Thread Starter

bobinbd

Joined May 28, 2016
23
The reset pulse will be exceedingly narrow. You need a pulse extender between 1Y and the two resets instead of the two resistors.
A monostable of some kind is indicated, but for this purpose this circuit would do the job but you will need to increase the capacitor to 470pF:
 

Thread Starter

bobinbd

Joined May 28, 2016
23
I implemented the circuit above that you suggested and that did not help the reset. I agree it makes sense that this would extend the pulse length and seems very logical, but I couldn't get it to work. I tried the 470pf, a 1uf and also a 100k resistor. That voltage divider is all that seems to work!

I didn't try it without the transistor.

Now when I say it does work, here is what I can determine.
If I keep the second sensor covered, so that both flip flops have been set and have my volt meter checking the output of FF Q1 complement, then uncover the sensor, the output goes from 0 to 5v. But the other FF Q2 complement stays at 0v when checked.
If I check FF Q2 and then uncover the sensor, the output goes from 0 to 5v but now FF Q1 complement stays at 0v. In other words, the act of check the status of the points affects the result. This is so totally quantum!

Maybe I should take a video of the breadboard!
 

AnalogKid

Joined Aug 1, 2013
11,055
I haven't stepped through all of the logic yet, but consider this - A set-reset ff, whether built out of gates or packaged in a 4013, has non-ff behavior when both inputs are true. This is shown on the 4013 datasheet logic table. It comes from the way the two outputs are derived.

ak
 

djsfantasi

Joined Apr 11, 2010
9,163
I understood the difference in entering the tunnel and leaving the tunnel. That is what I meant by triggering on the leading versus trailing edge of the pulse. The 4013 D flip flop is triggered on the leading edge. By running the trigger through an inverter (NOT gate), you can derive the trailing edge trigger from the sensor signal.
 

Thread Starter

bobinbd

Joined May 28, 2016
23
You show the two input NANDs with inputs unconnected.
I trust that those actually are connected to common. (?)
Yes those NAND's are connected together with the one input. Those connections seem to keep disappearing. Same with those wires that aren't lining up on my 4 input NOR gate (there is no actual 4 input NOR gate at CircuitLab).
 

Thread Starter

bobinbd

Joined May 28, 2016
23
I understood the difference in entering the tunnel and leaving the tunnel. That is what I meant by triggering on the leading versus trailing edge of the pulse. The 4013 D flip flop is triggered on the leading edge. By running the trigger through an inverter (NOT gate), you can derive the trailing edge trigger from the sensor signal.
I am not sure I understand what you mean. But let me try!
If the reset trigger goes through the NOT inverter, then its default condition will be NOT FALSE (so its output will be HIGH). So you are saying that would be an okay condition to leave the resets with a default HIGH signal and then when the NOR gate does momentarily trigger its output HIGH the NOT gate outputs LOW and that trailing edge trigger will then actually cause the reset?

So its the change in condition, either the signal rising to HIGH or falling to LOW that is the actual trigger?
 

djsfantasi

Joined Apr 11, 2010
9,163
None of these signals are going to the reset input. They are steered to the clock input. On a D flip flop wired as a T toggle flip flop, each rising edge toggles the output. So entering the tunnel, the leading edge will change the Q output to high. Upon exiting, the trailing edge inverted becomes the next leading edge input to the clock, and the output toggles/changes/resets
 

eetech00

Joined Jun 8, 2013
3,960
Hi

I think what is missing is third occupancy detection sensor circuit. If you can add/rearrange the occupancy sensors such that the following status is detected:

1. Tunnel west approach occupancy
2. Tunnel occupancy
3. Tunnel east approach occupancy.

Then you could use a directional stick circuit that could reliably detect when the train enters and clears the tunnel from either direction.
I'm attaching a circuit that should work. Each FF's Q output is set high based on direction of traffic and are interlocked so that only one can be set at any time. When the train leaves the tunnel and the trailing sensor detects light, it resets the FF. The WQ and EQ lines would be used to drive the circuits that light the leds.

The attached graphs show the state of the WQ and EQ signal lines for both westbound and eastbound movements. The sensors are represented as A, B, and C with B being the new sensor. (maybe you could setup an additional LDR inside the tunnel, that is lit by and LED mounted inside/top of the tunnel such that a train would break the beam when it passes thru the tunnel?)
 

Attachments

Thread Starter

bobinbd

Joined May 28, 2016
23
Hi

I think what is missing is third occupancy detection sensor circuit. If you can add/rearrange the occupancy sensors such that the following status is detected:

1. Tunnel west approach occupancy
2. Tunnel occupancy
3. Tunnel east approach occupancy.

Then you could use a directional stick circuit that could reliably detect when the train enters and clears the tunnel from either direction.
I'm attaching a circuit that should work. Each FF's Q output is set high based on direction of traffic and are interlocked so that only one can be set at any time. When the train leaves the tunnel and the trailing sensor detects light, it resets the FF. The WQ and EQ lines would be used to drive the circuits that light the leds.

The attached graphs show the state of the WQ and EQ signal lines for both westbound and eastbound movements. The sensors are represented as A, B, and C with B being the new sensor. (maybe you could setup an additional LDR inside the tunnel, that is lit by and LED mounted inside/top of the tunnel such that a train would break the beam when it passes thru the tunnel?)
Your circuit intrigues me, but it is quite different than mine. You might be on to something better than what I did. I do have some logic obstacle detectors that I use inside a tunnel at a turnout but I don't know why I need to know where the train is in the tunnel. A one engine train going through the tunnel would still not be detected by any sensor.

But one thing to consider is that this setup is exactly the same as a regular grade crossing circuit that turns on the flashing lights when a train approaches.
The train approaches from either direction, passing the first signal detector (LDR) turns on the detection circuit (LEDS) and only after the last car of the train passes the second signal detector (LDR) does the detection circuit turn off.

At some point though, the train could be long enough to be over both sensors or short enough that it is over neither sensor (maybe stopped in the tunnel) so the circuit has to wait for the second sensor to trigger and then be cleared! If the train stops or slows between the detectors, everything stays on until the second detection condition is met.

One thing to remember is the first LDR will trigger the lights on as soon as it is covered. It doesn't matter how long it stays covered. But the second LDR to be triggered (and either LDR can be the first or second trigger) doesn't really change anything (it does set a flip flop) until AFTER it is uncovered.

In my original diagram the second LDR sets the flip flop (when I really want latches I think) and once both flip flops are set then the system resets once both LDRs are uncovered. That is the condition my 4 input NOR gate is waiting for which should only occur when both flip flops have turned on and then both LDRs are uncovered.

Now, if the train is long enough to still be covering the first LDR while tripping the second LDR that is fine too because the NOR gate condition still won't be met to reset until the last car has passed over the second LDR.
I know I really should be using latches instead of flip flops because I don't think a clock signal is important.

So to recap:
The LDR's provide two signals:
[1] Trip a flip flop
[2] Am I covered?
 

eetech00

Joined Jun 8, 2013
3,960
Your circuit intrigues me, but it is quite different than mine. You might be on to something better than what I did. I do have some logic obstacle detectors that I use inside a tunnel at a turnout but I don't know why I need to know where the train is in the tunnel. A one engine train going through the tunnel would still not be detected by any sensor.
You don't need to know "where it is" in the tunnel....just "If its in" the tunnel. But you also need to know when its about to enter or leave the tunnel. That is why I ask if you could install an additional sensor(s) inside the tunnel.

But one thing to consider is that this setup is exactly the same as a regular grade crossing circuit that turns on the flashing lights when a train approaches.
The train approaches from either direction, passing the first signal detector (LDR) turns on the detection circuit (LEDS) and only after the last car of the train passes the second signal detector (LDR) does the detection circuit turn off.
Yes...that is exactly what it is. But your requirements face the same issues, without a requirement for being vital (failsafe) of course.

At some point though, the train could be long enough to be over both sensors or short enough that it is over neither sensor (maybe stopped in the tunnel) so the circuit has to wait for the second sensor to trigger and then be cleared! If the train stops or slows between the detectors, everything stays on until the second detection condition is met.
Yes. but if you had three sensors...short or long trains wouldn't be an issue as long as the sensors were properly spaced.

One thing to remember is the first LDR will trigger the lights on as soon as it is covered. It doesn't matter how long it stays covered. But the second LDR to be triggered (and either LDR can be the first or second trigger) doesn't really change anything (it does set a flip flop) until AFTER it is uncovered.

In my original diagram the second LDR sets the flip flop (when I really want latches I think) and once both flip flops are set then the system resets once both LDRs are uncovered. That is the condition my 4 input NOR gate is waiting for which should only occur when both flip flops have turned on and then both LDRs are uncovered.
Yes.....but what is odd is that you are setting both FF's. You only need to establish direction then set one of the FF's, then reset it when the move is completed.

Now, if the train is long enough to still be covering the first LDR while tripping the second LDR that is fine too because the NOR gate condition still won't be met to reset until the last car has passed over the second LDR.
I know I really should be using latches instead of flip flops because I don't think a clock signal is important.

So to recap:
The LDR's provide two signals:
[1] Trip a flip flop
[2] Am I covered?
You don't need a specific latch device. The D Flip Flop can function just fine as a latch by connecting the D pin to the appropriate logic level prior to receiving a rising edge on its CLK pin. In fact, I have a version of the circuit using discrete AND/OR/NOT gates but the D FF really simplifies the design. You could practically design the required logic with just diodes and resistors. The D FF can be set/reset by momentarily applying logic high to either the CLR or PRE pin.

I think you need three separate occupancy detection circuits (see my previous comments above) to get the operation you want.
 
Top