Servo vibrates

hey guys i connected the servo and everything works fine including the sweep

but when i use this code to read input from switch and then sweep servo it vibrates

#include <Servo.h>
int switchpin=7; //Input From button
Servo myservo;
int pos=1;
int value;
void setup()
{
  pinMode(switchpin, INPUT);
  myservo.attach(9);
  Serial.begin(9600);
}
void loop()
{
  value=digitalRead(switchpin);
  Serial.println(value);
  if(value==1)
  {
    for(pos = 1; pos < 45; pos += 1) 
  {                                 
    myservo.write(pos);             
    delay(35);                     
  } 
  for(pos = 45; pos>1; pos-=1)  
  {                                
    myservo.write(pos);            
    delay(1);                       
  }
  }
}

is it because of coding error or the signal wire being lose??

i dont understand it, it acts wiered sometimes it works sometimes it doesnot, it vibrates sometimes :frowning:

You probably need to explain how your servo is connected to the arduino, how you are powering the servo, and just what your code is supposed to do.

i connected it using the arduino power supply

the 5v and the ground pins on the arduino to the servo and the signal to the pin 9

and my code just reads an input from pin 7 and executes it executes sweep once.... and yeah input is working fine ive checked the input readings on serial monitor using println function:)

yeah so powering it i am doing it directly from arduino connected to pc via usb, and when ever i connect the servo the device unmounts itself from windows and device goes into unrecognized and yeah servo works fine with normal sweep but doesnot with my code in which i just pasted the sweep code into the if loop

kiriti:
i connected it using the arduino power supply

the 5v and the ground pins on the arduino to the servo and the signal to the pin 9

and my code just reads an input from pin 7 and executes it executes sweep once.... and yeah input is working fine ive checked the input readings on serial monitor using println function:)

yeah so powering it i am doing it directly from arduino connected to pc via usb, and when ever i connect the servo the device unmounts itself from windows and device goes into unrecognized and yeah servo works fine with normal sweep but doesnot with my code in which i just pasted the sweep code into the if loop

Always, always use an external power supply for a servo; servos can easily overload the Arduino's voltage reg output, causing power sags and resets, etc. Put an external supply on the servo (and tie the grounds together). If you still run into problems, post your schematic and code, please!

Hi Kiriti,

I experienced a similar problem when I was building project CIRC-04 from the SparkFun Inventor's Kit. The example code provided with the SparkFun Inventor's Guide is very similar to your code sample so I suspect you might be performing the same project.

Although the documentation said the servo supports positions 0 through 180, it seems that positions 1 thru 14 are actually reserved for continuous rotation at different fixed speeds. 1 is fast. 2 is slightly slower. Approaching 14 gets slower and slower.

This means I had to limit my range of motion to positions 15-180. When I adjusted the code to only rotate the server to positions within this range, it worked fine.

If I wanted to watch the servo slowly spin non-stop, I could instruct the servo to seek position 14 and watch it turn slowly. For more excitement, I could instruct the servo to seek position 1 and watch it spin faster. Fun stuff! :slight_smile:

I suspect different servos could have different properties. The servo included in my kit was labeled "Micro Servo 9g A0090". If your servo is different, you might try stepping through various target positions to isolate which values provide the continuous rotation feature.

Hope this helps,
-John

i've just started messing around with servos

as far as i can see, driving the servo beyond its physical limits will cause the vibrating

i discovered when using the sweep programme that i needed to make the limits between 30 and 170 degrees before the servo would stop chattering

I want to just support the answers listed here already. When the servo is near 0 or 180, there is vibration. I keep the values around 20-150 (which is all that I need) and the servo is perfectly still when holding those positions. Experiment maybe with the kind of servo you have.... mine are Hitec HS-81 micros.

So, do these cheap servos always come with a "surprise range"?
For the record, mine is also a Micro Servo 9g A0090 useful between 9 and 170º.

Hi,
You never power servos or motors from the Arduino's 5v, it can't take it! Your servo can use 1A to drive it, but will pull the arduino supply down, causing a brown-out or reset..

So you need a seperate supply for the servo but common the Gnd's, and yes not all servos do 0-180? Power should be a healthy 4 x AA's pref Nimh rechagables.

Get back to us, show us what you have done, etc. Most servo problems are power/connection related.

Hope it helps, Regards.

Mel.

Simple, this loop is happening every time round loop() - which you obviously didn't
mean to happen:

  for(pos = 45; pos>1; pos-=1)  
  {                                
    myservo.write(pos);            
    delay(1);                       
  }