Reversing a model railway

Hello,

I'm helping my dad build a model railway and unfortunately he doesn't have the space to build a full oval for his trains to run around, so we've built a u-shaped track for them to run end to end.
I figured out a way to stop and reverse the trains automatically using a couple of relays and I've wrote a simple arduino code to control it with timing delays.
This works fine but it's not the most accurate thing and its difficult to set it up so that they start and stop from the same positions. I have a couple of the HC-SR04 sensors and was wondering if anyone had any suggestions for using two of these to start and reverse the trains from either end in the same way.
I've programmed a single sensor before but I'm unsure how to incorporate multiple sensors in order to change outputs like that.

Thanks in advance

My timing code:

int relayPin1 = 8;
int relayPin2 = 7;
int relayPin3 = 6;
int relayPin4 = 5;

void setup() {
// put your setup code here, to run once:
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
delay(2000);
digitalWrite(relayPin4, HIGH); //direction relays changed
digitalWrite(relayPin3, HIGH);
delay(2000);
digitalWrite(relayPin1, HIGH); //power relays set, train moves forward
digitalWrite(relayPin2, HIGH);
delay(10000);
digitalWrite(relayPin1, LOW); //power relays reset, train stops
digitalWrite(relayPin2, LOW);
delay(2000);
digitalWrite(relayPin3, LOW); //direction relays changed
digitalWrite(relayPin4, LOW);
delay(2000);
digitalWrite(relayPin1, HIGH); //power relays set train moves in reverse
digitalWrite(relayPin2, HIGH);
delay(10000);
digitalWrite(relayPin1, LOW); //power relays reset train stops
digitalWrite(relayPin2, LOW);

}

I am using Light Dependent Resistors (LDRs) set between the sleepers and facing upwards to detect the presence of my trains. When the train blocks the light from the LDR the Arduino can detect it.

The LDR needs to be wired in series with another resistor (try 100k) with 5v connected to one end, GND connected to the other end and an Arduino digital I/O pin connected to the join between the LDR and the resistor.

I connect my LDR to GND and the resistor to 5v and the Arduino detects HIGH when the LDR is covered.

...R

Thanks that's not a bad idea,
I was just trying to use some left over stuff from an arduino kit, I had a handful of the HC-SR04 sensors left over from a project so I thought it would be nice to use those up. But, LDRs might be a good alternative

The HC-SR04 modules are bulky and require more CPU steps than a simple digitalRead(). However if you can get them to report a different distance depending on whether the train is present or absent they should be fine.

...R

Have you considered using hall effect switches like these A1104 ?

You could mount one at each end of the track in the P-Way and glue a rare earth magnet on the underside of the loco - Ah, dammit..only problem I've just thought of.....you'd need a magnet under every piece of rolling stock as you maybe could have a different wagon/loco reach the track end first depending on your train make-up.

Still, magnets are cheap enough and its easy to code to detect the switch activation....... :kissing:

STDummy:
Ah, dammit..only problem I've just thought of.....you'd need a magnet under every piece of rolling stock

That's the attraction (for me anyway) of LDRs. You need no additions to the trains.

Of course if you want to run your model railway as a night-time scene LDRs won't work unless it is convenient to have a street-lamp close to every LDR,

...R

Of course, there's the really basic, almost brute-force method of a micro-switch disguised as a piece of track furniture in the 4 foot. Cheap and easy?

My buddy has a small layout for his N scale trains. 3x4'. He has a gazillion moving trains on it, with many of them being point to point. Like Robin2 mentions, we use LDRs.
Great results, except:

  • When turning down some of the room lights to show off the lighted buildings and cars, many of the lines fail because they erroneously always detect the engine even when it is not there
  • Passing trains on nearby tracks often trigger the LDR

We fixed some if his issues with a well placed street lamp on the layout. Some places we could not justify the street lamp so those runs just dont work with the room lights out.

I think that if I were to do it over, I would use something like the TCRT5000. A bit more to bury under the track, but with a source that is independent of room lighting.

Well ours are 00 gauge so they should be a big enough target for a sensor and will definitely block out an LDR. I know the sensors are on the bulky side but I was thinking I could hide them in a terminus platform so the train was moving straight towards it. I might have to order a couple of LDRs, I've been attempting to write the two ultrasonic sensors into it but i'm struggling to get it working. I might be better off with a simpler code and just using LDRs as switches... just a bit annoying I can't get it working to test it now.

vinceherman:
I think that if I were to do it over, I would use something like the TCRT5000. A bit more to bury under the track, but with a source that is independent of room lighting.

I don't want to have to bury anything under my track and I only need two wires to the LDR - GND and signal. With the reflective sensors you also need a power wire.

But I recognize that I can't do night time scenes.

...R

“But I recognize that I can't do night time scenes.”

Can you do a test to see how the LDRs react to an IR LED?

larryd:
Can you do a test to see how the LDRs react to an IR LED?

That's been on my TODO list for a while - other priorities with the project :slight_smile:

...R

Robin2:
That's been on my TODO list for a while - other priorities with the project :slight_smile:

...R

Would that be the project that we haven't seen any pictures of? :slightly_frowning_face:

Thanks for the suggestions guys :slight_smile:
just wanted to let you all know I finally figured out a pretty decent way to programme the HC-SR04 sensors for what I needed.
Pretty much just modified one of the hundreds of codes for the little obstruction sensor, motor reversing car/ robots people like to build with them.... I think I might have been overthinking how complicated it needed to be more than anything.

wildbill:
Would that be the project that we haven't seen any pictures of? :slightly_frowning_face:

In general, yes. It is a long way from the picture stage.

However it incorporates this (nRF24 + Attiny 84 + Rohm 6212 h-bridge)

20190120_152606.jpg

20190120_153833.jpg

Track Plan (the yellow dots are LDRs)

...R