I wired up my servo and ran the following sample sketch and all I get is a contunous jerky rotation of the server a couple of degrees at a time. Tried a second server with the same results. Verified the wiring several times. This has to be something simple. It also messes up my serial port connection each time.
void setup()
{
servoMain.attach(10); // servo on digital pin 10
}
void loop()
{
servoMain.write(45); // Turn Servo Left to 45 degrees
delay(1000); // Wait 1 second
servoMain.write(0); // Turn Servo Left to 0 degrees
delay(1000); // Wait 1 second
servoMain.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
servoMain.write(135); // Turn Servo Right to 135 degrees
delay(1000); // Wait 1 second
servoMain.write(180); // Turn Servo Right to 180 degrees
delay(1000); // Wait 1 second
servoMain.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
}
Yes, a very common problems beginners have with using servos is assuming you can power them directly from a arduino board without problems. Servos should have their own independent voltage source and allow the arduino to just have to control the servos rather then try and control and power them.
Well, this is interesting, because I'm a newby too, and I've just bought the Arduino experimenters kit, and using the servo supplied with the kit I've wired up the power from the Arduino board and I'm getting very odd behaviour.
Sometimes it works, other times it doesn't and sometimes the servo seems to spin internally, without the arm actually moving.
The code is simple (I hope), it's just intended to rotate the servo 5 degrees every 5 seconds. Have I done anything wrong or is it all down to the power supply ?
//////////////////////////////////////////////////////////////////////
// Turns a servo motor dependant on an analog value
// A value of 0-255 gets written to the output pin
// This represents a duty cycle of 0%-100%
// This is translated as a rotation of 0-180 degress of the servo
//////////////////////////////////////////////////////////////////////
int outputPin = 11;
float fanalogValue ;
int analogValue ;
int degreeRotation=0 ; // the nomber of degrees we want the servo to rotate
void setup()
{
pinMode(outputPin,OUTPUT);
Serial.begin(38400);
}
void loop()
{
if (degreeRotation <= 180 && degreeRotation >= 0) // can't rotate more than 180 or less than 0
{
fanalogValue = degreeRotation/180.0*255.0 ;
analogValue = fanalogValue;
Serial.print("Analog Value:");
Serial.println(analogValue,DEC);
analogWrite(outputPin,analogValue);
degreeRotation += 5;
delay(5000);
}
else {degreeRotation = 0;}
}
hsteve, I think you should try that with the servo library... you don't position a servo with a simple analogue write: the servo library takes care of sending pulses of the correct characteristics with a servo write command.
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
}