HC-SR04 doesn't work when it's connected to Arduino Motor Shield R3

Hi, I'm a new user of arduino and this is my first project.

I'm trying to build a robot that avoid obstacles.

I have both the Arduino Motor Shield R3 and the HC-SR04 sensor and they work fine by them selves but when I connect the sensor to the shield it doesn't work probably, it detect up to 6-7cm only or some times more than 1000cm when the sensor itself can't detect more than 500cm (look at the attachment).

this is the code that I use to test it out:

#include "ArduinoMotorShieldR3.h"
#include <NewPing.h>

#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     4  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

ArduinoMotorShieldR3 md;

void setup()
{

  md.init();
}

void loop()
{
  delay(15);
  unsigned int dist= sonar.ping_cm();
  if(dist != NO_ECHO && dist < 30) {
    md.setSpeeds(200,0);
  } else  {
    md.setSpeeds(200,200);
  }
}

also the motors doesn't work the same, one is faster than the other.

and when I connect the battery to the arduino board(4 1.5v AA), it doesn't work the same as when it's connected by USB.

Deleted...

Hi,
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.

Your code will help a lot.
Sorry you have.... :o

Have you checked if the two pins used by the SR04 is not used by the motor shield?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

also the motors doesn't work the same, one is faster than the other.

Yes that is normal.

TomGeorge:
Have you checked if the two pins used by the SR04 is not used by the motor shield?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

The pins used by the shield are 3, 8, 9, 11, 12 and 13. And this is the library I'm using for the shield GitHub - gallingern/arduino-motor-shield-r3: Arduino library for the Arduino Motor Shield R3 .

And the circuit is in the attachment.

Hi,
Ohh dear.. the OPs image... not circuit diagram.


Tom... :slight_smile:

TomGeorge:
Hi,
Ohh dear.. the OPs image... not circuit diagram.

I'm sorry I don't know what that is :cry: :cry: :'(.

Can you give me an example of what you mean? I'm sorry for being annoying :sweat_smile:

Reading a schematic
http://blog.makezine.com/archive/2011/01/reading-circuit-diagrams.html
Colin's Lab video on reading a schematic

Is the x-tal on your sr04 a 4MHz?
I have many of those.. working on USB-power (4.95v), but NOT when I user external power and the bord got 5V

All my sr04 with an 8 MHz xtal works independent of power source