Servo not spinning according to code

Beginner here.

Trying to make this servo sweep properly but it has been spinning more than 180 degrees.

Hello
Welcome to the Arduino fora.
Before you do anything else please take a moment to read General guidance and
How to use this forum

Without seeing at least your code no one can possibly help you.

++Karma; // Well done for managing to post your photo properly on your first post, most people struggle with that.

Please post your code and describe EXACTLY what the servo is doing e.g. how far is it moving and and when? Is it positioning at all or just continually spinning round?

In general both breadboards and croc clips are a really bad idea when powering things like servos and motors but you should get away with them for a single MG90S.

Steve

Hi there, am using the sweep code from the arduino example library.
Currently, it spins 4 full circles in both directions so it is technically sweeping but not 180 degrees but 4 full rounds.

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 15ms 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 15ms for the servo to reach the position
  }
}

Thank you for posting your code properly.

I don't know the answer other than giving you some vague, not very helpful comments about the code out put not matching what the servo is expecting.

There are people here who know loads about servos, I hope one of them will be able to help.

That's not the actual code you're using. At the very least you are missing the #include <Servo.h> line.

I don't think I've ever seen a standard servo that moves under control for more than a complete circle. It's normally only specific (usually big and expensive) winch servos that can do that.

Try changing myservo.attach(9) to myservo.attach(9, 1000, 2000) and see if that helps. It may be that the range of pulses being sent to the servo is too wide for it. You can also try changing the for loops to so instead of going from 0 to 180 they do use numbers like 30 to 150 or an even smaller range say 60 to 120.

Steve

I second Steve's suggestion to try myservo.attach(9, 1000, 2000)
I have used many of these very cheap servos and often they react to a signal outside of their expected rate by spinning all the way round.
The attach method with the optional parameters of MIN and MAX set to 1000 and 2000 respectively limit the servo signal to that range.
Without those optional parameters, I believe the attach uses default values of 500(ish) and 2400.

Another possibility is that the servo is indeed a continuous rotation servo.

"Hi there, am using the sweep code from the arduino example library.
Currently, it spins 4 full circles in both directions so it is technically sweeping but not 180 degrees but 4 full rounds."

Below is some simple servo test code you can use to test your servo by giving it some basic position commands.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// 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.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}