ultrasonic sensor reads 0's when DC motors are initialied

Hi! I’m trying to build a 4wd obstacle avoiding robot using a sr04 ultrasonic sensor, 4 dc geared motors and a Adafruit motor shield(All being cheap Chinese copies). The motors have been noise suppressed by three .1uf capacitors for each motor. When I use Newping library, the sr04 reads fine, with following code:

#include <NewPing.h>
#include <AFMotor.h>
#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 //
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.

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)

but if i include
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
statements to the above code and not even run the motors, the sr04 sensor reads only 0’s(probably erroneosly).It’s all the same if I use pin d12 and d11 as trig and echo repectively. I’ve tested motors separately and they work fine. Please help.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Check that some pins used by the SR04 are not also being used by the motor shield, eg pin 4?

Thanks.. Tom.. :slight_smile: