Model Train detection using Arduino & IR

Planning on using an Arduino and sensors to detect presence of model trains.

Is there an advantage in using beam sensors (e.g. IR Break Beam Sensor - 3mm LEDs | Adafruit ADA2167 | Core Electronics Australia ) vs proximity sensors? Or vice-versa?

One of the possible deployments is outdoors and needs to be able to run in daylight & night & be weatherproof. Does this change things?

I have also considered using photoresistors. This would work for indoors layout with consistent lighting. Any comments on photoresistors vs IR sensors?

In some places, I want to use multiple sensors to cover a larger section of track. Having any sensor trigger will be enough to indicate “occupied”. Is there an easy way to wire multiple sensors to a single input, or am I better to use 1 input per sensor and the OR them in software?

Given that I want to deploy anywhere up to 50 of these things on a layout (monitored by multiple Arduino units), are there any tricks to easily calibrate them? If each sensor needs calibration, what are my options to allow them to be calibrated in-place without interacting with the Arduino code?

Thanks

Hi Andrew,

Not really, they are basically the same under the hood (phototransistor and LED pair), just arranged differently - Reflectance sensors have them both pointed the same way, so that the light bounces off an object, break-beam sensors have them arranged across a gap to sense an object there. Sometimes they’ll have modulation, where the emitter and receiver have circuitry to filter out everything but light pulsed at a certain frequency, but they are pretty rare in the maker space.

This might be a bit tricky, first thing that comes to mind is a 3D printed hood around the sensors to reduce stray light and moisture ingress

If you’re clever, you can grab sensors that are “open drain” in that they don’t drive their output high when they detect something, they just do/don’t conduct to GND. This means sensors won’t contest a pin, they’ll effectively be ORed in hardware. A pullup resistor will pull the pin high when none of them detect. ORing in software is nice and easy, but 50 sensors will have you running out of pins on all but the biggest microcontroller boards.

Some of these have adjustment potentiometers, so I’d look for that:

Lemme know if this sparks more discussion!

Hi James. Thanks for your input.

I’m very experienced with software (although not Arduino), but my hardware experience is limited to very simple circuits, so I’m feeling my way here.

I have an initial bias towards a “break-beam” solution. My rationale is that a break-beam solution is less requiring of calibration. Especially in the outdoors environment, with varying ambient light levels and other complications. It feels like a beam solution can easily remove most of the “noise” by using something like a hood or short PVC tube to localise the input. In contrast, a reflection-based sensor needs to be much more concerned about ambient IR and also whether random objects not in the actual detection zone can also trigger the sensor.

Indoor railroads often place the sensors facing “up” between the rails, using either IR reflection or light sensors. Light sensors are effectively break-beam sensors using the room’s ambient light as the beam source. IR reflection is also less “noisy” indoors, as you can assume a relatively fixed light level.

One concern with break-beams is that for dual-track (or multi-track) arrangements I would need to ensure that the beam covers only one track. This would mean placing nodes between (rather than just outside) the rails. Or I could put the receivers in an overhead gantry and have the emitters shine straight up.

Using reflective IR proximity detection, could I place them close beside a rail and have them detect objects up to 2-3cm away but not 5-6cm away?

The rationale for all of this is to automatically drive signals and protect “blocks” of track from multiple trains. For my design, each block controller needs about 5 each inputs + outputs for block control plus about the same to drive a signal to depict the state of the block. Thus the need for train detection (to know if a block is occupied) and multiple IO ports per signal / block. I can do the software easily enough; I need help getting the signals into the Arduino.

In my design, each signal / block is mostly independent, albeit with some outputs from one signal driving inputs to the previous signal. I’m thinking that each Arduino could manage up to (say) 4 signals / blocks. The number of sensors is greater than the number of blocks, because I plan to try to detect trains anywhere in the block, not just track ingress and exit.

Using some sort of IR / light based technology appeals to me because it can be added to an existing system without needing to interact with the existing track & track power in any way.

1 Like

Hi Andrew

Did you look at any existing solutions such at this shield https://www.azatrax.com/arduino-shield-ir-detector.html ?

Cheers, Steve

Hi Andrew

Hmm, you’d have to do some testing to work out whether the signal-to-noise ratio was adequate, personally I’d pick up a couple of each type of sensor and play around with positioning to see what you can see :slight_smile:

Maybe use your existing LED/photodiode to emulate one of these sensors, and measure the analog feedback with an Arduino analog pin to see what sort of signal you get?

Hi Andrew.
Just to clarify a bit. Here is a circuit outlining James’ “wired OR” configuration. This only requires 1 Arduino (or similar) input per Block. This requires an active HIGH from a sensor in the presence of an object. For the “Wired OR” configuration the output MUST be “Active LOW” .


Sensor details have been omitted but requires an active HIGH output.

This circuit may be more convenient in terms of real estate and convenience. As drawn it is for the same “Active HIGH” configuration but if perchance the chosen sensor arrangement happens to be “Active LOW” for “Object present” output just reverse the connections to the inverting (-) and non inverting (+) comparator inputs. ie; sensor outputs to + and reference to - comparator inputs.


Note the LM339 is a quad comparator with open collector output.
In both cases for the wired OR configuration to work correctly the output MUST be active LOW which should make no difference to whichever processor or controller used.

DO NOT use or enable any other pull ups. There is 1 common pull up in both systems.
For the 2N7000 version I strongly recommend using 5V to ensure complete switch on of the Mosfet. The LM339 supposedly works down to a minimum of 2V so should be OK at 3.3V but I have not used them at that level so could not be sure (I personally try to stick with 5V if possible).
The down side is the LM339 has only 4 comparators but you might be able to get away with 4 sensors per block.
Cheers Bob

Correction: I think this should read “Wired NOR”

The comparator method has another feature. If the signal from the sensor is analog the comparator will convert this to digital by switching at the half rail voltage point as set by the reference resistors. This point can be changed by changing the resistor values.

2 Likes

Hi Andrew
I just thought of a simpler solution.
If you use sensors with an NPN open collector (or drain) output and arranged to be active LOW when object present (ground) there will be no need for any IC or transistor interface. Join all outputs together then to VCC via a common pull up resistor then to Arduino or whatever digital input (do NOT enable any internal pull ups).

Basically the same as the top circuit without the mosfets. Just connect the open collectors directly to the common pull up resistor. Do not use the series resistor shown. That is only a current limiter as the misfit gate is a short circuit to ground at switch on. Do not use the 10k resistor to ground either. In other words the sensor output directly to the pull up resistor.

Some of these sensors are actually switched (High or Low with nothing between) but for this to work they MUST be open collector or drain output. If perchance the sensors are analog it should not matter as the Arduino or whatever should take care of it when it gets to the switching threshold.
Cheers Bob

Hi Andrew
This sensor would would fit the above scenario. or something similar.

IR Break Beam Sensor. SKU: SEN 0503

The line in the specifications regarding output mode

“Output Mode: NPN normally open”

suggests open collector. This is how it is normally specified.

The outdoors bit might be a killer for this particular one and shrouds might have to be devised. But this is not the only device on the market and a more suitable device could be found. A browse of Element 14 or RS Components might be worthwhile.
Cheers Bob

1 Like

Thanks Bob. That’s really helpful. I’m thinking of grabbing a few of those and trying things out.

For outdoor use, I’d actually planned to mount the sensor pairs pointing down short tubes (think PVC conduit) to protect against miscellaneous light sources and also protect against weather / dirt / etc. Though a small square of plastic to function as an umbrella might be sufficient.

1 Like

Hi Andrew
Glad to be of assistance. Keeps the OLD grey matter active.
Short BLACK tube would be the go. Need to only be a bit larger than the LED/Phototransistor. I think some such sensors actually come with this sort of thing.
Cheers Bob
PS. A pretty good example where a bit of hardware interfacing can assist the usual more popular software solution. In this case greatly reducing the number of digital inputs required.

1 Like

Hi Andrew

Are you thinking of going with the add on shield I saw on the web or building the the hardware that Bob showed the circuit for ?

It seemed to me a lot easier to get it up and running using existing detector hardware that was already proven in use that making something from scratch.

Cheers

Steve

Hi Stephen

Those circuits are pretty well proven. Been around since transistors and particularly ICs were invented.

In all fairness in the simple circuits shown there are a couple of down sides.

In the Mosfet circuit, if the sensor output is a slow analog signal the Mosfet will be just an amplifier until saturation is approached. But the Arduino digital input will not switch until a threshold voltage is reached when it will switch and so be a digital type signal. I don’t see any problem there.

The comparator circuit which would be my favourite as it will accommodate both NPN and PNP sensor outputs with a minor circuit change (swapping the inverting and non inverting inputs). The down side would be some instability at the switching point with a slow input signal. If this proves problematic it is very easily overcome with the addition of 2 resistors providing positive feedback to the non inverting input thus providing a schmidt trigger action and removing any instability

Andrew has already indicated that there would be 5 (or 4) sensors in each of up to 10 blocks which is a lot of inputs to look after. As Arduino action would be required on the activation of any sensor on a block by block basis the above approach avoids using many inputs and surely avoids a lot of processing. Requiring only 1 input per block.

I think that by making up a little board with 4 (or 5) transistors and a few resistors or 1 14 pin IC with a few resistors would use very little real estate and the little board could be sited at each block just running 3 wires back to a central Arduino. Nothing really hard about that.

My last suggestion even gets rid of most interfacing and I don’t think could be simpler. Use NPN open collector sensors, switching types if possible but if not the Arduino input should take care of that. Connect all the open collector outputs together then to VCC via a common pull up resistor. All the sensors will be “Wired OR” or NOR to 1 Arduino digital input.

I don’t think I could dream up anything simpler, the only criteria is that all sensor/interface HAVE to be open collector (drain)
Cheers Bob

1 Like

C’mon it’s 2023. Aren’t model trains using radio time of flight beacon arrays to detect and control traffic patterns? :slight_smile:

Hi Aaron
Trying to keep it simple. Great believer in the KISS principle.
“Keep It Simple St…d”.

And Yes, it is 2023 and Andrew might like to have his system still working in 2024.
Cheers Bob