servo sg90 is not working

i have a proble with my project. it consist of servo motor with arduino uno. when i upload the code to arduino, motor does not rotate. i'm giving power supple by use to arduino. there arn't any errors when verifing the code. i'm first time using the servo motor so any help will be appreciated.

code:

#include <Servo.h>

Servo middle;

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

void loop()
{
middle.write(90); // sets the servo position according to the value(degrees)sk
delay(300); // doesn't constantly update the servos which can fry them
}

sketch_jan15b.ino (329 Bytes)

Your code only sets the servo to the 90 angle over and over again. Once it's at the 90 position, it will not move.

I recommend starting with the File > Examples > Servo > Sweep example sketch. There is a tutorial for that sketch here:

Yeah you are not moving your servo motor, it's stationary at 90 degree. Try this code out. Servo Motor is attached to pin # 8 of arduino. It will also send response to Serial Terminal.

#include <Servo.h> //library for servo motor
 
Servo myservo;  // servo motor object for its control
 
int ang = 0;    // a variable to store the servo angle
 
void setup() {
 
  Serial.begin(9600);
  
  myservo.attach(8);  // servo motor is attached to pin no 8 og Arduino
 
}
 
void loop() {
 
  for (ang = 0; ang <= 180; ang += 5) // goes from 0 degrees to 180 degrees with a step og 5 degree
  { 
    myservo.write(ang);              // rotates the servo to rotate at specific angle
    delay(50);     // adding delay of 50 msec
    Serial.println("Motor has started its rotation from 0 to 180 degress");
      }
  for (ang = 180; ang >= 0; ang -= 5) // goes from 180 degrees to 0 degrees with a step of 5 degree
  { 
    myservo.write(ang);              // rotates the servo to rotate at specific angle
    delay(50);                      // adding delay of 50 msec
    Serial.println("Motor has started its rotation from 180 to 0 degress");
      }
}

This is nonsense:

delay(300); // doesn't constantly update the servos which can fry them

By default, the servo library in any case reminds the servo where it's supposed to be on a 20ms (50Hz) cycle.

Without that, if there's a load on the servo it would move away from where it's meant to be.

thanks for the help! i tried sweep code and jackthom41's code but motor does not rotate. do i need to give external power supply to servo?

Yes :slight_smile:

Sdongre:
thanks for the help! i tried sweep code and jackthom41's code but motor does not rotate. do i need to give external power supply to servo?

septillion:
Yes :slight_smile:

And make sure the servo/power -ve side is also taken to the Arduino ground

now im having new issue with servo. im using 4 servos for making robotic arm. one of them is used with a claw for grabbing the objects. for this i have to place servo upside down but when i do this servo stops working, it started creating a noise but did not move. what should i do?

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
void setup() {
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  //delay(10);
}
void loop() {
  pwm.setPWM(0, 0, 390);
  delay(2000);
  pwm.setPWM(0, 0, 300);
  delay(2000);
}

i have to place servo upside down but when i do this servo stops working, it started creating a noise but did not move. what should i do?

Put it in the bin and get another one