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!


22 comments:

  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!

    ReplyDelete
    Replies
    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

      Delete
    2. This comment has been removed by the author.

      Delete
    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
      delayMicroseconds(10);
      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
      delayMicroseconds(200);
      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
      }

      Matthieu

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

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

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

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

    ReplyDelete
  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
    https://postimg.org/image/5e7et2nm5/

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

    ReplyDelete