HCSR-04

I am using HCSR-04 ultrasonic sensor for obstacle avoidance. Once it senses anything beyond range like 400cm, it stops sensing even below 400cm. What should I do?

I used the code on the link below:

Call pulseIn() with a timeout parameter.

How much timeout should I use?

An "instructable", you say?

Oh dear!

sulivuli:
How much timeout should I use?

A little bit longer than the maximum range would give you.

Thanks for responding!

I tried using a timeout but that didn't work too.

but if i cut off the 5V supply to the HCSR-04 and then connect again, it works upto the next zero value.

Is there anyway I can switch off the senor and switch on again?

What switching transistor should I use? and what are the connections?

I vaguely recall reading about this recently.

I see that Instructable's code doesn't use a library; I may be wrong but maybe the NewPing library takes care of the problem you describe.

I have used several HCSR04 rangefinders and never encountered the problem of the sensor locking up like that so I wired one up and loaded the code from the link in your original post. Here is a sample of the output. The LEDs work as expected as well. It has run several minutes without locking up and with me varying the range with my hand.

Post the exact code that you are loading and a sample of the output. Try a different HCSR04.

Out of range
Out of range
102 cm
14 cm
30 cm
Out of range
Out of range
14 cm
23 cm
Out of range
Out of range
18 cm
23 cm
62 cm
100 cm
Out of range
79 cm
51 cm
Out of range
Out of range

The code from the link (cleaned up format only)

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  //  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 4)
  {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
    digitalWrite(led2,LOW);
  }
  else 
  {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else 
  {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

You can set the timeout to 15000 to make the program more responsive.

  duration = pulseIn(echoPin, HIGH, 15000);

There are reports of defective HC-SR04s that lock up. Throw it away and try another.

Thank you all!
I've bought some new HCSR04s.
I'll also try the code that has been posted.

I had a problem installing the Ping Library. :slight_smile: :slight_smile:
Thanks so much for replying.

sulivuli:
Thanks for responding!

I tried using a timeout but that didn't work too.

Details?

sulivuli:
I had a problem installing the Ping Library. :slight_smile: :slight_smile:

But did you eventually succeed to install it or not? NewPing?