Servo not working with arduino.

I connected my servo (AS-18 mg ACOMS) to my Arduino UNO and uploaded the sweep program. Motor did not move, the load LED blinks. This happens when I connect the arduino with my PC. [Case 1, see image]

It works fine when I connect my arduino to a external power supply (12V). [Case 2, see image]

When I connect external supply to positive and negative of servo and pulse wire of servo to arduino’s pin 9, it doesn’t work. (Arduino is connected to PC in this scenario) [Case 3, see image]

What I want, is to rotate the motor and read the angle in the serial monitor using Serial.print();
I found someone with similar problem and a good explanation here: http://electronics.stackexchange.com/questions/69139/servo-not-working-with-arduino
But the only reply says “use external supply”.

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

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


void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
  Serial.println(pos);
}

Images from Original Post so we don’t have to download them. See this Image Guide

5795b55e37615e388060a52b9b8fb2257fbf3ce7.jpg

a26c2c80ccc7b096fce30a98b8abd4bd6e381dd9.jpg

0e0d518a3a7d863d74d1c9e86bea3358245e38b0.jpg

…R

Give the servo its own power supply with the servo GND connected to the Arduino GND. I think there is no common GND in your third picture.

An Arduino cannot provide enough current to power a servo and if the servo causes the Arduino voltage to fall it can damage the Arduino or at least cause it to work erratically.

...R

And assuming you get the power sorted, let's look at this part:

What I want, is to rotate the motor and read the angle

Just to clarify, in case you think otherwise, you cannot normally read the position of a servo. In your case, you printed the "pos" which you commanded it to go to, but you have no way (normally) of knowing if the servo actually got there. It could have got mechanically jammed along the way: you won't know that.

Nor would you know it even if you used servo.read(), since that only gives you back the last commanded position, still with no guarantee it ever got there.

If you do need to read the actual position, then you will need a feedback servo.

Robin2: Give the servo its own power supply with the servo GND connected to the Arduino GND. I think there is no common GND in your third picture.

Thank you so much! It worked.