HC-SR04 and NewPing not working but hand-written code is

I've got a very simple setup with an HC-SR04. I've connected the power (5V from the Arduino 5V and GND pins), and the trigger and echo pins from the HC-SR04 are connected to pins 11 and 12 of the Arduino Uno. If I use hand-written code like this:

#define trigPin 11
#define echoPin 12
 
float duration, distance;
 
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
 
void loop() {
   
  // Write a pulse to the HC-SR04 Trigger Pin
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Measure the response from the HC-SR04 Echo Pin
 
  duration = pulseIn(echoPin, HIGH);
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
 
  Serial.print("Distance = ");
  if (distance >= 400 || distance <= 2) {
     Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

it works great! No issues. However, the general advice around the web is to prefer NewPing. Okay. I installed NewPing from the library manager in the IDE, and tried the very simple code:

#include <NewPing.h>
 
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 500

// NewPing setup of pins and maximum distance
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 
 
void setup() {
   Serial.begin(9600);
}
 
void loop() {
   delay(50);
   unsigned int distance = sonar.ping_in();
   Serial.print(distance);
   Serial.println("in");
}

which sadly does not work very well. Most of the distances printed are zero, and even if I put something large in front of the sensor, it only briefly registers it before going back to zero. I've tried increasing the delay to 500, which improves things, but there are still a lot of zeros. What would cause the hand-written code to work but not the NewPing code?

Is that your sketch, or a NewPing library example? Did you test any library example?

While there is no reason "New Ping" should not work. However I use code similar to yours. I had looked at using Timer1 hoping to get better resolution but then I realized the measurement was not stable enough to warrant the extra effort.

That code is directly from the NewPing repository.

Okay, when I used a different breadboard, the NewPing version worked. I think the old breadboard was a bit loose. But it's odd the handwritten version worked fine. Not sure if the NewPing library has some kind of procedure for discarding what it feels are noisy measurements?

You know what? Never mind. I solved the mystery, and I feel stupid now. I realize the 0 measurements are not random after all after experimenting. They happen when the reflecting surface is angled away from the receiver. Oops. Disregard, and thanks for the help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.