Servo vibrates when running sketch?

Howdy,

I am able to get my servo to rotate, but for some reason it starts vibrating after it rotates. I have looked around online and saw that maybe servo.detach(); will work, but (as you can see in my code below), that doesn't really help because I can't set my servo back to another angle after that.

I am just trying to get the servo to rotate when there's motion detected by my PIR sensor:

/*

This will use a PIR, green LED, red LED, and a servo.  When there is no movement detected, the LED will shine RED and the Servo will be at 0 degrees.  

When movement is detected, the greed LED will light and the Servo will rotate 180 degrees.
 */
#include <Servo.h>

Servo myServo;  // create a servo object 
 
int servoAngle;              //variable to hold theangle for the servo motor
int IRpin = 2;               // choose the input pin (for PIR sensor)
int greenLed = 4;            // choose the green LED
int redLed = 3;              // choose the red LED
int pirState = LOW;             // we start, assuming no motion detected
int val = 5;                    // variable for reading the pin status


// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(IRpin, INPUT);
  delay(5000);
  myServo.attach(9); // attaches the servo to pin 8 on the arduino.
  myServo.write(1);            //Start the Servo at 0 degrees
  Serial.begin(9600);
}

void GreenOn(){
    digitalWrite(greenLed, HIGH);   // turn the green LED on (HIGH is the voltage level)
    myServo.attach(9);                 // start sending signals to servo
    myServo.write(179);             // rotate the Servo 180 [really 179] degrees
    myServo.detach();                 // stop sending signals to servo
    Serial.println("Motion detected at: ");
  }

void GreenOff () {
  digitalWrite(greenLed, LOW);
}
void RedOn() {
    digitalWrite(redLed, HIGH);   // turn the green LED on (HIGH is the voltage level)
    myServo.attach(9);                 // start sending signals to servo
    myServo.write(0);             // puts the Servo back at 0 degrees
    myServo.detach();                 // stop sending signals to servo
}
void RedOff() {
  digitalWrite(redLed, LOW);
}

// the loop routine runs over and over again forever:
void loop() {
  val = digitalRead(IRpin); //read the input value from the IR sensor
  if (val == HIGH) { //check if the input is HIGH (i.e. detects motion)
    RedOff();
    GreenOn();
  }
  else { // if there's no movement, stay RED
  GreenOff();
  RedOn();
  }

}

Thanks for any help/ideas!

To detach a servo, that is when the servo is not used for a long time.
You can't attach and detach without any delay.

Can you do a test ? Does this cause vibration ?

for( int i=0; i<10; i++)
{
  myServo.write(50);
  delay(4000);
  myServo.write(130);
  delay(4000);
}

The Arduino can not supply enough current for the servo motor. You need a seperate power supply for those. Do you have that ? Is it a regulated power supply ?

Thanks for your reply - your script does not cause the servo to vibrate! I have tried though using like 5 and 177 (instead of 0 and 179) and that doesn't work - it still vibrates...do I just need to keep lowering the "angle"?

And I'm using the USB from my laptop to power the Arduino. And I also saw someone point this power issue out, but thought that was odd, because I bought the Arduino Starter Kit, and one of the projects (number 5 of 15) uses the servo and has it rotate (and in that project, it does indeed vibrate, as far as I recall). Why would they include a project using the servo, but fail to mention the power source needs to be stronger (is that the right word)? If that is the case, how do I power it with another source?

(I do have a capacitor on there, as the Project 5 says to use, like in this picture [note: I'm not using the potentiometer])

You need a power supply of 5V to power the servo motors.
It could be something as simple as this: Wall Adapter Power Supply - 5VDC, 2A (Barrel Jack) - TOL-15312 - SparkFun Electronics