HC-SRF04 is an ultrasonic ranging module provides 2cm – 400cm non-contact measurement function, the ranging accuracy can reach to 3mm.
Personally I got a good result in the range from 3,5cm to 3,8m.
- The Wired Connecting are:
- 5V Supply
- Trigger Pulse Input
- Echo Pulse Output
- 0V Ground
The Electric Parameters are in the image below.
The speed of the sound in the air at a temperature of 20° (Celsius Degrees) is about 343.4 m/s, here we will use the approximate speed of 340 m/s for simplicity, if more precision is needed the following law can be used depending on the temperature t:
V = 331.4 + 0.62 * t
For the realization of the ultrasonic distance meter we assume to work at an ambient temperature of 20 ° C and therefore the speed of sound will be about:
Now, remembering that:
v = s / t (v==speed, s == space, t == time)
the path space will be:
s = v * t
s = 0.0343 * t (time it is expressed in uS)
To calculate the distance traveled, it must be taken into account that the sound travels twice the distance to be measured (reaches the object and returns back to the sensor), then the measured t value must be divided by 2.
The correct formula for measuring the distance traveled is therefore:
s = 0.0343 * (t/2)
that is equivalent to:
s = 0,0171 * t or s = t/58.31
It is important use a push pull output to drive the TRIGGER pin of the module for resolve some kind o synchronization issue between the transducers.
For this reason I use OutPut pin of STM32 , that drive the TRIGGER, in push pull mode.
Is the default on mBED compiler, see this page.
SW using mBED compiler
Here there is my example that use a NUCLEO-F070RB + HC-SRF04 under mBED.
Thanks to mBED is very easy transfer the SW on different NUCLEO or MCU.