Newbie question: Servo motor control with Seeed shield v2.0

I am trying to create a rover using a seeed shield v2.0…

I have an ultransonic sensor connected to a sivel type shield that I created with a servo motor in the center, that allows me to turn and ‘look’ for correct paths…

I can get the seeed motors working with an ultrasonic sensor when not using the servo motor, but when I connected the servo motor, the seeed motors stop working.

I can see the seeed motors stopping when I attach the servo to my output pin.

myservo.attach(headPin);

Where the headpin = 2.

I am assuming that there is something going on in this initialization call that is causing a problem with the #include "MotorDriver.h" file provided from seeed.

Does anyone know if this is a known issue?

Thanks in advance for the help!

Hi, welcome to the forum,
This is the shield: Motor Shield V2.0 | Seeed Studio Wiki

How do you power the servo motor ? The Arduino 5V pin can not be used, it doesn't have enough power (current).

I have an ultransonic sensor connected to a sivel type shield

To a what?

Does anyone know if this is a known issue?

Yes, it is a known issue that some people just will not post their code. Nothing can be done about it.

How do you power the servo motor ? The Arduino 5V pin can not be used, it doesn't have enough power (current).

Yes, I am using the 5v pin, I was thinking that may be the problem, but wasn't sure because it works when I only run the servo and not the gear motors...

To a what?

Sorry, I meant to say swivel mount.

I really don't have a set of 'source code' yet... I am just trying to test right now. I have been moving the call to myservo.attach(headPin); trying to see if I can get it to work, so this is not the cleanest code.

Here source code:

#include "MotorDriver.h"
#include "Servo.h"
const int pingPin = 7;
const int headPin = 2;
int pos = 0;
Servo myservo;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  motordriver.init();
  
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  if (cm < 10)
  {
    Serial.print("Danger!! Danger!!");
    Serial.println();
    motordriver.stop();
    //myservo.attach(headPin); 
    if (pos < 45)
    { 
      pos = 45;
    }
    else if (pos < 90)
    {
      pos = -90;
    }
    else if (pos < 180)
    {
      pos =  0;
    }
    myservo.write(pos);
    delay(2000);
  }
  else
  {
    //myservo.detach(); 
    Serial.print("All is clear...");
    Serial.println();
    motordriver.goForward();
    if (pos != 0)
    {
      pos = 0;
      myservo.write(pos);
      delay(150);
    }
  }
    
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
    if (pos < 45)
    { 
      pos = 45;
    }
    else if (pos < 90)
    {
      pos = -90;
    }
    else if (pos < 180)
    {
      pos =  0;
    }
    myservo.write(pos);
    delay(2000);

Will your servo really move to -90?

Yes, the servo moves, but I didn't use-90... I was using 90... Just for testing purpose.

I was going to test if I can use -90 or if I have to use 270 today.

However, if I attach the servo, then the motors stop working.

I was going to try and re-wire the power today to not use the 5v output and see how it goes.

Topic is a little old but I was having a similar problem and the root cause is discussed here:

http://www.seeedstudio.com/forum/viewtopic.php?f=23&t=5715

Apparently the servo.h include disables PWM output on pins 9 and 10 which are used by the seedstudio motor shield library as well.

Hi,

I have the solution to the conflict between the MotorDriver.h and Servo.h files that causes wpm pins 9 and 10 to switch off. Thereby making the DC motors useless.

Seeedstudio replied and recommended the following;

Open the motordriver.h file in a text editor. change the speed pins from 9 & 10 to 5 & 6. Then save the file and restart the IDE.

Next, physically connect male jumper wires from pin 9 to 5 on the headers and pin 10 to 6.

Now my DC motors will work with one Servo motor.

Try at your own risk, but it worked for me. Good luck.