Servo motor not working properly

So, I'm using a SG90 servo motor but when I start it, it's like it's stuck.

I'm using this simple code

#include <Servo.h>

Servo myservo; 
int pos = 180; 

void setup() {
  myservo.attach(9);
}

void loop() {
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);         
    delay(15);         
  }
  delay(1000);
}

@subom7
Try making the delay 50

Hi, @subom7
Welcome to the forum.

Where in your code do you sweep the servo back the other way?
Try the "SWEEP" example in the IDE.

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

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

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

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 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}

Tom.. :grinning: :+1: :coffee: :australia:

After using this code that you gave, it's working fine for some seconds but stops then after i restart the Arduino it again works for a few seconds and again stops

How are you powering the servo?

Arduino nano; laptop

You need a separate power supply for the servo.
The nano can't supply enough curent for the servo

A 5V power bank or charger or 4 AA bateries will do