The HC-SR04 ultrasonic units are easy to program, requiring just a trigger pulse before timing the echo. But they can be difficult to hide in smaller models. Of course clever design can make one a feature – like Petoi’s Nybble:

Another rangefinder is the tiny VL53LOX

At only 25mm x 11mm this unit has a lot going for it and is almost as simple to program. It communicates via the i2c bus so has internal registers to address. For a single unit we start by setting up the link:
hi2csetup i2cmaster, 82, i2cfast, i2cbyte
Then to trigger a cycle we set a bit in register 0x00:
hi2cout 0x00,(0x01) ;trigger
Then the distance in mm will be ready in registers 0x1E, 0x1F :
hi2cin 0x1E,(b3,b2) ;read range in mm
To convert from the two input bytes to word value w3:
w3 = b3
w3 = w3 * 256
w3 = w3 | b2
And that’s how to program it!
Features and Issues
Life is never that simple of course but you can certainly get to see a screen of range numbers very quickly.
The ultrasonic sensors use a wide cone of sound so will not be too picky on size of target; they won’t care about light levels but can give a lot of false echoes.
This IR sensor might be disturbed by light levels but won’t give false readings due to target size or shape. However it has a very narrow cone of light and might just miss the target altogether if not accurately directed at it.
Like all complex electronics it hates RFI noise and can down tools in a certain situations. I have found a few filtering techniques but remember logic won’t help you. Best avoid noise.
Notice in the photo the orange tab protecting the chip apertures. Remove it if you don’t want unexplained variations of range!
Application
Wheeled vehicles require front and back sensors to prevent them running into things. If the sensors also provide distance information then this can be used for proportional deceleration. In this simple case only one sensor is in use at one time. So we can use the XSHUT pin to shutdown the other device.
I retrofitted two VL53LOX boards to a G-scale tram built 8 years ago, controlled by a Picaxe 14M2 to run at scale speed with on-board batteries:
RFI noise from the brushed motor knocked out one sensor and affected the other. I have ordered four more from China to see how they compare on this front. The recommended 220nF green capacitor has no impact, nor does earthed braiding. Three turns through a ferrite ring helped the most. Sifting errors and occasionally resetting the chips makes it workable.
The cone of ultrasound was a problem for another batch of trams fitted with radios and programmed to move around each other. The project worked fine on the bench but the trams would not move on the track. The sound reflected back from the sleepers - so the sensors had to be angled up at 45 degrees - severely impacting on their range and making them visible. For that write up see:
https://picaxeforum.co.uk/threads/model-trams-share-networks.27763/
The cone of IR light is much tighter and the beam crosses the sleepers without issue. However, the buffer stops may not be seen as the sensor gets closer because they are set at different heights. Surprisingly, fitting a piece of transparent plastic between the cross piece and the rails solves this problem.

The tiny sensor is mounted just above the safety fender and would disappear if painted black.
Addressing More Sensors
The VL53LOX has a soft i2c address at register 0x8A. It will initialise with address 0x27 but can be overwritten. So as not to reprogram all chips with the same new address, the others must be shut down.
San Francisco Cable Car.bas

Another rangefinder is the tiny VL53LOX

At only 25mm x 11mm this unit has a lot going for it and is almost as simple to program. It communicates via the i2c bus so has internal registers to address. For a single unit we start by setting up the link:
hi2csetup i2cmaster, 82, i2cfast, i2cbyte
Then to trigger a cycle we set a bit in register 0x00:
hi2cout 0x00,(0x01) ;trigger
Then the distance in mm will be ready in registers 0x1E, 0x1F :
hi2cin 0x1E,(b3,b2) ;read range in mm
To convert from the two input bytes to word value w3:
w3 = b3
w3 = w3 * 256
w3 = w3 | b2
And that’s how to program it!
Features and Issues
Life is never that simple of course but you can certainly get to see a screen of range numbers very quickly.
The ultrasonic sensors use a wide cone of sound so will not be too picky on size of target; they won’t care about light levels but can give a lot of false echoes.
This IR sensor might be disturbed by light levels but won’t give false readings due to target size or shape. However it has a very narrow cone of light and might just miss the target altogether if not accurately directed at it.
Like all complex electronics it hates RFI noise and can down tools in a certain situations. I have found a few filtering techniques but remember logic won’t help you. Best avoid noise.
Notice in the photo the orange tab protecting the chip apertures. Remove it if you don’t want unexplained variations of range!
Application
Wheeled vehicles require front and back sensors to prevent them running into things. If the sensors also provide distance information then this can be used for proportional deceleration. In this simple case only one sensor is in use at one time. So we can use the XSHUT pin to shutdown the other device.
I retrofitted two VL53LOX boards to a G-scale tram built 8 years ago, controlled by a Picaxe 14M2 to run at scale speed with on-board batteries:
RFI noise from the brushed motor knocked out one sensor and affected the other. I have ordered four more from China to see how they compare on this front. The recommended 220nF green capacitor has no impact, nor does earthed braiding. Three turns through a ferrite ring helped the most. Sifting errors and occasionally resetting the chips makes it workable.
The cone of ultrasound was a problem for another batch of trams fitted with radios and programmed to move around each other. The project worked fine on the bench but the trams would not move on the track. The sound reflected back from the sleepers - so the sensors had to be angled up at 45 degrees - severely impacting on their range and making them visible. For that write up see:
https://picaxeforum.co.uk/threads/model-trams-share-networks.27763/
The cone of IR light is much tighter and the beam crosses the sleepers without issue. However, the buffer stops may not be seen as the sensor gets closer because they are set at different heights. Surprisingly, fitting a piece of transparent plastic between the cross piece and the rails solves this problem.

The tiny sensor is mounted just above the safety fender and would disappear if painted black.
Addressing More Sensors
The VL53LOX has a soft i2c address at register 0x8A. It will initialise with address 0x27 but can be overwritten. So as not to reprogram all chips with the same new address, the others must be shut down.
San Francisco Cable Car.bas
Attachments
-
4.9 KB Views: 14