Model Train Tunnel Circuit

Discussion in 'General Electronics Chat' started by bobinbd, Nov 21, 2016.

  1. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    I have developed a circuit to turn on/off tunnel lights on my HO railroad. This circuit is to replace a previous circuit that works pretty well except when train lengths are short!

    In the rest or normal state, the tunnel lights are off and monitor led is green (a three wire red/green led common anode).

    The idea is that when a train goes over either LDR embedded in the track at the entrances to the tunnel, the lights in the tunnel turn on, and when the entire train has exited, the lights turn off. The idea here is to use two Flip-Flops, one for each tunnel entrance, that are set when a train is detected. Once either of the flip flops is set, transistor Q1 turns off and transistor Q2 turns on. The signal for this is from the quad input NOR gates on the CD4002 (Y2) which is triggered when either or both of the Flip Flops are turned on ( triggered by 1Q and 2Q).

    The set signal of the Flip Flops comes from the NAND gates that send a +5 signal when the LDR's are covered. This all works great!

    The more difficult issue is the Reset. The condition for this is: Both flip flops must be on (set), which means a train has entered the tunnel AND it has also left the tunnel (triggering both Flip Flops) and both LDR's must be uncovered (last car has passed). At this point all inputs to the first NOR gate are set to 0 (2 NAND gates and the two complements of 1Q and 2Q of the flip flops). This sets 1Y to +5 and this signal goes to the two resets of the Flip Flops. Both flip flops will always be reset at the same time. This is where it gets dicey. I needed to take the signal for the resets from a voltage divider. Directly linking the two resets to the output of 1Y didn't work. Only one flip flop would reset.

    Most of the time, the circuit starts up in the rest state, but I have been unable to figure out how to make sure the system is reset on power up.

    I had used a CD4013 (D flip flop) but I could not get the reset to reset both flip flops. One would always go back to set again.
    I don't know why I couldn't get that one to work. Sometimes it only worked if the voltage meter was connected.

    I am quite the novice at this, by the way.

    So, there must be a few things wrong in the circuit, but I don't know where!
     
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    7,750
    1,903
    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:
    [​IMG]
     
  3. hp1729

    Well-Known Member

    Nov 23, 2015
    2,304
    284
    I don't see how that 1Y output is going to ever go high to reset the latches. At any given time one of the latches is reset, outputting a high.
    I am unclear where the sensors are? Do they sense cars or weight on that section of track?
     
  4. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    tunnel-circuit-with-delay-common-anode.png
    The sensors are imbedded in the HO track but they are exposed to the room lighting. They typically register about 2.4 - 5 ohms in the light and over 20k ohms when a car or engine passes over top and shades them.

    The sensors are GL5516 ldrs. Each one is attached to it's own NAND Gate so that when it is darkened, the resistance goes up, the voltage level drops to about 1.5v in the voltage divider and the NAND gate generates a 5v signal. This signal sets the flip flop 1Q (or 2Q) to 5v which also sets the complement 1Q (or 2Q) to 0v.
    When the second flip flop is also triggered, it's complement 2Q (or 1Q) also goes to 0v. Both flip flops need to set before a reset can occur. (1C and 1D of the 4 input NOR gates). The NAND gate will stay positive until the last train car has passed, at which time 1A and 1B of the 4 input NOR gates will also be 0v which will trigger a positive output from 1Y. In the normal rest position, with the ldrs uncovered by a train and the flip flops not set, the 1A 1B inputs will be 0v and the 1C and 1D (from the flip flops) will be 5v so 1Y will only be positive after both flip flops have been set by a NAND gate.

    Also note that a NAND gate which sets the flip flop, will only return to 0v output after the last train car has passed over it and light is shining on it again!

    I think I may need to add a RC delay to the NAND signals to account for chance that they switch to a zero output between cars when light can reach the LDR but I will cross that bridge when I have to!

    Attached is a schematic of the three circuits that I have employed on the layout for three tunnels. The LDRs are about 12 inches from the mouth of each end of the tunnels. Tunnels are about 18" to 30" in length. In this schematic, the tunnel lights turn on as long as a car of the train is covering the LDR with about a 3 second delay added. For a regular length train of about 7 -10 cars, the engine will cover the ldr at the exiting end of the tunnel before the entrance ldr is uncovered or very shortly afterward, depending on speed. With a slow moving short train, the tunnel lights will go out prematurely. That's why I thought up the new circuit!

    I have been reading up on this electronics stuff for about a year after I bought an Arduino electronics kit which I thought was like the electronic kits from 30 years ago. It isn't, but that's another story!
     
  5. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    I am going to test this out on my breadboard and I will let you know the results. I don't want to try to solder this on perfboard until I am sure I have all the right pieces because I know it will take me 8 hours of soldering.
     
  6. hp1729

    Well-Known Member

    Nov 23, 2015
    2,304
    284
    Thanks. I see now. The tunnel only goes one way, or does it work in either direction?
    Does the new design have one too many inverters? With no traffic the tunnel lights are on?
     
    Last edited: Nov 22, 2016
  7. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    Yes, either direction, so the circuits have to work regardless of which way the train is moving. That does add a complication! There is no way of knowing which ldr will be activated first. So first doesn't matter. In the circuit with the flip flops it only matters that both have been triggered (or set) but not which is first or second.
     
  8. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    7,830
    2,203
    Are any of the tunnels dual-track, where trains might be going through in both directions?

    Also, your circuits will be much easier for us to understand and discuss if you stop using chip packages as the schematic symbol, and use logic diagram symbols for gates, flipflops, etc. The first job of a schematic is to convey the signal relationships, not the wiring relationships. For example, your schematic in post #1 would be much easier to interpret if the flipflops and logic gates were shown individually. Few people carry the entire CMOS 4000 series pinout library in their head.

    ak
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,980
    1,842
    I'm very attracted to this problem due to a long ago shared hobby and an intuition that is clambering for more detail to solve this.

    Questions:

    Can a train be long enough to cover both an entrance and an exit?

    Do you want the empty state to be issued when the front of the train hits the exit sensor or after it leaves the tunnel/sensor?

    This would be ez-peezy to do with a tiny micro controller, not that I am recommending this as it is a steep learning curve for you. But is it enough fun for someone to write some code and snail mail you a device that does it all. (I would volunteer but my work schedule is flipping crazy for the rest of the year.)

    Some simple flip flops and gates should do this.
     
  10. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    7,830
    2,203
    There are four possible logical sequences to implement: long and short trains, times left and right direction. Long trains in either direction are the easy pair. Short trains are a problem. If they have a minimum length and minimum speed, then a timer can discriminate between both sensors being exposed as a reset or as a gap between entering and exiting. Still, two of the cases are tricky.

    ak
     
  11. Dodgydave

    AAC Fanatic!

    Jun 22, 2012
    8,067
    1,385
    How about a missing pulse detection circuit with a 555 chip, put the photo led sensors at each end of the tunnel, as long as the train interrupts the beam the lights are on, when the train exits the tunnel, the timer times out...
     
  12. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    tunnel-circuit-gates.png
    Thanks for the suggestion. I have updated the diagram to include the gates. I don't have any double tracked tunnels, but there is one that has a turnout at the entrance, but I think that logically I could just add the input for that track to NAND gate for it's partner track (imagining there is one flip flop for the east entrance and one for the west entrance.

    I forgot to mention that I tried this circuit with a D flip flop (CD4013) but the reset was only working on one flip flop, not both (I had D and CLK connected to ground). They would both flip, but then one would flip set again.
     
    Last edited: Nov 22, 2016
  13. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    The empty state occurs when the last car has cleared the exit sensor (either sensor can be the exit sensor depending on which direction the train is travelling in.

    So the logic is:
    First sensor is triggered - Flip Flop is set High.
    Second sensor is triggered some time later (on exit) - Flip Flop is Set High
    When both flip flops are set high, that is the reset condition (Q complements are low) but only when both sensors are also Low. When the sensor is triggered, it stays high until the last car has passed at which point the Q complements are both Low and the two Sensors are Low and the Reset is triggered.

    As for using an Arduino. That would be very simple. I had no clue there was such a thing until after I bought it, but I am a programmer so it might take half an hour to code the logic. I just thought doing this with logic chips is much more challenging and rewarding!
     
    Last edited: Nov 22, 2016
  14. dendad

    Distinguished Member

    Feb 20, 2016
    2,744
    738
    With an Arduino, you could have 2 x light beams across each end of the tunnel and have then connected as a couple of quadrature inputs. then as the train passes through, it counts up as it goes in and down as it exits. When the counter is non zero, the lights go on. An added time out would be a good idea in case of missed counts.
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,980
    1,842
    I see the states as follows:

    With the sensor as in Post 4, each gate output is low with no train, high with train.

    Initially both are 0. When a train comes one goes high... and after then end of the train passes it low again. The exit sensor will at some point go high, and then low as the end of the train passes.

    That needs two flip flops, as either can set the state, but the reset depends on which sensor set the state. Plus some other gates... Perhaps I can sketch something tonight, or better this will inspirer someone to post something before I even get home.
     
  16. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    7,750
    1,903
    To get around the gaps between carriages, you could use two LDRs connected in series at each end. If one of them is exposed to light at a gap, the other one will be covered and the series combination will still be high resistance. It will go low resistance only when both are exposed. That way the train can go very slowly without confusing the circuit.
     
    ErnieM likes this.
  17. AnalogKid

    AAC Fanatic!

    Aug 1, 2013
    7,830
    2,203
    One of the NOR gate outputs is divided by 2 before driving the reset inputs. This will not work. What is the reason for the two resistors?
    Also, the two transistors have identical base and emitter connections, so the two collectors act as one. You could eliminate one transistor. Is this your intent?

    Also, not to be picky or anything, but a rectangular box is nor the right symbol for a NOR gate.

    ak
     
  18. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    tunnel-circuit-with-delay-gates-common-cathode.png
    Now that sounds like an interesting idea!
    That would definitely work. I would probably have to increase the 10k voltage divider resistor by a little bit. It all depends on the room lighting but that is pretty minor. A very elegant solution!


    Inserted is a simpler diagram of the circuit I am using now. This circuit generally works fine. The only issue with it is with a short train or a slow moving train. The tunnel lights only stay on when either LDR is covered and for a short time delay afterwards.

    My new circuit tries to more logically determine when the train is actually out of the tunnel (using the flip flops to store the information). Except there might be a cleaner way of using the flip flops than just triggering set and reset.
     
  19. bobinbd

    Thread Starter New Member

    May 28, 2016
    23
    4
    The reason for the rectangular box for the 4 input NOR gate is because I am using CircuitLab for the diagrams (just signed up) and I can't find a 4 input NOR gate object. Just 2 and 3 input. So no choice there I think! If you know a way how to do that, that would be awesome!

    The two transistors are a NPN and PNP (2N3904 and 2N3906). Only one is on when the base is either HIGH or LOW. They Flip Flop! The Red/Green LED is a three wire Common Anode and then the Orange lights (used in the tunnels) are connected in parallel with the RED led. The green and red are tunnel clear or tunnel occupied signals.

    Now, I agree there is an issue with the voltage divider for the reset signal. But this does work. When I connected the two reset pins directly to the Y1 output, one of the flip flops (I think number 2) would SET itself again. My voltage meter showed it's Q output going to 0 and then almost instantly back to 5v again. The voltage divider stopped this. But that indicates to me there is something subtly wrong with the circuit. Maybe a stray signal bouncing around. I just don't know what exactly is happening or why.
     
  20. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    7,750
    1,903
    You have a race condition because the reset pulse, in resetting the flipflops, removes the condition generating the reset pulse. This means the reset pulse will be very narrow and too short to properly reset the flip-flops. You need to extend the length of the reset pulse so that the flip-flops are properly reset.
     
Loading...