Distance Measurement with an Ultrasonic Sensor HY-SRF05

Measure the world around you.

Jul 23, 2017

89078 views

10 respects

Components and supplies

1

Arduino UNO

1

HY-SRF05 Ultrasonic Sensor

Project description

Code

Project code

arduino

Downloadable files

circuit diagram

circuit diagram

circuit diagram

circuit diagram

Comments

Only logged in users can leave comments

Anonymous user

2 years ago

In my testing - simple breadboard as shown in this schematic - the sensor seems to have trouble with 'soft surfaces' - such as a cloth covered couch, sweatshirt etc. it very consistently detects the correct range to 'harder surfaces' - bare hand, book, water bottle etc. I planned to use this sensor on a 4wd car - but if it misses softer surfaces then I'll need to use something else... any advice?

Obi-JuanKenobi

2 years ago

So i am trying to have two of this sensors working at once. I connected both of the sensors to the same 5v pin and to the same grd pin, but I connected the other pins individually (I mean I have two pins for the trigger and two pins for the echo). Then I copied the code and duplicated it, and it looks like this: const unsigned int TRIG_PIN1=3; const unsigned int ECHO_PIN1=2; const unsigned int TRIG_PIN2=5; const unsigned int ECHO_PIN2=4; const unsigned int BAUD_RATE=9600; void setup() { pinMode(TRIG_PIN1, OUTPUT); pinMode(ECHO_PIN1, INPUT); pinMode(TRIG_PIN2, OUTPUT); pinMode(ECHO_PIN2, INPUT); Serial.begin(BAUD_RATE); } void loop() { digitalWrite(TRIG_PIN1, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN1, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN1, LOW); digitalWrite(TRIG_PIN2, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN2, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN2, LOW); const unsigned long duration1= pulseIn(ECHO_PIN1, HIGH); int distance1= duration1/29/2; if(duration1==0){ Serial.println("Warning: no pulse from sensor"); } else{ Serial.print("distance to nearest object:"); Serial.println(distance1); Serial.println(" cm"); } delay(100); const unsigned long duration2= pulseIn(ECHO_PIN2, HIGH); int distance2= duration2/29/2; if(duration2==0){ Serial.println("Warning: no pulse from sensor"); } else{ Serial.print("distance to nearest object:"); Serial.println(distance2); Serial.println(" cm"); } delay(100); } Then I opened the serial monitor and it tells me that one works perfectly and that the other says, Warning: no pulse from sensor. How do I get two running in sync?

Nicholas_N

2 years ago

Maybe the signal of the first sensor disturbs the second. Try to use a bigger delay between the sensors.

Anonymous user

2 years ago

You did a sequence error. You send both signal near at the same time, then wait for the first echo, then wait 100 ms and after this wait for the second echo, but it has arrived 100 ms before... You should send first signal, wait for first echo, wait for silence (100 ms should be enought), send second signal and wait for second echo. Wait again for silence and restart.

Obi-JuanKenobi

2 years ago

That sadly didn't work, thanks for the help.

rowan07

2 years ago

in the code you give different pins to plug in then in the code

Anonymous user

2 years ago

hi, good job! I have a question... in arduino references it is said about pulseIn that: if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds now... in your code pulseIn pin is low at first, then it turns high when it receives pule which has been sent by trig, then it turns low after 10 MicroSeconds (as delay time). so duration must be 10 MicroSeconds!!! I know your code works and is right, but I cant solve this! I appreciate your help.

fmarzocca

2 years ago

The sensor has a maximum range of 450 cm, not 3 meters!!

Anonymous user

5 years ago

In my testing - simple breadboard as shown in this schematic - the sensor seems to have trouble with 'soft surfaces' - such as a cloth covered couch, sweatshirt etc. it very consistently detects the correct range to 'harder surfaces' - bare hand, book, water bottle etc. I planned to use this sensor on a 4wd car - but if it misses softer surfaces then I'll need to use something else... any advice?

fmarzocca

6 years ago

The sensor has a maximum range of 450 cm, not 3 meters!!

MortezaAzad

6 years ago

hi, good job! I have a question... in arduino references it is said about pulseIn that: if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds now... in your code pulseIn pin is low at first, then it turns high when it receives pule which has been sent by trig, then it turns low after 10 MicroSeconds (as delay time). so duration must be 10 MicroSeconds!!! I know your code works and is right, but I cant solve this! I appreciate your help.

Obi-JuanKenobi

6 years ago

So i am trying to have two of this sensors working at once. I connected both of the sensors to the same 5v pin and to the same grd pin, but I connected the other pins individually (I mean I have two pins for the trigger and two pins for the echo). Then I copied the code and duplicated it, and it looks like this: const unsigned int TRIG_PIN1=3; const unsigned int ECHO_PIN1=2; const unsigned int TRIG_PIN2=5; const unsigned int ECHO_PIN2=4; const unsigned int BAUD_RATE=9600; void setup() { pinMode(TRIG_PIN1, OUTPUT); pinMode(ECHO_PIN1, INPUT); pinMode(TRIG_PIN2, OUTPUT); pinMode(ECHO_PIN2, INPUT); Serial.begin(BAUD_RATE); } void loop() { digitalWrite(TRIG_PIN1, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN1, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN1, LOW); digitalWrite(TRIG_PIN2, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN2, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN2, LOW); const unsigned long duration1= pulseIn(ECHO_PIN1, HIGH); int distance1= duration1/29/2; if(duration1==0){ Serial.println("Warning: no pulse from sensor"); } else{ Serial.print("distance to nearest object:"); Serial.println(distance1); Serial.println(" cm"); } delay(100); const unsigned long duration2= pulseIn(ECHO_PIN2, HIGH); int distance2= duration2/29/2; if(duration2==0){ Serial.println("Warning: no pulse from sensor"); } else{ Serial.print("distance to nearest object:"); Serial.println(distance2); Serial.println(" cm"); } delay(100); } Then I opened the serial monitor and it tells me that one works perfectly and that the other says, Warning: no pulse from sensor. How do I get two running in sync?

Obi-JuanKenobi

2 years ago

That sadly didn't work, thanks for the help.

Anonymous user

2 years ago

You did a sequence error. You send both signal near at the same time, then wait for the first echo, then wait 100 ms and after this wait for the second echo, but it has arrived 100 ms before... You should send first signal, wait for first echo, wait for silence (100 ms should be enought), send second signal and wait for second echo. Wait again for silence and restart.

Nicholas_N

2 years ago

Maybe the signal of the first sensor disturbs the second. Try to use a bigger delay between the sensors.

rowan07

7 years ago

in the code you give different pins to plug in then in the code

Anonymous user

7 years ago

Hi Nicholas, there is a difference between HY-SRF05 and HC-SR04 from your BOM. Found the datasheet here: http://www.f15ijp.com/2012/09/arduino-ultrasonic-sensor-hc-sr04-or-hy-srf05/