Servo twitches heavily while DC motor is running

I'm building a "turret" (a webcam mounted on a servo), that communicates with Processing to find an AR marker taped to a stand, and rigged to a DC motor that kicks the stand over when the user "fires" the turret at the marker. (There's a pic here). The user uses a Leap Motion to rotate the turret, with Processing writing the angle the servo needs to be at over USB.

This works fine, but I want to expand the set-up with a second DC motor that runs continuously, instead of the short burst of the first DC motor. The problem is that while the second motor is running, the servo is twitching heavily. At first I thought it was a problem with the power supply, so I hooked up a second 9V battery for the servo only; this did not fix the problem. I searched around the internet and these forums, but I haven't found a solution yet. Here's hoping some of you know what I'm doing wrong! :slight_smile:

The Arduino code:

//Software servo library to prevent serial communication mishaps
#include <SoftwareServo.h> 

const int SERVO_PIN = 9;
const int MARKER_MOTOR_PIN = 5;
const int GUNS_MOTOR_PIN = 3;

String _serialBuffer;
SoftwareServo _servo;

//For kicking over the marker
boolean _isKicking;
int _timeOfKick;
const int KICK_TIME = 100;

void setup()
{
  Serial.begin(9600);
  pinMode(SERVO_PIN, OUTPUT);
  pinMode(MARKER_MOTOR_PIN, OUTPUT);
  pinMode(GUNS_MOTOR_PIN, OUTPUT);
  _servo.attach(SERVO_PIN);
}

void loop()
{
  _servo.refresh();
  
  while(Serial.available() > 0)
  {
    int character = Serial.read();
    if(character == '\n') //string complete
    {
      setTurretPosition(_serialBuffer.toInt());
      _serialBuffer = "";
    }
    else if(character == 'A' && !_isKicking)
    {
      _isKicking = true;
      _timeOfKick = millis();
      analogWrite(MARKER_MOTOR_PIN, 150);
    }  
    else if(character == 'B') //Start guns
      analogWrite(GUNS_MOTOR_PIN, 100);
    else if(character == 'C') //Stop guns
      analogWrite(GUNS_MOTOR_PIN, 0);
    else
      _serialBuffer += char(character); //Add angle digit to buffer
  }
  
  //Disable DC motor after time
  if(_isKicking && _timeOfKick + KICK_TIME < millis())
  {
    _isKicking = false;
    analogWrite(MARKER_MOTOR_PIN, 0);
  }
}

void setTurretPosition(int angle)
{
  _servo.write(angle);
}

Firstly you need proper power supply for motors and servos. a 9V battery (PP3 size)
is inadequate for any motor or servo, just give up using them. Something like 4 or 5
AA rechargables with decent multi-amp output current is where to start.

The jitter in the servo may be due to power rail noise, in which case a proper supply
will cure it.

Or it could be RFI, interference from the high currents in the motor leads inducing
signals in the servo wiring.

Ensure wires to motors are twisted pair, or even shielded. Keep wiring to motors
and servos well away from sensitive electronics and each other. Ensure you have
a star-ground configuration and that high-current ground paths are outside the
Arduino board itself. Small motors can have a small ceramic capacitor soldered
direct to the terminals to quench brush-arc noise, something like 0.1uF, which can
reduce RFI (especially true on small cheap motors with metal brushes).

Thanks for the fast reply, Mark!

MarkT:
Firstly you need proper power supply for motors and servos. a 9V battery (PP3 size)
is inadequate for any motor or servo, just give up using them. Something like 4 or 5
AA rechargables with decent multi-amp output current is where to start.

Can you elaborate on this some more? From where I'm standing, the servo and motors are running just fine with a 9V battery power supply.

The jitter in the servo may be due to power rail noise, in which case a proper supply
will cure it.

If I understand you correctly, the twitching could occur from a noisy power supply; however, if this were the case, the servo would always be twitching. It's only doing so when a DC motor is running though, and the servo and motor have a different power supply, so this can't be it.

Or it could be RFI, interference from the high currents in the motor leads inducing
signals in the servo wiring.

This seems possible, but the servo signal wire is only close to the motor PWM wire going into the H-Bridge, so there are no "high" currents in there, right? Or am I wrong in assuming that the RFI can't carry over several cm?

Ensure you have a star-ground configuration and that high-current ground paths are outside the
Arduino board itself.

Isn't that a little overkill for 3 tiny motors?

Small motors can have a small ceramic capacitor soldered
direct to the terminals to quench brush-arc noise, something like 0.1uF, which can
reduce RFI (especially true on small cheap motors with metal brushes).

I don't have any capacitors, so I can't try this. :frowning:

From where I'm standing, the servo and motors are running just fine with a 9V battery power supply.

If they are working fine why are you posting here that you have a problem?

It's only doing so when a DC motor is running though, and the servo and motor have a different power supply, so this can't be it.

The DC motor generates interferance and that is what is causing the twitching. Do you want to cure it or not?

Or am I wrong in assuming that the RFI can't carry over several cm?

Yes you are wrong. How far away is your local TV transmitter, more than a few cm I would guess.

Isn't that a little overkill for 3 tiny motors?

No.

I don't have any capacitors, so I can't try this

Then either get some, or take up knitting, maybe electronics is not for you.

Grumpy_Mike:
If they are working fine why are you posting here that you have a problem?

They work fine when the servo and motor are running separately; I meant to say that there's no issue of too little power or anything like that, so I was hoping to get some more info on why 9V batteries are inadvisable.

Then either get some, or take up knitting, maybe electronics is not for you.

You really go out of your way to live up to your nick name, don't you? What a lovely community.

was hoping to get some more info on why 9V batteries are inadvisable.

Because they are expensive and very low current and capacity. That means they are limited in how long they will work or how much power they will provide. Technically they have what is called a high output impedance which exacerbates the sort of problems you are seeing.

What a lovely community.

We are, we give good advice here, and we consider no question is too simple or trivial to answer.

What we get irked at is having good advice argued with. You are the one with the problem. We tell you what to do to put it right and then you argue that it is not correct advice.
If you are so smart you would not have to ask the question in the first place as you would already be doing what we advise you to do.

There is a world of difference between someone who is ignorant, that means that they do not know stuff but want to learn, and a fool. It is fools I do not suffer gladly, the ignorant who want to learn are welcome.

Because they are expensive and very low current and capacity. That means they are limited in how long they will work or how much power they will provide. Technically they have what is called a high output impedance which exacerbates the sort of problems you are seeing.

Thanks, that's good to know.

I believe you misinterpreted my questions as coming off as a know-it-all; I never intended to argue your answers, but learn more about the problem by asking about them. You instead immediately passive aggressively block me out, and preemptively conclude that I'm not willing to learn; not a nice welcome to someone who is new to all this.

Yes well being over 60 I have no idea what passive aggressively actually means. They never had it when I was a lad.

If each motor independently runs OK and without jitter, then I would investigate the possibility of the code itself being the cause. For instance, the while loop could get quite busy ... is it possible to check and process just one character at a time? If so, then the while loop could be replaced with "if" or switch / case statements to smooth out code execution timing.

Can you elaborate on this some more? From where I'm standing, the servo and motors are running just fine with a 9V battery power supply.

What servo do you have that is normally powered by 9v? Most hobby servos have an upper voltage limit of 6v-7.5v.