Trouble interfacing servo to Arduino Due

I was running the Knob example and when I connected a micro-servo to the Arduino Due it started behaving erratically (it started rotating full 360 and stopping for some time in between ). I have a Servo file in Arduino\libraries with the Servo.h and , sam and avr folder containing the ServoTimers.h and Servo source files but not in Arduino\hardware\arduino\sam ......What can be the problem??...... I need some immediate help........

The Knob code-

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

Hello AB08,

Could you tell us which servo you are using? How are you connecting the servo with Due? Are you using a servo shield?
DUE is tolerant to 3.3V. Most of the servos work with higher voltages (4.8-6). It is very important to consider voltage and current operation of your servo before connect it to DUE to avoid damage it. Also read the manufacturer's specs.
Some servos needs specific outputs to drive their clockwise, counter clockwise and stop functions.

regards

I am using an Avionics 4.3g servo and I am powering it up externally with 5V......I have a common ground with the Arduino Due............ The PWM to the servo has a peak value of 3.3V from the board....

I'd recommend you to start powering the servo alone (no load) with 3.3V (low torque) from DUE.
Then, run the following 'test code' to determine the behavior of the servo. Take note of the values when the servo slow down, stop, CW , CCW, etc. Then, you can map your output according to your needs.

#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;

void setup()
{
 Serial.begin(9600); 
 myservo.attach(9);
}

void loop() 
{ 
  val = analogRead(potpin);
  val = map(val, 0, 1023, 700, 2300);
  myservo.writeMicroseconds(val);
  delay(15);
  Serial.println(val);
  delay(500);
}

Good luck!

Palliser:
I'd recommend you to start powering the servo alone (no load) with 3.3V (low torque) from DUE.

No, this is really fraught with danger of damaging the entire board, do not power
a servo from the 3.3V rail of the Due board, even small voltage spikes could blast
the SAM chip, it cannot tolerate anything above 3.6V.

You need a level converter to shift the 3.3V PWM signal to 5V before routing
this to the servo. For instance a 74HCT family chip powered at 5V will interpret
3.3V logic on the inputs correctly and output 5V. Or there are level converter
breakout boards readily available.