Ping Sensor and motor shield not working together

My project is to run two motors using motor shield taking the input information from the ping sensor.
When the distance calculated using ping is less than 25 cm the motors will run in forward direction. Else they will run in the opposite direction.

My problem: When the motor shield

(http://www.amazon.in/gp/product/B00ZF7L7FU?psc=1&redirect=true&ref_=oh_aui_detailpage_o06_s00#productDetails)

is attached to the arduino uno ping sensor is reading “0” in the serial monitor and the motors are also not running.

The program I am using is

#include <AFMotor.h>
#define trigPin 12 //difine the pins of the sensor
#define echoPin 13
AF_DCMotor motor1(1); // set up motors.
AF_DCMotor motor2(2);

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

motor1.setSpeed(255);
motor2.setSpeed(255);
motor1.run(RELEASE);
motor2.run(RELEASE);

}

void loop() {

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29;
if(distance<25) {
Serial.println(" “);
Serial.println(“Obstacle is close than 25 cm”);
Serial.println(“Obstcle detail”);
Serial.println(distance);
Serial.print(“cm”);
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
else{
Serial.println(” ");
Serial.println(“Obstacle is at a distance greater than 25 cm”);
Serial.println(“Obstcle detail”);
Serial.println(distance);
Serial.print(“cm”);
motor1.run(FORWARD);
motor2.run(FORWARD);

}

}

I have tested the ping and the motor separately, they work quite well. But when attached together is doesn’t works. Please help.

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks … Tom… :slight_smile:

Hi Tom,
Thanks for the reply,
The code I am using is

``M

How does the Arduino communicate with the motor shield? If it uses SPI, you can't use pins 12 and 13 for the ping sensor.

The pins free are 2, 13, A0-A5

What pins are not used on the motor shield?
All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19)
Digital pin 2, and 13 are not used.
The following pins are in use only if the DC/Stepper noted is in use:
Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)
The following pins are in use if any DC/steppers are used
Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch
The following pins are used only if that particular servo is in use:
Digitals pin 9: Servo #1 control
Digital pin 10: Servo #2 control