HCSR04 Ultrasonic Sensor WAS Acting Fine

Hello. I have a robot with alot of things plugged into it, one of which being an Ultra Sonic Sensor. Just recently, the sensor started bugging out - returning 0 most of the time, and occasionally “flickering” to a normal sensor value before returning to zero. I have tried using a different sensor, different pins on the arduino, and different values in the code with no luck. Here is my code:

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  28  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     29  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // 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(9600); // 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");
}

Any idea why the sensor keeps returning zero?

Hello. I have a robot with alot [sic] of things plugged into it

Like what?

AWOL: Like what?

Two Servo Motors (Battery Powered) An Arduino MOVI Shield An HC- 05 Bluetooth Module And (of couse) an Ultrasonic Sensor

It's an Arduino Mega by the way.

After studying the behavior of the sensor more closly, I have noticed that when MAX_DISTANCE is set to 400, the sensor reads values at around 190cm properly. (The nearest wall) When I move my hand close to the sensor, the sensor starts acting oddly at around 40cm, and lower. (And jumps to a reading of 0cm)

which US sensor are you using? looks like the three prong by the code...

nevermind... i'll shut up. i didn't see the first part

Now the Sensor is jumping back and forth between 0 and 1149!

Update: Nevermind. See next post below.

Aha!
I noticed something new!

When the sensor is working properly, it makes a soft “puttering” noise. I assume this is caused by the ultrasonic clicks the sensor generates when it is operating. However, when I move the sensor around a bit, the clicking stops.

As long as I hear the clicking, the sensor works fine. But as soon as the clicking stops it goes to zero!

By the way, when I discovered this phenomenon I was using a different Arduino board. This leads me to conclude that it is not the Arduino Board on the robot’s other systems that are causing these issues.

Couls it be an issue with the jumper cables connecting the sensor and the arduino?

So when the wires bend one of them disconnects.

As I mentioned, I have tried a different sensor with a different board and received the same results! The only constants between the two is the code (which WAS working fine, and has not been changed) and the wires!

Do you think that scenario (a bad jumper wire) is likely?