2 dc motors and servo with vhn 5019


I’ve built a robot which is moved with 2 dc motors and a servo, connected to a Pololu VHN 5019, wwhich is stacked onto an Arduino Uno. I want the motors to move forwards / backwards only, and the servo to move from 90 º to 110 º (to turn right) and to 90 º to 70 º (to turn left). The servo makes the robot tilt in the desired direction, which provides the turning as the dc motors are moving. I have, however, a problem with the code. When the bluetooth turns on, the servo moves continuosly (it should only move when a button on a mobile phone app is pressed) and the dc motors don’t react. If I eliminate the servo code lines, the motors work just fine. The servo is connected to the pin on the vhn corresponding to 5v in the Arduino. The external power supply is 2 lipo batteries in parallel, supplying 12 v and 6000 mah. Could you please point me in the right direction? I’m also attaching the code:

#include <DualVNH5019MotorShield.h>

#include "DualVNH5019MotorShield.h"
DualVNH5019MotorShield md;

#include <Servo.h>
Servo myservo; 

char dataIn='S';
char determinant;
char det;
int vel = 200; //Bluetooth Stuff

int overdrive = 13; //Press Toggle Switch #1, the pin13 LED will light up

void setup(){


int check()
{if (Serial.available() > 0) {dataIn = Serial.read(); 
        if (dataIn == 'F'){determinant = 'F';} 
        else if (dataIn == 'B'){determinant = 'B';}else if (dataIn == 'S'){determinant = 'S';}
        else if (dataIn == '0'){vel = 400;}else if (dataIn == '1'){vel = 380;}
        else if (dataIn == '2'){vel = 340;}else if (dataIn == '3'){vel = 320;}
        else if (dataIn == '4'){vel = 280;}else if (dataIn == '5'){vel = 240;}
        else if (dataIn == '6'){vel = 200;}else if (dataIn == '7'){vel = 160;}
        else if (dataIn == '8'){vel = 120;}else if (dataIn == '9'){vel = 80;}
        else if (dataIn == 'q'){vel = 40;}
        else if (dataIn == 'U'){determinant = 'U';}else if (dataIn == 'u'){determinant = 'u';}
        else if (dataIn == 'W'){determinant = 'W';}else if (dataIn == 'w'){determinant = 'w';}
        }return determinant;}

void loop(){ det = check();  // You'll need to reconstruct this if your not using the Pololu Dual VNH5019

      while (det == 'F')   // F, move forward
       {md.setSpeeds(vel,vel);det = check();}
      while (det == 'B')   // B, move back
       {md.setSpeeds(-vel,-vel);det = check();}
      while (det == 'S')   // S, stop
       {md.setSpeeds(0,0);det = check();}
      while (det == 'W')
      {myservo.write(110);delay(100);det = check();} 
      while (det == 'w')
      {myservo.write(90);delay(100);det = check();} 
      while (det == 'U')
      {myservo.write(70);delay(100);det = check();} 
      while (det == 'u')
      {myservo.write(90);delay(100);det = check();} 

Thanks in advance!


We moved your post to the “Motor controllers/drivers and motor” section of the forum since it seemed more appropriate.

I suspect you are running into a conflict with Timer 1, which is used by the Arduino servo library and our VNH5019 motor driver shield library. There are a couple ways you could go about working around this.

One way is to use Timer 2 to control the servo. A method for doing this is described in the “Controlling a servo with an Arduino Uno” section of our Zumo shield’s user’s guide.

Alternatively, you could use the alternate constructor for the VNH5019 shield that allows you to remap the PWM1 and PWM2 pins. Remapping these PWM pins causes the library to use analogWrite instead of 20 kHz hardware PWM using Timer 1. Please note that if you choose this option, you will need to remap the physical connections on the board as well. You can find instructions for how to do this in the “Remapping the Arduino Connections” section of the Dual VNH5019 Motor Driver Shield User’s Guide.


Thank you very much for your answer!