When i run a test code in Newping library for ultrasonic sensor SR04 then in works properly but when i run the actual code for obstacle avoiding robot then it shows 0 cm everytime. This means that wiring is correct but there is something wrong in my code below

#include <NewPing.h>
#include <AFMotor.h> //import your motor shield library

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor motor1(1, MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);

int distance;

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

void loop() {
  delay(50);
  distance = sonar.ping_cm(); // start the scan
  delay(20);
  if (distance < 2) {   
   Serial.println("Close Obstacle detected!" );
   Serial.println("Obstacle Details:");
   Serial.print("Distance From Robot is " );
   Serial.print(distance);
   Serial.print( " CM!");// print out the distance in centimeters.
   delay(10);
   Serial.println (" The obstacle is declared a threat due to close distance. ");
   Serial.println (" Turning !");
   motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
   motor2.run(BACKWARD);

}
  else {
   Serial.println("No obstacle detected. going forward");
   delay(15);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward! 
   motor2.run(FORWARD);
  }
}

Test code for sensor which worked:-

#include <NewPing.h>

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

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

void loop() {
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}

Tell me whats wrong here

motor shield used is from dk electronics

Motor Shield used below Wiring of sensor is connected to Arduino below the shield

  • replace the whole if-else loop with Serial.print(distance); .... what does it print now? – jsotola Dec 1 at 19:56
  • The NewPing library returns zero when there is no echo detected. And 2 cm may be too short. If I remember correctly, this is the minimal distance that these sensors can detect. – chrisl Dec 2 at 10:25
  • yes i tried serial.print and showed some reading. But now it goes to 1000cm :| – Sultan Morbiwala Dec 2 at 11:33

Try with

unsigned int distance;

Instead of

int distance;

It could also be the motor shield that is using the same pins as your ultrasonic sensor.

  • Thanks but it didn't work. Using serial.print() instead of distance variable helped. – Sultan Morbiwala Dec 2 at 12:55
  • And your sure that your motor shield is not conflicting ? What make/model is it ? – Andre Courchesne Dec 2 at 14:10
  • its like the one in dk electronics. Now again the sensor is showing 0cm so i think you are right. I have done sensor wiring under the shield so is that a good method? – Sultan Morbiwala Dec 2 at 14:28
  • Not sure about the wiring without seeing it. What I would recommend is trying your sketch without the motor shield. – Andre Courchesne Dec 2 at 14:45
  • I tried the test code without motor shield and it worked but doesnt work with motor shield on it. – Sultan Morbiwala Dec 2 at 17:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.