Position sensing

roho

Member
...of a model train.

Hi folks,

Does anyone have any practical experience of locating trains automatically on a model railway? The purpose would be automating the running of the railway, such as the routing of trains, signalling, etc. The first requirement is that the hardware needs to be kept out of sight under the baseboard. This rules out e.g. the train breaking a beam as a potential solution. Secondly, some dozens of these position sensors will be needed, so the hardware will need to be simple and cheap.

A couple of ideas so far. Firstly, an IR LED and its matched photodiode. A series of pairs of holes are drilled through the baseboard such that each pair is side by side under the track. An LED goes in one of the holes and the sensor in the other. These are mounted on a PCB (for mechanical support and electrical interface) which is screwed into place on the underside of the baseboard. My thinking is that a train (with some reflective painting on its underside?) running over the top of this can be detected. However, preliminary testing (without any baseboard) suggests that ambient light may be a problem, which would mean analogue detection of the presence of a train would be needed. The interface chip is a 28X2, and this must support up to 9 of these sensors, and running 9 ADCs at the same time as well as signalling and communicating with the outside world sounds like too much, assuming it's even possible in the first place. I'd much rather have digital outputs from the sensors.

This led to the second idea of gluing a small magnet to the underside of the locomotive and using a Hall effect sensor under the tracks. This is new territory for me though, and I'd welcome any feedback from those with any experience.
 

hippy

Technical Support
Staff member
Reflective IR and magnetic sensors are possibilities. Another option is using Light Dependent Resistors (LDR) to create a break-beam from ambient light levels being pursued here -

https://picaxeforum.co.uk/threads/calibrating-ldr-using-a-separate-ambient-ldr.31720

Using digital sensors is probably easier than using analogue as there's less need to scan anything or compensate. Digital can be 'straight in' signals; if an input is activated than the corresponding sensor has been. One can then just idle while waiting for such a signal.

Handling multiple sensors can be done by multiplexing. Having one PICAXE repeatedly scan the array of sensors and report which ones have activated to a second PICAXE which actually deals with such activations. How many sensors a single PICAXE can handle would depend on the type of sensor and how long activations last for as a train passes.

Another alternative is to use one PICAXE per set of sensors. Most allow some kind of latched detection for a number of sensors so it shouldn't matter how brief an activation is. I believe the 18M2 may be best for that, supporting 8 such inputs. There may be other non-PICAXE chips which can do such latched detection for more sensors which can be easily interfaced to a PICAXE.

So lots of options, all of which seem feasible.
 

bgrabowski

Senior Member
Reflective IR and magnetic sensors are possibilities. Another option is using Light Dependent Resistors (LDR) to create a break-beam from ambient light levels being pursued here -

https://picaxeforum.co.uk/threads/calibrating-ldr-using-a-separate-ambient-ldr.31720

Using digital sensors is probably easier than using analogue as there's less need to scan anything or compensate. Digital can be 'straight in' signals; if an input is activated than the corresponding sensor has been. One can then just idle while waiting for such a signal.

Handling multiple sensors can be done by multiplexing. Having one PICAXE repeatedly scan the array of sensors and report which ones have activated to a second PICAXE which actually deals with such activations. How many sensors a single PICAXE can handle would depend on the type of sensor and how long activations last for as a train passes.

Another alternative is to use one PICAXE per set of sensors. Most allow some kind of latched detection for a number of sensors so it shouldn't matter how brief an activation is. I believe the 18M2 may be best for that, supporting 8 such inputs. There may be other non-PICAXE chips which can do such latched detection for more sensors which can be easily interfaced to a PICAXE.

So lots of options, all of which seem feasible.
A digital reflective sensor to consider for the baseboard idea is the Hamamatsu S6986 from active-robots.com and other suppliers. This has an output which modulates the light from any red or infra red LED and is very resistant to false triggering by ambient lighting. They are quite expensive at £5.45 each but in my experience they are the most effective short-range digital proximity sensors. Using visible red LEDs with these sensors reduces the range a little but gives you the advantage of being able to see where the light beam is directed.
 

erco

Senior Member
Plenty of relevant input in the "other" train thread Hippy linked to. Chances are, your train layout is in a fixed location and you control the lighting, which IMO makes an ideal case for using photocells/LDRs. They are cheap, plentiful, and super easy to use with a Picaxe. The main thing is to get your hands on some and start experimenting. The OP of the other thread hasn't even started yet, with any effort you can beat him at his own game.
 

Hemi345

Senior Member
This led to the second idea of gluing a small magnet to the underside of the locomotive and using a Hall effect sensor under the tracks. This is new territory for me though, and I'd welcome any feedback from those with any experience.
I like that idea. Takes light interference out of the equation. Two hall effect sensors spaced apart could be used to determine the direction the train is moving. A magnet on the first and last locomotive/car on the track could help time things too.

Do the motors in the locomotives produce their own magnetic field that could be detected by the sensor? Then again, if a locomotive is pushing cars down the track, then you'd need a magnet on the front car.
 

hippy

Technical Support
Staff member
The disadvantage I can see for hall effect sensors is that any trigger may be short lived, easy to miss. That might be mitigated if the trains or engines do emit any magnetic fields, by using magnetic strips, or even by pulse-stretching the signals.

Another 'no sensor cost' options might be to isolate a very short section of rail, use that as a sensor for when the wheel and axle short it to the other rail.
 

erco

Senior Member
Or simply use ultrasonic sensors looking sideways to detect trains.

BTW I have used the Hamamatsu S6986 as bgrabowski suggested. A very nice sensor to sense a reflector, see


See also Waveshare's sensor:


BTW, full props to Philo, from whom I learned everything I know about the S6986 at http://www.philohome.com/sensors/lasersensor.htm
 
Last edited:

depeet

New Member
When I was a teenager (around the eighties), I divided the tracks in block with plastic rail connectors as was described in a book by Elektor. Each block had a sensingcircuit that signaled a central unit. In those days, it took a lot of TTL IC's and finally a communication with my Commodore. If I find that book again, I could post some circuits. Of course, all those TTL circuit could be replaced by a Picaxe. Question is which system you use Marklin or something else?
 
Last edited:

mortifyu

New Member
Recently and currently I am doing a lot of research and development using PICAXE TOUCH16. This may be a very good option for what you are looking to achieve. Thus far with the help of discussions here on this forum and further research I have found some limitations for my intended application. Supply rail voltage variations can cause issue and also temperature can cause drift of sensed TOUCH16 values. I am still working on finding myself a solution, however for your application I’d be confident you could solidly utilise this area of the PICAXE architecture. With a bit of clever programming you could repeatedly refresh the base (no train detected) TOUCH16 value and when a train does pass over the TOUCH ‘sensor plate’ it will make for a dramatic change from the base value in turn providing clear detection of a train. This now eliminates any concern of ambient light.

If IR was your preferred method, then you could easily make the IR emitter pulse at a particular frequency (perhaps 10kHz or more) and have your IR receivers counting. This way when the correct frequency of pulsing IR was detected you would then know it was a definite reflection from the train rather than ambient light or some other source.


Regards,
Mort.
 

mortifyu

New Member
Another TOUCH detection device I have come across recently is the MTCH10x series of sensors also manufactured my Microchip. These sensors do provide a HIGH to LOW output that could also be helpful with your application.

If you were to use MTCH101, best you get out your magnifying glass and ultra fine pitch soldering tips. Doable, but takes experience to hand solder such small packages.


Regards,
Mort.
 

Hemi345

Senior Member
The disadvantage I can see for hall effect sensors is that any trigger may be short lived, easy to miss.
Ahh, yes. Pulse stretch or better yet, use an SR Latch IC so the host can't miss it no matter how quick it is. Although with an SRLatch IC (such as the CD4043B/CD4044B), you'd need to use an additional pin for each detection point to reset it, but that's easily accomplished using an I2C port expander for both detection and resetting. Even a small 08M2 could be used for many detection points.

Some additional thought is some I2C port expanders (pX) have an interrupt pin. The 08M2 could just monitor the interrupt pin on the pX in a tight loop. When that pin goes low, query the pX to see which detection point on the track was triggered.
 
Last edited:

erco

Senior Member
Or go old school on that Hall-effect pulse stretcher. Trigger a 555 one-shot with the sensor to stay active for a second or two, plenty long enough to detect. Many people have never used the venerable 555, one the very first commercially available/affordable ICs that hobbyists could get their hand on... way back in 1971!https://en.wikipedia.org/wiki/555_timer_IC

Seriously, I think it's helpful to get your hands dirty and solder some old analog hardware together occasionally. Keeps us humble and appreciative of how easy we havw it these days. Plus, it's jolly good fun!

Related, I just had another booth at Maker Faire and same as last time, the little $5 line followers using an LM393 comparator stole the show. People are baffled how they work so well with only a dozen components in plain sight.

 

erco

Senior Member
Best prices I found on eBay UK:

D2-1: https://www.ebay.co.uk/itm/283356990321

D2-5: https://www.ebay.co.uk/itm/174035105605

The D2-1 is easier to build since it uses premade motor modules. The D2-5 uses worm gears and requires mechanical assembly with spacers in odd places. That's an important difference if you are building these with kids and want a trouble-free first DIY experience.


I've done some hacking on these. Here's a minimal 555 timer version:

Sorry for the hijack, roho. Let's get back OT!
 
Top