gregh
electronics, computers and scratchbuilding

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.

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.

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).

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.

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: