Ultrasonic Sensor returning zero

Issue #22 resolved
Former user created an issue

I know there has been a previous post on this issue, but even when i place a flat object parallel to the sensor at around 1 meter range, the sensor still returns zero. Also i have implemented your suggested fix, to no avail.

Here is my code.

#include <NewPing.h>

#define TRIGGER_PIN  13
#define ECHO_PIN     12
#define MAX_DISTANCE 300


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  Serial.begin(9600);
}

void loop() {

  delay(150);
  int uS = sonar.ping();
  if (uS==0)
  {
    Serial.println("MAX: resetting sensor");
    pinMode(ECHO_PIN, OUTPUT);
    delay(150);
    digitalWrite(ECHO_PIN, LOW);
    delay(150);
    pinMode(ECHO_PIN, INPUT);
    delay(150);
  }
  else
  {
  Serial.print(" ");
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
  }
}

Comments (2)

  1. Tim Eckel repo owner

    That isn't a solution, it's a suggestion. Also, the problem is that you have a defective sensor, not the library. The library can't fix a defective sensor, and that code is only an attempt to wake up the sensor based on what others have experienced. Still, the problem is a faulty sensor.

  2. Log in to comment