Servo moving without instructions. Power loss or code error?

Hi Guys

New to electronics, but I got an Arduino starter kit about two weeks ago as a gift and started playing around a bit. I’m loving the Arduino project so far! :slight_smile:

Anyway, I started building a very basic radio-controlled car (Wireless XBee, Servo’s and DC motors) and noticed some strange behaviour:

I have the Wireless Shield with XBee plugged into the Arduino and am driving both an H-Bridge for the DC motor (http://itp.nyu.edu/physcomp/Labs/DCMotorControl) as well as the Servo motor included in the starter kit from the Arduino board. (I just used the example in the book) I then created a little “power-supply” for a 9V battery (http://luckylarry.co.uk/arduino-projects/making-a-9v-battery-dc-power-supply/ - I’m planning to rather build a battery pack of 8xAA’s so it can last longer than 10 minutes…) and what I noticed is: when I move the DC motor by setting PIN3 HIGH or PIN4 HIGH the Servo starts “jittering”.

I didn’t include the decoupling capacitor as per the first link’s article and am wondering if this could be the cause? It’s not like the board loses power or resets itself, it’s just that the servo moves without being instructed to?

My code is as follows:

#include <Servo.h>

const int motor1 = 3;
const int motor2 = 4;
const int enable = 9;
Servo xAxis;
String serIn = "";
boolean bDone = false;

void setup() {
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(enable, OUTPUT);
  xAxis.attach(8);
  Serial.begin(9600);
  serIn.reserve(10);

  digitalWrite(enable, HIGH);
  xAxis.write(90);
}

void loop() {
   if (bDone) {
     if (serIn.substring(0, 1) == "B") {
       digitalWrite(motor1, HIGH);
       digitalWrite(motor2, LOW);
       delay(2000);
       digitalWrite(motor1, LOW);
     }
     else if (serIn.substring(0, 1) == "F") {
       digitalWrite(motor1, LOW);
       digitalWrite(motor2, HIGH);
       delay(2000);
       digitalWrite(motor2, LOW);
     }
     else if (serIn.substring(0, 1) == "L") {
       for(int i = 90; i > 30; i--) {
         xAxis.write(i);
         delay(10);
       }
       xAxis.write(90);
     }
     else if (serIn.substring(0, 1) == "R") {
       for(int i = 90; i< 150; i++) {
         xAxis.write(i);
         delay(10);
       }
       xAxis.write(90);
     }
     bDone = false;
     serIn = "";
   }
}

void serialEvent() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == '\n')
      bDone = true;
    else {
      serIn += inChar;
    }
  }
}

I’ve hooked up the second Arduino with Wireless Shield and XBee to the PC and am just sending the commands directly from the serial monitor to test for now…

Below is a rather crude modification to the picture I used from the article to show you exactly how my circuit looks :slight_smile: (Minus the wireless shield of course)

I didn't include the decoupling capacitor as per the first link's article and am wondering if this could be the cause? It's not like the board loses power or resets itself, it's just that the servo moves without being instructed to?

Decoupling capacitors are not optional components.

The symptoms of inadequate decoupling can be many and various and hard to diagnose, so always include decoupling of every chip before even attempting to run the circuit.

I didn't include the decoupling capacitor as per the first link's article and am wondering if this could be the cause? It's not like the board loses power or resets itself, it's just that the servo moves without being instructed to?

As others have stated, decoupling capacitors are MANDATORY. Motors generate lots of noise. I also put a .01uF ceramic across the motor terminals or two caps to the motor case.

Examining you image, I notice that the servo, which contains a DC motor, and your motor draw power from the UNO 5 volt regulator. A unwise design leading to noise injection and brownouts. Motors should draw directly from the source before the regulator. The servo may not like the 9v input (depending on the model). About 7v is desirable. 2 cell LiPo is the perfect choice.

Joe

That 9v battery will not last long at all. Servos and motors need external power supplies. Powering the motor from the arduino PWM pins might damage or kill your arduino.

Remeber, standard Servo's work on 4,8 to 6 volts, special HV (High voltage) models can take up to 7,4V - 9V will kill them in the long run.

// Per.