Servo Becomes Jerky when DC motor runs.

Hello All,

Background and equipment:
I need some help to determine what is causing my HiTech hobby servo to become jerky when I turn on a small 9v DC motor. I am using a arduino Fio v3 to send Serial data about the position of two pots and a switch. One pot controls the position of the servo and the other pot controls the speed of the DC motor. The switch controls whether or not the motor is on. On the receiving end I am using adafruits v1 motor shield with an Arduino uno. I am using two xbee s1 radios to transmit the data. Power source for the receiving arduino uno is a tekkeon power pack. The battery for the motor shield is a wimpy 9v batt. The power sources will be swaped, but haven't yet, due to lack of easy connectors.

Observation:
When the dc motor switch is low (DC motor OFF) the servo responds accurately without twitching. As soon as I turn on the DC motor the servo begins to twitch back and forth. I can still roughly control the servo but it jumps back and forth around the position it should be at.

Below is my code:
Transmitter:

//Transmitter: Fio V3 with Xbee S1 

int potpin = 0;  // analog pin used to connect the potentiometer
int intValue = 0; // an integer valuse (16 bits)
int speedpot = 1; //Potentiometer pin for speed
int speedval = 0; //value to store speed
int direcpin = 15;
int direcstate = 0;
void setup() 
{ 
  //myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(direcpin, INPUT);
  Serial.begin(38400);   // Why so slow? Cause I like it.
  Serial1.begin(38400);  //Open serial port, sets data rate to 9600 bps
} 
 
void loop() 
{ 
  Serial1.print('H');        //send a header character
  
  //Servo Control code
  intValue = map(analogRead(potpin), 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  Serial1.write(intValue / 256);  //Send the high byte
  Serial1.write(intValue % 256);  //send the low byte
  Serial.println(intValue);       //show me the value on my computer

  //DC motor control
  speedval = analogRead(speedpot) / 4;  //scalled to 0-255
  Serial1.write(speedval / 256);  //Send the high byte
  Serial1.write(speedval % 256);  //Send the low byte
  Serial.println(speedval);
  
  //DC motor direction control
  direcstate = digitalRead(direcpin);
  Serial.println(direcstate);
  
  if (direcstate == HIGH)
    {
      Serial1.write(2);
      Serial.println("High");
    }
  else
    {
      Serial1.write(1);
      Serial.println("Low");
      Serial.write(1);
    }
  
  delay(250);                 // waits for the servo to get there
}

Receiver:

//Reciever: Arduino Uno with Xbee S1

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

#include <AFMotor.h>
AF_DCMotor motor(1, MOTOR12_64KHZ);  // create motor #2, 64KHz pwm

boolean flag = false;
//Servo variables
int value1 = 0;  //value to hold the servo postion once assembled
int value2 = 0;
int tvalue = 0;

//Motor Variables
int speed1 = 0;
int speed2 = 0;
int dc_speed = 0;
int dc_direc = 0;

//Long without DELAY()
long previousmillis = 0;
int interval = 250;

 void setup() 
{ 
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(38400);  //Open serial port, sets data rate to 9600 bps
  pinMode(7, OUTPUT); // LED showing me something is going on when the servo stops moving
} 
 
void loop() 
{ 
  unsigned long currentmillis = millis();
  
  if(currentmillis - previousmillis > interval)
  {
      previousmillis = currentmillis;
      if(Serial.read() == 'H')  // is this the header
      {
          //Servo control
          value1 = Serial.read();   //read the most sig fig
          value2 = Serial.read();   //read the least sig fig
          tvalue = value1 * 256 + value2;   //assemble the number
          tvalue = constrain(tvalue, 17, 114);  //min and max travel positions learned by trial and err..destruction
          myservo.write(tvalue);                  // sets the servo position according to the scaled pos value

          //Motor Control
          speed1 = Serial.read();   //read the most sig fig
          speed2 = Serial.read();   //read the least sig fig
          dc_speed = speed1 * 256 + speed2;   //assemble the number
          dc_speed = constrain(dc_speed, 40, 255);
          dc_direc = Serial.read();

          if (dc_direc == 2)
            {
              motor.run(FORWARD);
            }
          else
            {
              motor.run(RELEASE);
            }

          motor.setSpeed(dc_speed);
      }
  //delay(250); // chill out for a bit (REMOVED B/C OF ILL AFFECTS OF MOTOR Pausing)
  
  Serial.flush();
  }
  
  
  //Show me that I am not stuck in the while loop
  if (flag == false)
  {
    digitalWrite(7, HIGH);
    flag = true;
  }
  else
  {
    digitalWrite(7, LOW);
    flag = false;
  }
}

At the moment I can't think of anything that would be causing the jerky motions of the servo only when the dc motor is on.

P.S. Thanks to PaulS for his info on Can't send integers correctly over Serial - Interfacing - Arduino Forum I wouldn't have made it this far without this forum and posts like his, and AWOL.

I can't see what I/O pins the DC motor is connected to ?

I presume motor.run(FORWARD) just starts the motor moving and it runs indefinitley ?
What is motor.run(RELEASE) supposed to do ?
Is there a STOP option? If so what is the difference.

A PP3 type 9v battery is pretty much useless for any purpose with an Arduino. Try a pack of 6 AA cells.

How is the servo powered ? It should NOT be powered from the Arduino 5v pin.

Serial.flush() is only used when sending data. It's purpose is to wait until all characters have been sent. It does not empty the serial input buffer. It is very badly named.

...R

At the moment I can't think of anything that would be causing the jerky motions of the servo only when the dc motor is on.

In adequate or inappropriate power supply setups would be something to look at.

Robin,

According to the adafruit motor shield FAQ Pin 11 is used to run the DC motor connection #1 where I have my motor hooked up.

According tot he adaftui AF_DCMotor_class FORWARD sets the direction of spin. Which depends on the polarity of the motor. RELEASE "Stop the motor. This removes power from the motor and is equivalent to setSpeed(0)." I used the release because there setSpeed(0-40) couldn't overcome the friction forces and spin the motor. So I would skip those speeds with my constrain(speed, 40, 255), and use RELEASE to stop the motor.

Understood about the battery.

The servo is connected to the servo pin connector on the adafruit motor shield. According to this the power for the servo is drawn from the arduino 5v power. If i want to supply the servo with external power i need to cut a trace on the board.

I think that answers all the questions.

I did switch the power sources. I hooked the Tekkeon MP3300 up to the motor shield to power the DC motor. The servo pulled its power from the arduino's 5v regulator which was powered by a 9v battery. The servo would still twitch when I turned on the dc motor. it did not prevent us from controlling the servo. It just twitched around the position we where telling it to be in.

Some more background information. This is a for a RC Boat. I was using this project as a tool to teach my nephew and niece about electronics, programming and some woodworking. We built the boat out of simple stuff we had laying around. For example the "Boat" was a piece of 1.5" thick flat foam. The rudder was made out of a piece of cherry wood that I had laying around, connected to a block of cedar and a piece of coat hanger for the pivot pin. The motor was attached to a prop I purchased from a hobby store for $3. It was a prop for a traxxas villian boat. The drive shaft was a piece of coat hanger. We used a mechanical pencil tube to run the drive shaft through the foam and to act as a bushing at the end closest to the prop to prevent it from slapping around. I turned a coupler from a piece of aluminum at work and drilled a 2mm hole in one end for the dc motor and ~0.090" hole for the coat hanger. I tapped some holes for set screws to attach the coupler to the shafts. I attached the servo to the rudder with some scrap pieces of wire. The motor used for the prop was a simple cheap dc motor I had laying in a drawer. We also placed a keel on the boat made from a scrap of ceder. We tested the boat yesterday and it worked well. We had a lot of fun working on this project. I think they enjoyed the project and learned something. Hopefully I inspired them to create their own projects. The boat was very slow by the way. The strength of the DC motor was lacking. But we where able to send the boat out into the middle of a lake and steer it wherever we wanted. I'm not an RC hobbyist but I may have been bitten by the bug. I am thinking of replacing the motor shield with a RC ESC and purchasing a brushless motor. I would need a new hull. Version 2 coming soon:)

According to this the power for the servo is drawn from the arduino 5v power.

This is very, very bad advice. Whoever wrote that should know better, and Adafruit does a disservice by suggesting it.

The Arduino cannot safely supply enough power to most servos and you stand a chance of damaging the Arduino if you try.

You should always power motors and servos from a separate power supply. Otherwise you are pretty much guaranteed to have the sorts of troubles you are having.

A 4 cell AA pack works well to power 1 or 2 servos. You don't need to cut any traces, just make sure all the grounds are connected together.