Ultrasonic and Infra-red detection of train direction

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
I’ve been testing these Ultrasonic Ranging modules - a HC-SR04 device which sends a short pulse of ultrasonic (40kHz) sound and waits for an echo to return. The echo time is a measure of distance to the ‘target’. The ultimate aim is to detect a train’s direction and use this to control the signals on a single line section. One module would be needed at each end of a single line.
IMG_7503.JPG


I bought mine from Jaycar (catalogue#XC4442 $7.95 because I wanted it quickly) but also available cheaper from Hobbyking: https://hobbyking.com/en_us/ultrasonic-module-hc-sr04-kingduino.html

and on ebay. for example: https://www.ebay.com.au/i/232419741570?chn=ps

It needs a 5V supply takes 15mA sensor angle 15 deg.
Apply a 10us 5V trigger to TRIG pin, then wait for ECHO pin to go high.
Distance to object in cm is = time in us/58

You have to provide a 10us Trigger pulse to start the 40kHz pulse and the ECHO pin goes high (+5V) when an echo is received. I use a picaxe 08M2 to send the trigger pulse and to measure the time till the Echo pin goes high. For the distances we are dealing with, the echo times are around 1-4ms.

The module has a detection angle of +/- 15 degrees vertically and horizontally. (seems less on my measurements). In the program I can define a max and min detection distance to be considered, so as long as there is no fixed object in the detection ‘cone, except a train, I should be able to detect it.
(at say 700mm from the unit, the ‘cone is +/- 70mm roughly, so scarcely wider than a train.)

My initial approach was to point the module along the track and then measure the distance to a train as it approached or departed. Repeating this every 100 ms say, and subtracting the two values, should give a ‘speed’ that is either positive or negative. Unfortunately this did not work. While pointing the module at a solid object like a wall, gave a steady distance reading, it seems a small moving target like a loco gave a ‘jittery’ set of readings. For a slow moving train, these jitters could be larger than the change in distance I’m trying to measure, so the train could seem to approach and then depart on consecutive scans. Scratch that idea!

So I tried this second approach with more success. Still pointing along the track, I defined in the program, two zones called ‘near’ and ‘far’ for want of any better name. So in my test case ‘near’ extends from 150mm to 350mm and ‘far’ from 350 to 550mm. The zones have to be long enough to take 2 measurements of the fastest train while it is in the zone.
Hopefully the video explains the arrangement better.

A bit of simple logic in the picaxe, then determines if a train has gone from the ‘near’ to ‘far’ zone over consecutive scans (=departing) or from ‘far’ to ‘near’ zone (=approaching).

The simple program illustrated in the video here, just lights a red LED when a train approaches and a green LED if it is traveling away from the detector. The LED colours don’t show up well in the video.


I’m not sure if this will ever be practical as the module has all the electronics on a bare board.
IMG_7505.JPG
How I could mount it outside, without rain getting on it, and without blocking the US signal, will be a challenge. But it has been an interesting, if frustrating, project. (Now to try infra-red detectors).
 
Last edited:
  • Like
Reactions: 1 user

GAP

G Scale Trains, HO Trains, 1:1 Sugar Cane trains
14 Jun 2011
4,061
944
Bundaberg Queensland, Australia
Country
Australia
ringbalin-light-railway.blogspot.com
Best answers
0
Country flag
I’ve been testing these Ultrasonic Ranging modules - a HC-SR04 device which sends a short pulse of ultrasonic (40kHz) sound and waits for an echo to return. The echo time is a measure of distance to the ‘target’. The ultimate aim is to detect a train’s direction and use this to control the signals on a single line section. One module would be needed at each end of a single line.
View attachment 231389


I bought mine from Jaycar (catalogue#XC4442 $7.95 because I wanted it quickly) but also available cheaper from Hobbyking: https://hobbyking.com/en_us/ultrasonic-module-hc-sr04-kingduino.html

and on ebay. for example: https://www.ebay.com.au/i/232419741570?chn=ps

It needs a 5V supply takes 15mA sensor angle 15 deg.
Apply a 10us 5V trigger to TRIG pin, then wait for ECHO pin to go high.
Distance to object in cm is = time in us/58

You have to provide a 10us Trigger pulse to start the 40kHz pulse and the ECHO pin goes high (+5V) when an echo is received. I use a picaxe 08M2 to send the trigger pulse and to measure the time till the Echo pin goes high. For the distances we are dealing with, the echo times are around 1-4ms.

The module has a detection angle of +/- 15 degrees vertically and horizontally. (seems less on my measurements). In the program I can define a max and min detection distance to be considered, so as long as there is no fixed object in the detection ‘cone, except a train, I should be able to detect it.
(at say 700mm from the unit, the ‘cone is +/- 70mm roughly, so scarcely wider than a train.)

My initial approach was to point the module along the track and then measure the distance to a train as it approached or departed. Repeating this every 100 ms say, and subtracting the two values, should give a ‘speed’ that is either positive or negative. Unfortunately this did not work. While pointing the module at a solid object like a wall, gave a steady distance reading, it seems a small moving target like a loco gave a ‘jittery’ set of readings. For a slow moving train, these jitters could be larger than the change in distance I’m trying to measure, so the train could seem to approach and then depart on consecutive scans. Scratch that idea!

So I tried this second approach with more success. Still pointing along the track, I defined in the program, two zones called ‘near’ and ‘far’ for want of any better name. So in my test case ‘near’ extends from 150mm to 350mm and ‘far’ from 350 to 550mm. The zones have to be long enough to take 2 measurements of the fastest train while it is in the zone.
Hopefully the video explains the arrangement better.

A bit of simple logic in the picaxe, then determines if a train has gone from the ‘near’ to ‘far’ zone over consecutive scans (=departing) or from ‘far’ to ‘near’ zone (=approaching).

The simple program illustrated in the video here, just lights a red LED when a train approaches and a green LED if it is traveling away from the detector. The LED colours don’t show up well in the video.


I’m not sure if this will ever be practical as the module has all the electronics on a bare board.
View attachment 231390
How I could mount it outside, without rain getting on it, and without blocking the US signal, will be a challenge. But it has been an interesting, if frustrating, project. (Now to try infra-red detectors).

Maybe put the US detectors in tubes (similar to the covers on traffic lights and use a conformal coating to keep the electronics dry.
Would IR be as effective in the summer heat?
Where I worked in a previous life we used an IR link for remote cameras and when the temp rose the signal degradation was directly proportional.
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Maybe put the US detectors in tubes (similar to the covers on traffic lights and use a conformal coating to keep the electronics dry.
Would IR be as effective in the summer heat?
Where I worked in a previous life we used an IR link for remote cameras and when the temp rose the signal degradation was directly proportional.
Tubes are a good idea. I'll keep in mind. They'd have to be a slightly larger dia than the cylindrical US detectors

I have had IR working outdoors for years - will keep that discussion to a new topic in a few weeks.
 

PhilP

G Scale, 7/8th's, Electronics
5 Jun 2013
33,588
3,524
Nottingham
Best answers
0
Country flag
Interesting... wonder if there is a low-end Doppler unit that might get round the 'jitters'??

Hmm.. Something else to keep me away from year-end accounts! ;)
 

dunnyrail

DOGS, Garden Railways, Steam Trains, Jive Dancing,
Staff member
GSC Moderator
25 Oct 2009
26,202
4,996
75
St.Neots Cambridgeshire UK
Best answers
0
Country flag
Blub blub blub and oh what? Far to tech for me but you appear to have cracked a tricky problem.

Thinking about this in a practical signallers way I think you will need this to operate a Relay that would set Signals at the opposite end to Red when the section has been entered then releases the Relay when the section has been vacated thus allowing a further Train to enter the section from whatever end! Tricky bit will be how you decide which end though I think your Red and Green Led’s May show the way.

Have you tried a second Loco following the first one to see what happens?
Then another one the opposite way?

Because that is what is likely to happen in real life. This is the System tester in me kicking into gear!
 

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
10,702
1,244
Cheshire
www.riksrailway.blogspot.com
Best answers
0
Country flag
Brilliant and simple enough even for me to understand. I like the way it can detect trains reversing before exiting the system.

Rik
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Blub blub blub and oh what? Far to tech for me but you appear to have cracked a tricky problem.

Thinking about this in a practical signallers way I think you will need this to operate a Relay that would set Signals at the opposite end to Red when the section has been entered then releases the Relay when the section has been vacated thus allowing a further Train to enter the section from whatever end! Tricky bit will be how you decide which end though I think your Red and Green Led’s May show the way.

Have you tried a second Loco following the first one to see what happens?
Then another one the opposite way?

Because that is what is likely to happen in real life. This is the System tester in me kicking into gear!
I didn't expect too many people to follow or be interested, so thanks for the comments.
Yes I have tested it with trains following one another and it's OK.

I must start thinking about the single line logic. Since I'd use a picaxe at each end for the detectors, I would use it for the signals too.
I think I need TWO 'train counters' - one for each direction. This will allow trains to follow one another. Obviously, if both counters are non-zero, we have a head-on collision imminent! And if both are zero, signals at both ends can be green.
The problem I'm still wrestling with is the communications between each end. How many 'messages' have to go from each end to the other - I think 2 is the minimum. I'd like to use wireless somehow.
Has anyone had experience using Bluetooth between two ends, not between say a mobile phone and a 'receiver'? Seems it should be possible for 2-way communication.
 
  • Like
Reactions: 1 user

John Russo

Registered
24 Sep 2015
197
21
66
PA
Best answers
0
Country flag
Check out this little set up:

https://www.adafruit.com/product/3316

Could be mounted at a horizontal or own angle so as to provide water detection.

I am about to start fiddling with Raspberry Pi /Python and Pi zero to do what you are doing.

Two of these spaced 2-3 inches apart combined with some logic shoud be able to detect any scenario of movement.

Another set at the opposite end of the section should give you what you need for occupancy, entrance, exit.

I think two sets of two Hall Effect sensors could provide that also. Same as using reeds but more reliable.
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Check out this little set up:

https://www.adafruit.com/product/3316
I am about to start fiddling with Raspberry Pi /Python and Pi zero to do what you are doing.
I think two sets of two Hall Effect sensors could provide that also. Same as using reeds but more reliable.
Interesting little unit. My mind boggles at the ability to use light to measure 5mm. That's a distance of 10mm round trip, so would take 30 PICO seconds for the round trip. How do they measure that short a time?
Keep us updated on your fiddling with your project.
I'm still experimenting with reflective infra-red detectors, but am having trouble detecting flat wagons which have very little reflective surface. I may be forced back to across-the-track method.
 

John Russo

Registered
24 Sep 2015
197
21
66
PA
Best answers
0
Country flag
My mind boggles at the ability to use light to measure 5mm. That's a distance of 10mm round trip, so would take 30 PICO seconds for the round trip. How do they measure that short a time?

I did not think about that until you mentioned it. When I was closer to this stuff we were dealing in the micro second world. I recall the notion that one second is an eternity inside a computer.

I found this link that suggest they have it down to 1 pico second clock intervals

The clock on the Pi is 1.2 GHz which puts it the micro category as far as timing.

I too am boggled at a pico speed clock.

Pico is 1/1000 or a Nano and a Nano is 1/1000 of a Micro.

Amazing.
 

John Russo

Registered
24 Sep 2015
197
21
66
PA
Best answers
0
Country flag
I'm still experimenting with reflective infra-red detectors, but am having trouble detecting flat wagons which have very little reflective surface. I may be forced back to across-the-track method.

Have you considered using image recognition?

The camera samples at 60 frames per second. Fairly rudimentary image processing would detect direction, motion, speed, and presence. The outdoor issue would be mitigated.

You would have to read up on OpenCV. https://opencv.org/about.html


A little How-To.

 
Last edited:

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Sort of interesting but probably a bit of overkill for what I want to do. Some people get a kick out of using top tech (and usually top $). I like to KISS. I've learned Picaxe and don't think I'm up to learning raspberry Pi.
 

GAP

G Scale Trains, HO Trains, 1:1 Sugar Cane trains
14 Jun 2011
4,061
944
Bundaberg Queensland, Australia
Country
Australia
ringbalin-light-railway.blogspot.com
Best answers
0
Country flag
Sort of interesting but probably a bit of overkill for what I want to do. Some people get a kick out of using top tech (and usually top $). I like to KISS. I've learned Picaxe and don't think I'm up to learning raspberry Pi.

Come on Greg a whizzkid like yourself would master that over a weekend. :) :)

I am toying with Raspberry Pi for some signaling on the new layout and I have been looking at a neighbor's setup where he is using one to control some of his Ham Radio gear, this guy has more antennas on his house than NASA have and is a radio nut.

On another note with detecting direction, in a past life I worked on Pokies and they had a feature called "coin in yoyo" to stop people putting strings on coins and pulling them back out to use again. The feature works with 3 beams that the processor must see cut in a particular order for the coin to be legit.
Just a random thought that came to me at 3.30am this morning while I was driving back from Qld.
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Well after a month of frustration, I give up. I don't think the ultrasonic detectors will be any use for detecting train direction. It's amazing how some initial good test results can be overturned when you start intensive testing.

Just to jog your memory, I have the US detector 'looking along' the track – reflections from the front of the train are detected as it approaches, or the back of the train as it leaves. I started with a single small loco and all seemed great. Then I added a low sided open wagon and the troubles began. When the train approached the detector, with the loco leading, no problems – it reflects from the larger front area OK. But if a train departed the last wagon has to provide the reflective surface and it didn't work reliably. By this I mean it would get the jitters – it might detect it and it might not on successive scans. Using a flat wagon showed it was impossible to pick up wagons with small profiles.

To try and salvage some use for them, I tried using them pointed across the track so a train side was the reflective surface. This works quite well, even for flat wagons, if it's at 90 degrees across the track. But it does pick up the gaps between wagons, which is not desirable.

So I tried at a slant angle across the track to overcome that problem. This doesn't work well – the US beam is then reflected off the wagon or loco side or end, and away from the detector.

across track diag.jpg

Oh well, chalk it up to experience!
But I have had success in writing the program to control a single line signals – I just need reliable detectors.
 

trammayo

Interested in vintage commercial vehicle, trams, t
24 Oct 2009
22,675
4,685
75
Co. Mayo
Country
Ireland
Best answers
0
Country flag
Well after a month of frustration, I give up. I don't think the ultrasonic detectors will be any use for detecting train direction. It's amazing how some initial good test results can be overturned when you start intensive testing.

Just to jog your memory, I have the US detector 'looking along' the track – reflections from the front of the train are detected as it approaches, or the back of the train as it leaves. I started with a single small loco and all seemed great. Then I added a low sided open wagon and the troubles began. When the train approached the detector, with the loco leading, no problems – it reflects from the larger front area OK. But if a train departed the last wagon has to provide the reflective surface and it didn't work reliably. By this I mean it would get the jitters – it might detect it and it might not on successive scans. Using a flat wagon showed it was impossible to pick up wagons with small profiles.

To try and salvage some use for them, I tried using them pointed across the track so a train side was the reflective surface. This works quite well, even for flat wagons, if it's at 90 degrees across the track. But it does pick up the gaps between wagons, which is not desirable.

So I tried at a slant angle across the track to overcome that problem. This doesn't work well – the US beam is then reflected off the wagon or loco side or end, and away from the detector.

View attachment 232501

Oh well, chalk it up to experience!
But I have had success in writing the program to control a single line signals – I just need reliable detectors.

May I offer a suggestion? Could you insert a timer in the equation. When I was involved in running a cable tramway, we were instructed by the Railway Inspectorate (after an incident) to find a means of detecting the speed of the tram and applying, automatically, the winding drum brakes if the tram was entering the platform/s too fast.

We used Infrared beams (two of them a measured distance apart) - when the tram passed the first beam it set off a small timer. If the tram was going too fast, it wouldn't allow the timer to time out and, via a relay, applied the air brake - thus preventing a collision with the end/s of the platform. I say in the plural because there are two tracks and two trams.

And that worked absolutely fine. We did have problems with sunlight effecting the beams so I substituted induction loops instead.

I'm not saying that the whole process would work for you but, if the longest train was timed, then a small leeway of a couple of seconds added, then maybe the power to the "lead in" tracks could be cut until the timer has run it's course and the train cleared the single track section.

Of couse, if battery operated locos (or steam) were run then all it could do is just operate signalling.

Just a thought.
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
May I offer a suggestion? Could you insert a timer in the equation. When I was involved in running a cable tramway, we were instructed by the Railway Inspectorate (after an incident) to find a means of detecting the speed of the tram and applying, automatically, the winding drum brakes if the tram was entering the platform/s too fast.
We used Infrared beams (two of them a measured distance apart) - when the tram passed the first beam it set off a small timer. If the tram was going too fast, it wouldn't allow the timer to time out and, via a relay, applied the air brake - thus preventing a collision with the end/s of the platform. I say in the plural because there are two tracks and two trams.
And that worked absolutely fine. We did have problems with sunlight effecting the beams so I substituted induction loops instead.
I'm not saying that the whole process would work for you but, if the longest train was timed, then a small leeway of a couple of seconds added, then maybe the power to the "lead in" tracks could be cut until the timer has run it's course and the train cleared the single track section.
Of course, if battery operated locos (or steam) were run then all it could do is just operate signalling.
.
Thanks for your ideas Mick. Your comments are more applicable to 'across-the-track' detection. My work above has been on trying to determine if a train is approaching or leaving the one detector.
I have concurrently done lots of work on infrared similar to your idea of breaking 2 beams a known distance apart and will soon post on that. But the need to cater for long and short and fast and slow trains and even a train stopped over a detector, really makes it more complicated than your problem above.
I have used across-the-track IR detectors outside for years to just control turnouts on reverse loops, so I know I can get them to work. I am hoping to get a reflective system to work outside, where both the transmitting LED and the receiver are on the same side of the track and rely on the IR beam reflecting off the train.
more to follow.........
 
  • Like
Reactions: 1 user

ge_rik

British narrow gauge (esp. Southwold and W&LLR)
24 Oct 2009
10,702
1,244
Cheshire
www.riksrailway.blogspot.com
Best answers
0
Country flag
Ah, the frustrations of breaking new ground! I really admire your persistence, Greg. I know you've been working on this for several weks now, testing, tweaking and re-testing. What I really respect is the way you always try to keep costs to a minimum when developing the ideas. How much do you think the finished unit(s) will work out at (not including development and construction time of course)?

Rik
 

John Russo

Registered
24 Sep 2015
197
21
66
PA
Best answers
0
Country flag
If you still want to sue the ultrasound, could you mount them from an overhead gantry. I say them, because I agree with the previous poster suggesting to use two sensors, spaced at a known distance.

In that regard, could you scrap the US and put two Hall Effect sensors in the track spaced at a known distance. Same math as using Reeds, just that Hall Effects are solid state and less expensive.

https://www.amazon.com/A3144-A3144E...UTF8&qid=1517148693&sr=8-1&keywords=us5881lua

https://www.adafruit.com/product/158

You might need to put three sensors, placed at known distances, in order to achieve the logic to detect the entrance and exit scenarios you described.
 

gregh

electronics, computers and scratchbuilding
1 Nov 2009
3,137
263
Sydney, Australia
www.members.optusnet.com.au
Best answers
0
Country flag
Ah, the frustrations of breaking new ground! I really admire your persistence, Greg. I know you've been working on this for several weks now, testing, tweaking and re-testing. What I really respect is the way you always try to keep costs to a minimum when developing the ideas. How much do you think the finished unit(s) will work out at (not including development and construction time of course)?

Rik
Thanks Rik. This project has really become just a 'goal' with probably no use. I doubt I'll ever use it , but during a hot summer it's easier to sit inside in front of a fan doing this stuff - just to exercise the brain.
At a real guess final costs might be $75 for all the components for one section (ie 2 pairs of IR detectors and 2 picaxe)