HC-SR04 Ultrasonic Sensor Not Working When Using Motor Shield

I have an HC-SR04 ultrasonic sensor. When hooked directly to my Arduino Uno, it works fine. When I attach the Arduino motorshield, however, I get no response from the Ultrasonic sensor. I am using the 5V and GND pin inputs for power, A0 for the sensor's TRIG and A1 for the sensor's ECHO. Do I need to change these when using the motor shield?

Thank you.

No idea, what pins does your motor shield use?

Will it work with the shield attached but the motor not turning?

Hi dumars4,
Why are you using analogue pins for the HC-SR04?? I have used this many times, but usually on D7 & 8, save Analogues for other things including your servo.
As Grumpy says,. what shield are you using!! I tried one of those cheap Chinese things and in the end gave up..

I brought a cheap (Chinese) proto shield and stuck a TI DRV883 module on it for motor control including PWM, etc. Then just wired the SR04 and servo to pin headers.... Here's a picture...

Can I help!

Regards

Mel.

I'm using the Arduino motor shield: http://arduino.cc/en/Main/ArduinoMotorShieldR3

I used the analog input because that's where the tutorial I was using had them when plugging directly into the Arduino. Unfortunately, the tutorial was using the AdaFruit motor shield. After putting the motor shield on, I plugged into the same pins but it doesn't work. See attached photo.

Thank you.

Did you manage to make progress on this issue? I had the exact same problem, as a complete beginner using the tutorial from Miguel Grinberg's blog (which I presume is the same one you mention, as he suggests using the A0 and A1 pins as digital 14 and 15).

It was fixed easily enough by shifting the Trig and Echo pins for the sensor to 4 and 7 respectively on the MotorShield and making the change in the sketch definitions. I'm still wondering though why A0 and A1 stopped working when the MotorShield was placed...

EDIT: The issue turns out to be a very simple one (we all have to start somewhere, right?) in that the Arduino Motor Shield R3 uses A0 and A1 for current sensing capabilities. In the end it was a RTFM problem, combined with following a tutorial that used a different motor shield.

@dumars4, have you sorted this out? I have the same problem.

Have you read this reply?
It was fixed easily enough by shifting the Trig and Echo pins for the sensor to 4 and 7 respectively on the MotorShield and making the change in the sketch definitions.
the Arduino Motor Shield R3 uses A0 and A1 for current sensing capabilities.

Client7:
Did you manage to make progress on this issue? I had the exact same problem, as a complete beginner using the tutorial from Miguel Grinberg's blog (which I presume is the same one you mention, as he suggests using the A0 and A1 pins as digital 14 and 15).

It was fixed easily enough by shifting the Trig and Echo pins for the sensor to 4 and 7 respectively on the MotorShield and making the change in the sketch definitions. I'm still wondering though why A0 and A1 stopped working when the MotorShield was placed...

EDIT: The issue turns out to be a very simple one (we all have to start somewhere, right?) in that the Arduino Motor Shield R3 uses A0 and A1 for current sensing capabilities. In the end it was a RTFM problem, combined with following a tutorial that used a different motor shield.

Hi
I'm having a different problem using Ultrasonic Sensor SRF05 and the Arduno Motor Shield. Both are working fine when used separately, but once their are both supposed to work together the Motorshield stay quiet (not light and no motor sound) although the Arduino sketch is still running.
I checked for the Pins => the Ultrasonic Module is wired to A4 and A5 finally (for now at least), so that here is no conflict with MotorShield
I checked for the power => MotorShield is plugged on batteries NI-MH 800mA 14.4V, Arduino is plugged on USB cable and the Vin pin of the MotorShield is cut (bended to be not connected for now), it seems to be no power drop off as the sketch still runs
What is funny is that when Echo is not connected, things work but once I connect it the motorShield stops.
Similarly, when Trigger is not connected, things work for 1 second then stop.

The code still works fine, I check it on monitor

Any idea, thanks.

PS: Here is the whole code in case I miss something

#include <NewPing.h> // bibliothèque de gestion des delays

const int VMax=160;
volatile int distance=0;

#define PinEcho A4  // broche Echo sur pin Digital 7
#define PinTrigger A5  // broche Trigger sur pin Digital 6

#define distanceMax 100 // définition de la distance maximale de captation en centimètres. 

NewPing sonar(PinTrigger, PinEcho, distanceMax); // initialisation de la fonction de sonar de notre bibliothèque

int pingLap = 50; // temps entre chaque pulsation en millisecondes.
long pingTimer;     // minuterie entre chaque pulsation.

boolean tourne=false;
long randNumber;

void setup() {
  Serial.begin(115200);
  Serial.flush();
  pinMode(3,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pingTimer = millis(); // initialisation de la minuterie.
}

void loop() {
  if (true) {  // to skip the sensor or to activate it
    if (millis() >= pingTimer)
    {
      Serial.print("Ping ");
      pingTimer += pingLap;
      sonar.ping_timer(echoSonar); // fonction de vérification des conditions.
      Serial.println(distance);
      
      if (distance<20) {
        if (!(tourne)) {
          Serial.println("dist < 20");
          randNumber = random(0,2);
          tourne=true;
        }
        else {
          Serial.println("Tourne");
          deplace((randNumber*2-1)*90,50);
        }
      }
      else {
        Serial.println("dist > 20");
        deplace(0,100);
        tourne=false;
      }
    }
  }
  else {
    deplace(0,100);
    Serial.println("Tout droit");
  }
}

void deplace(int angle,int vitesse) {
  // angle en degrée entre -180 et +180
  // vitesse en %
  int droite;
  int gauche;
  // verification que l'angle est bien compris entre -180 et +180
  while (angle < -180)
    angle = angle + 360;
  while (angle > 180)
    angle = angle - 360;
  // les configurations moteur focntion de l'angle  
  if ((angle >= 0) & (angle <= 90)) {
    droite = VMax;
    gauche = map(angle,0,90,VMax,-VMax);
  }
  else {
    if ((angle >= 90) & (angle <= 180)) {
      gauche = -VMax;
      droite = map(angle,90,180,VMax,-VMax);
    }
    else {
      if ((angle >= -180) & (angle <= -90)) {
        droite = -VMax;
        gauche = map(angle,-180,-90,-VMax,VMax);
      }
      else {
        if ((angle >= -90) & (angle <= 0)) {
          gauche = VMax;
          droite = map(angle,-90,0,-VMax,VMax);
        }
      }
    }
  }
  droite = droite*vitesse/100;
  gauche = gauche*vitesse/100;
  
  if (droite>0)
    digitalWrite(12,HIGH);
  else
    digitalWrite(12,LOW);
  if (gauche>0)
    digitalWrite(13,HIGH);
  else
    digitalWrite(13,LOW);  
  analogWrite(3,abs(droite));
  analogWrite(11,abs(gauche));
  
  Serial.print(angle);
  Serial.print(" + ");
  Serial.print(vitesse);
  Serial.print(" = ");
  Serial.print(droite);
  Serial.print(" + ");
  Serial.println(gauche);
}

void echoSonar() {
  distance=distanceMax;
  if (sonar.check_timer()) // fonction de vérification de la minuterie.
  { 
    distance=sonar.convert_cm(sonar.ping_result);
  }
}

piautr:
Hi
I'm having a different problem

Then start a new thread.

The cause of the SRF05 and MotorShield conflict is due to the use of Timer2 in the NewPing function NewPing Library. Timer2 is also used to controll the 3 an 11 pins PWM of the Arduino Uno.
My rapid solution is to cut (disconnect) pins 3 and 11 from the MotorShield and use pins 5 and 6 to control the MotorShield PWM (connected by wire).
Now it works