Pages: [1]   Go Down
Author Topic: Ultrasonic distance sensor HC-SR04 lack of timeout problem  (Read 5732 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to use a HC-SR04 ultasonic distance sensor from Ebay (http://cgi.ebay.com.au/ws/eBayISAPI.dll?ViewItem&item=280640838032),
but it doesn't appear to have an internal timeout for when it fails to detect an echo.
This sometimes results in the echo line staying high forever, even if you re-trigger a measurement.
The only way to get it to reset is by cycling the power.

The spec for the HC-SR04 is somewhat incomplete, however research indicates that this is a copy of the (Devantech) SRF04 sensor from the following site, http://www.robot-electronics.co.uk/htm/srf04tech.htm.
The spec for this says that "If nothing is detected then the SRF04 will lower its echo line anyway after about 36mS.".

My HC-SR04 sensor doesn't do this. Does anyone else have the same sensor with a working timeout?

The vendor has scrubbed the IDs off the chips so I don't hold much hope for doing a retro-fix.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25803
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
however research indicates that this is a copy of the (Devantech) SRF04
I've been using the Devantech devices for over ten years without any problems like you describe.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i think i have the same problem... my hc-sr04 is measuring the distance with no problems. but if there is no object in range, i become a 0 value and the sensor hangs and any next value ist 0 even when there are objects....
i should get 3 other sensors this week, so i'll tel you..
sry for my eng.. smiley-roll
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you have the same problem with the other sensors?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Following up to myself....

I'm driving the sensor power rail from an Arduino pin and if the echo PWM signal gets stuck because of no echo I cycle the power off for 100ms, then wait for 100ms before taking a reading.
So far it's been working very well.
With this sensor I get distance resolution of about 1mm  which is better than the spec of 3mm and much better than a DF Robotics sensor which only seems to measure in 10mm increments even when using the PWM output.
Jitter is only about 2us in a 1448us round trip at a distance of 25cm, which equates to 0.14%.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry to hijack your post, but I have a question for you. What kind of range do you get with yours? Mine seems to have a very limited range of about 5cm. Perhaps i'm coding incorrectly. What formula do you use to convert output to in/cm?

I've tried several examples always the same result.....
Logged

SubMicro
Electronics newbie & Home Machinist

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Max range to a flat hard perpendicular surface is about 1.5 meters.

The distance formula is given here.
http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

5cm does not sound right at all.

Do you have access to an oscilloscope?

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry it's on the "I really need one but can't afford it list!" Any way it's just an ID10T prob or are my sensors bad? I know you can't tell with out testing, but what tests can I do lacking an Oscilloscope?
Logged

SubMicro
Electronics newbie & Home Machinist

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25803
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but what tests can I do lacking an Oscilloscope
Train your pet bat to flap its wings when it hears a 40kHz pulse?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 153
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well funny as that may be, smiley-lol I'm still in need of guidance here. I'm gonna start a new post. I feel bad hijacking this one. sorry
Logged

SubMicro
Electronics newbie & Home Machinist

Reno Nevada USA
Offline Offline
Newbie
*
Karma: 0
Posts: 37
C# Developer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This timout thing bugged me too. I applied a bit of RTFM strategy and found a solution. Good luck, and please let me know if this helps you.
Code:



// JamesHappy
// Arduino UNO 0022
// Demonstration sketch of HC-SR04 Ultrasonic Range Finder with echo timeout

// Onboard LED should blink while searching for surface
// Onboard LED should be solid when surface is within a specified distance
// Rangefinder should only wait for echos under a calculated timeout

// Reference: http://www.elecfreaks.com/244.html
// Reference: http://www.arduino.cc/en/Reference/PulseIn
// Note: HC-SR04 tested successful in both 5v and 3.5v

int iTrigger     = 2;  // Digital Pin 2
int iEcho        = 3;  // Digital Pin 3
int iAlertLED    = 13; // Digital Pin 13
int iEchoTimeout = 0;  // In Microseconds
int iMaxDistance = 20; // In Centimeters

int iTriggerPullDown = 2;  // In Microseconds
int iPingWidth       = 10; // In Microseconds

void setup() {
  pinMode(iTrigger,OUTPUT);
  pinMode(iEcho,INPUT);
  pinMode(iAlertLED, OUTPUT);
  iEchoTimeout = iMaxDistance*2*29;
}

void loop() {
  digitalWrite(iTrigger, LOW);
  delayMicroseconds(iTriggerPullDown);
  digitalWrite(iTrigger, HIGH);
  delayMicroseconds(iPingWidth);
  digitalWrite(iTrigger, LOW);
  unsigned long ulPing = pulseIn(iEcho,HIGH,iEchoTimeout);
  if(ulPing) {
    digitalWrite(iAlertLED, HIGH);
    SimulateLoad(250);
  }
  else {
    digitalWrite(iAlertLED, HIGH);
    delay(25);
    digitalWrite(iAlertLED, LOW);
    SimulateLoad(225);
  }
}

void SimulateLoad(int iDutyCycle) {
  delay(iDutyCycle);
}

Logged

Pages: [1]   Go Up
Jump to: