Hi! I recently got my HC SR04 sensor and tested it using NEW_PING library.I am using UNO R-3.
Here's my code
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
It was working initially just fine but after sometime it started giving zeroes.
Here's the output
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
I have also taken help from this thread HC-SR04 always returns zero - Sensors - Arduino Forum
but nothing has helped me resolve this problem.
I must tell you that i have rewired the circuit many times,changed the pins and changed the wires also.
I have even used other libs also which are provided with the IDE. But results are always the same.
please help.
Consistent nothing means there is an open connection somewhere.
One thing I have recently run into twice (two different sensors) on this device is the solder connection for the connection pins to the device's board somehow opens when soldering to the pins. For example the device would work in a breadboard but not after soldering to the pins when mounting the device where it was intended. I had to carefully remelt the factory solder job that connected the pins to the sensor board to bring it back to life.
Maybe that is what is going on with your setup.
akseidel:
Consistent nothing means there is an open connection somewhere.
One thing I have recently run into twice (two different sensors) on this device is the solder connection for the connection pins to the device's board somehow opens when soldering to the pins. For example the device would work in a breadboard but not after soldering to the pins when mounting the device where it was intended. I had to carefully remelt the factory solder job that connected the pins to the sensor board to bring it back to life.
Maybe that is what is going on with your setup.
I am using a breadboard for making the connections. I don't think that factory solder job is not good because initialy it was working like a charm.
Try another SR04 sensor, perhaps the one currently in use has broken?
Idahowalker:
Try another SR04 sensor, perhaps the one currently in use has broken?
When I thouch the back of sensor it gives some value but they are incorrect. So I think that sensor is fine.
Idahowalker
Rohan_kumar_2002:
When I thouch the back of sensor it gives some value but they are incorrect. So I think that sensor is fine.
Using the eraser end of a pencil and gently probing the sensor with various pressures, if you get intermittent readings; like has been previously posted, there is a bad connection.
Idahowalker:
Using the eraser end of a pencil and gently probing the sensor with various pressures, if you get intermittent readings; like has been previously posted, there is a bad connection.
If there are bad connections then on touching it must give correct values ,but that's not the case.
Regards
Idahowalker