d = DISTANCE(trig, echo)
‘trig’ is the I/O pin connected to the “trig” input of the sensor.
‘echo’ is the pin connected “echo” output of the sensor
You can also use 3-pin devices and in that case only one pin number is specified.
The value returned is the distance in centimeters to the target or -1 if no target was detected.
If you repeatedly call this function you must arrange for a delay of at least 60mS between each call otherwise errors may occur (caused by an echo from the previous sound pulse).
The I/O pins are automatically configured by this function but note that they should be 5V capable as the HC-SR04 is a 5V device.