The HY-SRF05 is an ultrasonic emitter/receiver used to measure distance with a precision of ~0.3cm. It sends out a 40 KHz square wave signal that reflects on objects in front of the sensor. This signal is then read back by the sensor and the duration of the received signal is reflected on the ECHO pin.
Features
Supply voltage: 4.5V to 5.5V
Supply current: 10 to 40mA
Trigger pin format: 10 uS digital pulse
Sound frequency: 40 KHz
Echo pin output: 0V-VCC
Echo pin format: digital
How to use
Send a 10Us wide pulse (low to high) to the Trigger pin.
Monitor the ECHO pin.
When the ECHO pin goes HIGH, start a timer.
When the ECHO pin goes LOW, stop the timer and save the elapsed time.
Use the elapsed time in the following formula to get the distance in cm:
Distance (in cm) = (elapsed time * sound velocity (340 m/s)) / 100 / 2
We will see a code example later
Parts List
Layout
Code
[codesyntax lang=”cpp”]
/* VCC to +5V GND to ground TRIG to digital pin 12 ECHO to digital pin 13 */ const int TRIG_PIN = 12; const int ECHO_PIN = 13; void setup() { // initialize serial communication: Serial.begin(9600); pinMode(TRIG_PIN,OUTPUT); pinMode(ECHO_PIN,INPUT); } void loop() { long duration, distanceCm, distanceIn; digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN,HIGH); // convert the time into a distance distanceCm = duration / 29.1 / 2 ; distanceIn = duration / 74 / 2; if (distanceCm <= 0) { Serial.println("Out of range"); } else { Serial.print(distanceIn); Serial.print("in: "); Serial.print(distanceCm); Serial.print("cm"); Serial.println(); } delay(1000); }
[/codesyntax]
Output
Open the serial monitor and you will something like this depending how close the module is to an object
I was moving the sensor away from an object
4in, 10cm
3in, 9cm
4in, 10cm
4in, 10cm
4in, 10cm
4in, 10cm
4in, 12cm
4in, 12cm
4in, 12cm