Go Down

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


Hello everyone
I'm in the same case as you, 3 sensors and all the same problems .
even the solution with transistor to turn off the power does nothing.
a lot of research and time spent, but no real solution ,
TECKEL , have you received a defective sensor?
Thank you for your help.


Howdy arduiners?

After being very upset with this SR-04 meter, I have found that works for my models. Here it goes.

The solution consists in giving power to the SR-04 through a transistor, a BC-547. The SR-04 is the load, and the transistor is activated with one output pin of the arduino. Newping library is used and code in explained below. Basically consist in checking if the distances is getting too low and, in this case, power down the meter, delay and then power it up again. Also a led has been added to test the condition of the power. When lit the SR-04 is powered up.

It FINALLY worked for me! If it is not clear enough I can send anyone a draft schematic or even a picture of the mosnter. Hope anyone can breath again now! ;-)


#include <NewPing.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  8
#define  ECHO_PIN     9
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;
const int resetsensor = 12; //Pin to BC-547 in Common Emitter mode thru 1k ohm resistor in base
const int ledsensor= 13; //Light to clone the reset sensor

void setup()   /****** SETUP: RUNS ONCE ******/
  Serial.println("Resetting the dammed SR-04 UltraSonic Distance Meter");
  Serial.println("questions to vidalgm@gmail.com");
  pinMode (resetsensor, OUTPUT);
  pinMode (ledsensor, OUTPUT);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  digitalWrite (resetsensor,HIGH); //Gives power to the dammned SR-04 thru the BC-547
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.println(" cm");
  if (DistanceCm < 5){  //If distance is getting too low
  digitalWrite (resetsensor, LOW); //reset transistor, this is, take out power of SR-04
  digitalWrite (ledsensor, LOW); //light down to indicate we are resetting
  delay (200);
  digitalWrite (resetsensor, HIGH); //power up SR-04 again
  digitalWrite (ledsensor, HIGH); //ligh up to indicate SR-04 is again powered up

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

// None
//*********( THE END )***********

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