NewPing Library


I have a problem with NewPing Library, always return Ping : 0cm when i lunch NewPingExample code.

I am on Arduino Pro Mini 5V ATMega 328, the Sonic sensor is correct, because if i use the HY-SRF05 test code, i have got values, with the same pins.

I don’t understand what happen.

Working code for testing sonic sensor

const int TRIG_PIN = 7;
const int ECHO_PIN = 8;
void setup() {
  // initialize serial communication:
void loop()
   long duration, distanceCm, distanceIn;
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  digitalWrite(TRIG_PIN, HIGH);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN,HIGH);
  // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
  distanceIn = duration / 74 / 2;
 Serial.print("in, ");


No resut with NewPing Library

#include <NewPing.h>

#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // 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.
  pinMode(ECHO_PIN, INPUT);

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)

Anybody help me ?

Hi, Not sure...

Code HERE I know works..

Note: you don't need to pinMode the sensor pins; the library does that..

Same thing return Ping: 0 in Ping: 0 cm :confused:

What distances do you get with the "HY-SRF05 test code"?

Between 10 cm to 25 cm for the tests.