Go Down

Topic: Ultrasonic distance sensor HC-SR04 lack of timeout problem (Read 7258 times) previous topic - next topic


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.


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.
"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.


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:


Did you have the same problem with the other sensors?


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%.


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.....
Electronics newbie & Home Machinist


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

The distance formula is given here.

5cm does not sound right at all.

Do you have access to an oscilloscope?


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?
Electronics newbie & Home Machinist


but what tests can I do lacking an Oscilloscope

Train your pet bat to flap its wings when it hears a 40kHz pulse?
"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.


Well funny as that may be, XD I'm still in need of guidance here. I'm gonna start a new post. I feel bad hijacking this one. sorry
Electronics newbie & Home Machinist


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: [Select]

// 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(iAlertLED, OUTPUT);
  iEchoTimeout = iMaxDistance*2*29;

void loop() {
  digitalWrite(iTrigger, LOW);
  digitalWrite(iTrigger, HIGH);
  digitalWrite(iTrigger, LOW);
  unsigned long ulPing = pulseIn(iEcho,HIGH,iEchoTimeout);
  if(ulPing) {
    digitalWrite(iAlertLED, HIGH);
  else {
    digitalWrite(iAlertLED, HIGH);
    digitalWrite(iAlertLED, LOW);

void SimulateLoad(int iDutyCycle) {


Just ran into same problem too with HC-SR04. Tried to reset sonar's CPU by sending pulse to pin 7, but didn't help. For me it looks like analog part of the module works like some sort of trigger if there no enough external noise in background (yes, it did help when i just clap near module - starts working again).

So, i've doing pulse to discharge C9 capacitor ( http://kazus.ru/forums/attachment.php?attachmentid=75731&d=1423821255 ) by connecting it to one of digital pins of arduino through 500 ohm resistor (setting in INPUT mode, without pull-up most of time, OUTPUT mode + LOW for pulse). Needs a small delay to recharge the capacitor to right level after the pulse.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131