ok... so I am using four 12V motors and one ultrasonic sensor with a motor shield... and I am getting some odd results... in my code I have it so when the distance to a object is less than 4 cm it will go backwards and when something is not 4 cm in front of the robot it will go forwards... simple! but the wheels on the left side of my robot will stop! and just do nothing!

The weirdest part about it is when I make some code JUST moving the wheels back and forth the wheels will work PERFECTLY fine. and when I use some code JUST for the sensors... it works fine! but together... it does all that weird stuff.

this is the shield I am using: https://www.pololu.com/product/2507

if there is any information I missed that you need let me know and I will get back with you. I am new to Arduino so I am extremely confused...

the code.

#include "DualVNH5019MotorShield.h"
DualVNH5019MotorShield md;

#define trigPin 7
#define echoPin 8

void setup() {
    md.init();  //setting up wheel code
    Serial.begin (9600);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);

void loop() {
    long duration, distance;
    digitalWrite(trigPin, LOW);  
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;

//This is the sensor testing if somthing is in range!
if (distance < 4){  
        Serial.println("in range");
else {
        Serial.println("not in range");

if (distance >= 200){
else {
        Serial.println(" cm");
  • Why are you delaying the loop 500 ms at the end of each iteration? Feb 22, 2017 at 22:21

1 Answer 1


TLDR: change your project (both program and connection) to use ultrasound sensor on any two of pins 3,5,11 and it should work.

You are using pins 7 and 8 for ultrasound.

At the same time you are using the shield https://www.pololu.com/product/2507 for motors, but if you look at the shield description on picture Dual VNH5019 motor driver shield with an Arduino (shield and Arduino powered separately).https://a.pololu-files.com/picture/0J3753.1200.png?5877a2053bf73d6dbb43d10e7d268ce5 You will see, that the shield is using pin 7 as M2INA and pin 8 as M2PINB

So each loop you push M2INA (trigPin) LOW, then HIGH then LOW, then wait until the sensor write HIGH to M2INB (echoPin) just to immediately send PWM pulses to both trigPin (M2INA) and echoPin (M2INB) via commands


No way it can work as you expect.

You must use other pins (unused by the shield) for your ultrasound sensor. I suggest to declare

#define trigPin 3
#define echoPin 5

and connect your ultrasound sensor there acordingly.

(You can use also pin 11 if you want. I would not recomend pin 13, as there is connected the Arduino LED, which is used by bootloader too so you could confuse the sensor, nor the pins 1 and 2, as they are used by Serial (and USB). Also you can use the "analog pins" A2..A5 as digital pins, but it may confuse you a little (AVR can use "analog" pins as "digital" but many beginers does not like constructs as "digitalWrite(A2)", "digitalRead(A3)").

Also keep in mind, that A4 and A5 are used for I2C communication, if you would use I2C later in project.

  • it works now! thank you very much! this is my first time using this website and it has saved my life :) thank you!!!!!! I tried 3 and 5 and it didn't work... I tried 1 and 0 and it worked great! your smart XD thanks.
    – Max
    Feb 24, 2017 at 20:44
  • Nice, that it works now :) Can you mark it as answer?
    – gilhad
    Feb 24, 2017 at 20:47
  • I think I did it. I clicked the checkmark and the up arrow button.
    – Max
    Feb 24, 2017 at 20:52
  • Maybe it took some time to propagate thru th whole system :)
    – gilhad
    Feb 24, 2017 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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