May 24, 2015

Repair faulty HC-SR04 ultrasonic sensor (solve erratic no-readings problems)

Recently I got some of these cheap sensors from Chinese suppliers, for a workshop I had to teach.
A couple of students reported that their sensors were behaving in an erratic way, sometimes working, then stopping again, the working...
After replacing them with more expensive ones bought at a local store, I decided to try fixing the faulty ones...

EDIT (11/oct/16): have a look at Matthieu comment below, you may get rid of the errors by software instead of hacking the board.

The faulty sensors were bought from different manufacturers, so it wasn't a "bad batch" issue.

Trying to figure out what was happening, I observed that sometimes when I touched the back of the modules' boards made them work again after they got stuck.
Probably there was a bad solder or some interference or bad shielding problem. So, touching here and there, I localized the exact pin that reacted to my fingers. It was one of the receiver transducer's pins (the one not tied to ground).
Then I tested to connect that pin through a 10k resistor to ground but that didn't help. Then to VCC, but it didn't work either.
So I thought that there was some kind o synchronization issue between the transducers, that required the reacting pin of the receiver transducer's to be triggered now and then.
That idea led me to make one more try, this time connecting the pin through the 10k resistor to the TRIGGER input pin of the module, and voila!

I did the same with the other modules and... all of them started to work right, finally!


  1. Just tried this on one of my cheap Chinese boards - no luck. Still returning ZERO until I make a loud clap then it works for just a minute. Thanks for the idea though!

    1. I know this comment is quite old, but hopefuly this would help somebody !
      Your issue is due to a lack of timeout of some chinese boards. If it sends a pulse but don't get the echo back, it's stuck. By claping in your hands you produce a sound that trigger the echo and unstuck you from an infinite waiting. The simplest solution is to have a timeout in your software (if you ise Arduino's pulseIn the default timeout is 1 second but you can specify it in microseconds, just look at the documentation), and then, if you trigger the timeout (with pulseIn it returns 0 when timeout occurs), you know that you'll be stucked. At this moment, you have a very simple solution : just switch pinMode on Echo to OUTPUT, and send a LOW pulse, then go back to INPUT. By ding this you simulate an echo and the sensor chip will be unstucked !
      I can give code if anybody need

    2. This comment has been removed by the author.

    3. For those who asked for code, here it is !
      It's really simple

      int getDistance(int trigPin, int echoPin) // returns the distance (cm)
      long duration, distance;

      digitalWrite(trigPin, HIGH); // We send a 10us pulse
      digitalWrite(trigPin, LOW);

      duration = pulseIn(echoPin, HIGH, 20000); // We wait for the echo to come back, with a timeout of 20ms, which corresponds approximately to 3m

      // pulseIn will only return 0 if it timed out. (or if echoPin was already to 1, but it should not happen)
      if(duration == 0) // If we timed out
      pinMode(echoPin, OUTPUT); // Then we set echo pin to output mode
      digitalWrite(echoPin, LOW); // We send a LOW pulse to the echo pin
      pinMode(echoPin, INPUT); // And finaly we come back to input mode

      distance = (duration/2) / 29.1; // We calculate the distance (sound speed in air is aprox. 291m/s), /2 because of the pulse going and coming

      return distance; //We return the result. Here you can find a 0 if we timed out


    4. Matthieu, please, can you deliver that code? Or your code is similar to the Fardenco? Thanks in advance.

    5. Hi, in fact I am Fardenco, I don't know why my name was changed to my pseudo when I posted this comment
      So this is indeed the code I was talking about ;)

  2. Thank you very much, the same problem has been resolved.

  3. Thanks!!!! same problem with 13 hc-sr04

  4. Thanks!! same problem with 13 hc-sr04

  5. it works, but sensitivity goes down - now it senses only up to 30 cm.
    I have idea to put transistor key triggered from one more arduino pin in case no pulse is received - maybe that could also work.

  6. After testing my code and spending some time debugging i found your guide and it worked for me, nice hack, i attached a picture

  7. my hc-sr04 shows wrong/random reading.This happened after my first usage. first it was right now its a random number reading. any help would be appreciated.

  8. can i use 1k resistor insted of 10k resistor

  9. Mine seems to get stuck at 4cm. Do you think this would help?

    1. I don't know if it will work for you. However, this sensor range is supposed to start at 5cm, so check your code first.

  10. Bought some HC-SR04 just few days ago. Wouldn't work with above solution. However they work when inverting the trigger signal. I have the trigger high all time and pull it low for 20 µs. Then the pulse length of the echo shows the distance.

    More about this version of HC-SR04
    Though the board looks the same on first glance the layout is a bit different. The ICs on U3 is RCWL-9200 where I did not find any datasheet for and U1 is a LM324 XJS 834H.

    First I watched the signal of trigger and echo on an oscilloscope driving it the regular way. When pulling the trigger high it looked like charging an capacitor and within 20µs did not reach more than 2V, while at the same time the echo was following the trigger signal. Really looked confusing. Now with keeping the trigger high and triggering by pulling low it looks perfect with sharp edges on the signals.